Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JFreeChart get mouse coordinates

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

like image 981
Jeff Storey Avatar asked Oct 02 '09 22:10

Jeff Storey


1 Answers

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());
like image 106
ChssPly76 Avatar answered Oct 17 '22 08:10

ChssPly76