1

I want to get the exact instance of XYDataItem when a mouse click makes the crosshair lock on the nearest data point, as the picture below shows.

The crosshair now locks on a data point

Now I am able to get the X-Y position of the crosshair when a mouse click event happens with a ChartMouseListener, but it may not be good to get the XYDataItem according to the X value and Y value. From the mouse event, another thing is ChartEntity, but it is based on where the mouse clicks, not where the crosshair locks on.

So.. Is there any better way to let me do this?
Or
if you want to make show a lot of additional data related to the data point each time when the crosshair locks on it, how will you do that? (Now I am extending XYDataItem and adding a new field to the sub class so that I can either put the information directly in it, or put an ID that can help me get the additional data from the data list, and that's why I hope the crosshair can give me the exact data item).

Dante WWWW
  • 2,729
  • 1
  • 17
  • 33

1 Answers1

2

Add a ChartProgressListener. Once drawing has finished, ask the XYPlot for the domain and range crosshair values. Locate the corresponding value in your XYDataset.

Use a tooltip to display data in your chart, as shown here. You can override generateLabel() to add your custom data. Alternatively, update an external component's model, as shown in the miscellaneous crosshair demos.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Thank you trashgod. In `XYSeries`, the only way that I can get a XYDataItem is to use `getDataItem(int index)`, and there is also a function named `indexof(Number x)` which returns the index of the item with the x value, so I should have been able to use this to get the index and then get the item -- however, I can't. There will be a lot of `XYDataItem`s with the same x value in the `XYDataset`, so I assume I will get the index of the first one with the x value when I use indexof -- that is a problem. – Dante WWWW Dec 28 '11 at 13:57
  • Won't the combination of domain (_x_) and range (_y_) be unique? An an [sscce](http://sscce.org/) may be helpful. – trashgod Dec 28 '11 at 14:11
  • The combination will be unique, but `indexof` just has a 'x' as the parameter. I will prepare an sscce with sample data. – Dante WWWW Dec 29 '11 at 01:36
  • 1
    Ah, I see; you may need to do a [full scan](http://www.jfree.org/jfreechart/api/javadoc/src-html/org/jfree/data/xy/XYSeries.html#line.855) of the model. – trashgod Dec 29 '11 at 01:45
  • Update: I checked again and I will have to say I can't make sure if the combination could be unique, because the x is revision number in svn and the y is the score of the benchmark test, and I often test one revision for multiple times, thus there may be a same x and y. But if there are two points with the same x and y value, I can just ignore it. But those with same x and different y could not be ignored. – Dante WWWW Dec 29 '11 at 01:52
  • Do you mean... do the iteration in my own code -- compare both x and y? – Dante WWWW Dec 29 '11 at 01:56
  • 1
    Exactly. You may also need to do an [epsilon comparison](http://stackoverflow.com/questions/356807/java-double-comparison-epsilon). – trashgod Dec 29 '11 at 01:59
  • Comfirmed this post as answer and +1 for the two comments that helped me a lot. Thank you trashgod. – Dante WWWW Dec 29 '11 at 02:15