1

I need to be able to take a point, as returned from a MouseListener, and identify the nearest point in a scatter plot. I'm using JFreeChart with an XYDataset.

I have added a mouse listener to my ChartPanel, and am trying to compare these x,y values to those returned when I iterate through the data and check their locations. My code looks something like this:

ValueAxis domainAxis = chartPanel.getXYPlot().getDomainAxis();
ChartArea chartArea = chartPanel.getChartRenderingInfo().getChartArea();

for(int i=0; i < myXYData.getItemCount(0); i++) {
    double mouseX = e.getX(); // e is the MouseEvent
    double pointX = domainAxis.valueToJava2D(myXYData.getX(0, i), chartArea, RectangleEdge.BOTTOM);
    System.out.println("difference is " + (pointX - mouseX));
}

The problem is that the MouseEvent is reporting points relative to the top-left of the ChartPanel, so (0,0) is above the title, and left of the x-axis labels. The valueToJava2D method, however, is giving values relative to the area where the values are plotted, so (0,0) is below the graph title and to the right of the x-axis labels. This means that I get a non-zero difference when the mouse is directly over one of my data points!

How can I resolve this disparity?

thanks, Eric

Eric Lindauer
  • 1,813
  • 13
  • 19

1 Answers1

1

I'd look at a ChartMouseListener, illustrated here and here.

If that is not sufficient, you may have to have to look at the axis, where the area and edge have meaning.

It may help to explain the goal, rather than your current approach. Alternatively, edit your question to include an sscce that shows your current approach. Many short examples may be found here.

Addendum: The practical limit on the max # of points is quite small.

For a reasonable number of points, you can enlarge the Shape, as shown here and here; see also ShapeUtilities. To call out details, consider a custom tooltip generator, examined here.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • ChartMouseListener works great when the mouse is directly over one of the points in the graph. However, I would like to be able to find the closest point when the mouse is not directly over a point. Any ideas? My goal is to provide useful feedback to the user when the mouse is merely close to a point, but not directly over it. – Eric Lindauer Sep 20 '12 at 05:21
  • A "closest" match will surely require iterating over the data set using some suitable metric. As this seems resource-intensive, can you elaborate on the goal? – trashgod Sep 20 '12 at 14:08
  • The graph represents game requests for a chess server. The graph looks nice, but the points are a bit small. I would like to make the effective click area where the user accepts a game bigger, without changing the aesthetic of the graph. I would also like to give information about the challenge (rating, time etc) when the user is "close" to the point, without requiring quite so much precision that they get the mouse exactly over it. The practical limit on the max # of points is quite small, so the time to iterate and do the math is not a problem. – Eric Lindauer Sep 20 '12 at 18:46
  • 1
    Ah, that makes sense. I've suggested a few simpler alternatives above. – trashgod Sep 20 '12 at 19:30
  • Thank you for taking the time to suggest these work-arounds. Making the shapes a little bigger is an improvement. I guess the conclusion is that you aren't aware of any way to find the offset from the top left of the ChartPanel to the top left of the plot. Oh well. I guess that's why they give us the source code... :) – Eric Lindauer Sep 20 '12 at 19:54
  • I'm sure it's possible; I've just never gone there. :-) – trashgod Sep 20 '12 at 20:07