JFreeChart get mouse coordinates
Asked Answered
M

1

9

Is there a way in JFreeChart to determine from a ChartMouseEvent that x,y coordinates (in plot space) the mouse is over? I've tried using the domain crosshair value but that seems inaccurate and lags the actual mouse event.

thanks,

Jeff

Maintopmast answered 2/10, 2009 at 22:33 Comment(0)
F
17

Mouse coordinates from getTrigger() are relative to ChartPanel so you need to convert them:

Point2D p = chartPanel.translateScreenToJava2D(mouseChartEvent.getTrigger().getPoint());
Rectangle2D plotArea = chartPanel.getScreenDataArea();
XYPlot plot = (XYPlot) chart.getPlot(); // your plot
double chartX = plot.getDomainAxis().java2DToValue(p.getX(), plotArea, plot.getDomainAxisEdge());
double chartY = plot.getRangeAxis().java2DToValue(p.getY(), plotArea, plot.getRangeAxisEdge());
Fruitcake answered 2/10, 2009 at 22:55 Comment(2)
Thanks. I couldn't figure out what the plotArea argument was supposed to be. Worked like a charm, appreciate it.Maintopmast
Here seems to be one conversion layer too much. I get incorrect results with this if the ChartPanel has been scaled from its default dimensions. Removing the translateScreenToJava2D step and supplying the point from MouseEvent.getPoint() directly to java2DToValue gives correct values in that case too.Sacrum

© 2022 - 2024 — McMap. All rights reserved.