How to retrieve XYItemEntity from Domain/RangeCrosshair

A discussion forum for JFreeChart (a 2D chart library for the Java platform).

How to retrieve XYItemEntity from Domain/RangeCrosshair

Postby snoopygee » Mon Feb 23, 2009 11:30 am


I have set the following in my chart:


This enables the functionality that wherever I click on the chart the doman and range crosshairs jump to the nearest data point on my chart. This functionality is exactly what I'm looking for.

The problem is that I cannot seem to identify the XYItemEntity that the crosshair lines have repositioned themselves over. The ChartMouseListener only returns information regarding the actual position that you clicked your mouse, but not the new position that the crosshairs have been placed.

Any ideas how to retrieve the new position and therefore the valid XYItemEntity?

Posts: 18
Joined: Wed Jan 28, 2009 3:10 pm

Re: How to retrieve XYItemEntity from Domain/RangeCrosshair

Postby kandidat » Wed Mar 04, 2009 9:04 pm


i have the same problem. Did you or anybody find a solution?

In the Documentation is no hint for that.

Posts: 7
Joined: Thu Dec 04, 2008 11:39 pm

Re: How to retrieve XYItemEntity from Domain/RangeCrosshair

Postby RichardWest » Thu Mar 05, 2009 12:00 am

You can get the value of the crosshairs via getDomainCrosshairValue() and getRangeCrosshairValue().
Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Re: How to retrieve XYItemEntity from Domain/RangeCrosshair

Postby kandidat » Thu Mar 05, 2009 11:17 am


to get the Value is not the Problem. I want to get the Dataset or exactly the Item on the Dataset on which th crosshair is actually "connected".

I fond a solution, but i hope i can find it easier.

Code: Select all
          // Transferring  Domainx-Values to items

                    //  5000    6000                   10000  Domain values 6000 is the selected DomainValue
                    //  0       x                       1000  items in the plot
                    // I'm searching for x
                    double vdiff = itemN-item1;   // 5000
                    double vdiff2 = domainCrosshairValue - item1;     // =1000
                    double vquot = vdiff2/vdiff;  // =1/5
                    double ixx = itemCount * vquot;   // =200;

                    // with ixx i can get now the the y-values of other datasets in the plot.
                    double y = datasetCollection.getYValue(datasetIndex, (int)ixx);

I think there must be an easier way to get the ixx-value, like int getDomainCrosshairItemIndex() or XYItem getDomainCrosshairItem()
Posts: 7
Joined: Thu Dec 04, 2008 11:39 pm

Re: How to retrieve XYItemEntity from Domain/RangeCrosshair

Postby david.gilbert » Thu Mar 05, 2009 10:25 pm

The CrosshairState object (used temporarily by the XYPlot class to track/update the crosshair position as each renderer processes data values) does record the dataset index but never makes that accessible to the outside world. It probably makes sense to add 'seriesIndex' and 'itemIndex' to the info being collected, then write that into fields on the XYPlot class (as we do currently for the actual values) then you could retrieve the indices after the plot is drawn. Can you enter a Feature Request in the database at SourceForge and reference this thread in it?
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
JFreeChart Project Leader
Posts: 11655
Joined: Fri Mar 14, 2003 10:29 am

Re: How to retrieve XYItemEntity from Domain/RangeCrosshair

Postby stefan__ » Fri Jan 08, 2010 3:27 pm


do you know the status of this feature request, i.e Is it being worked on or under consideration?

Posts: 4
Joined: Thu Jan 07, 2010 4:17 pm

Re: How to retrieve XYItemEntity from Domain/RangeCrosshair

Postby paradoxoff » Tue Jan 12, 2010 9:42 pm

Since neither the feature request nor the patch list contains a relevant entry, I assume that this feature is not on the todo list.
In principal it should be easy to add the missing methods to the CrosshairState class, but the additional series and item index are meaningless for a CategoryPlots. The CategoryPlot uses an instance of CategoryCrosshairState for tracking which offers to store the row key and column key.
Consequently, one could use an instance of XYCrosshairState for an XYPlot. Interestingly, this class is already included in JFreeChart. It has no methods except a non argument constructor, but the missing methods could be easily added. Unfortunately, the drawItem method of XYItemRenderer only uses an instance of CrosshairState, not XYCrosshairState. Changing that would break the XYItemRenderer interface!
Since the CrosshairState is only used in AbstractXYItemRenderer.updateCrosshairValues(CrosshairState, double, double, int, int, double, double, PlotOrientation) one could still perform the necessary cast from CrosshairState to XYCrosshairState in this method.
Once the series and item index are stored in the XYCrosshairState instance, it should be trivial to add the missing private fields and public getters get[Dataset/Series/Item]IndexAtCrosshair.
Posts: 1622
Joined: Sat Feb 17, 2007 1:51 pm

Re: How to retrieve XYItemEntity from Domain/RangeCrosshair

Postby morgandev » Wed Dec 01, 2010 12:52 am

This would be very nice, and would match functionality already available for category plots.

I've added it to the feature request list :) ... tid=365494

As far as I can tell, this will require a change to the signature of AbstractXYItemRenderer.updateCrosshairValues and the committers might be wary of making this change. In the meantime I'm going to create a XYItemRenderer proxy to wrap every renderer and track the last change to the CrosshairState.
Posts: 3
Joined: Sat Dec 09, 2006 1:41 am

Return to JFreeChart

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 6 guests