I have a XYSeries chart and I want to get the coordinates of a point when I'm clicking on it. I implemented the MouseClicked() method and I use event.getTrigger().getX() and event.getTrigger().getY() to get the Java2D screen coordinates. I know I have yet to use the translateJava2DtoValue() (or maybe Java2DtoValue() ? ) method to get the corresponding coordinates on the axis, but in the post I read ( can't post url because I'm new ) it's said that it's an axis classes methods, and I don't know how to have access to this method. I probably need to get the axis, but I don't know how to.
Thanks for helping.
Edit : ok I found it I think (using plot and getDomainAxis). By the way, the method Java2DtoValue() needs (double java2DValue, Rectangle2D area, RectangleEdge edge) and I don't know to what correspond Rectangle2D and RectangleEdge. Any clue ?
getting coordinates with XYSeries
Hello,
try this:
try this:
Code: Select all
public void chartMouseClicked(ChartMouseEvent event) {
if (null == event) return;
// screen coordinates
int screenX = event.getTrigger().getX();
int screenY = event.getTrigger().getY();
// data coordinates
ChartEntity entity = event.getEntity();
if (entity instanceof XYItemEntity) {
XYItemEntity itemEntity = (XYItemEntity) entity;
XYDataset xyDataset = itemEntity.getDataset();
int seriesIndex = itemEntity.getSeriesIndex();
int itemIndex = itemEntity.getItem();
double xDataValue = xyDataset.getXValue(seriesIndex,itemIndex);
double yDataValue = xyDataset.getYValue(seriesIndex,itemIndex);
System.out.printf("The screen coordinates [%d,%d] contain the data point [%f,%f].",
screenX,screenY,xDataValue,yDataValue);
}
}