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

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406

1 Answers1

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());
ChssPly76
  • 99,456
  • 24
  • 206
  • 195
  • Thanks. I couldn't figure out what the plotArea argument was supposed to be. Worked like a charm, appreciate it. – Jeff Storey Oct 02 '09 at 23:21
  • 7
    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. – Joonas Pulakka Apr 11 '11 at 09:51