Retrieve point under mouse click

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Cedric Moonen
Posts: 7
Joined: Wed Jul 03, 2013 8:53 am
antibot: No, of course not.

Retrieve point under mouse click

Post by Cedric Moonen » Wed Mar 19, 2014 4:43 pm

Hello,

Is there a way to know if a user clicked on a point of a series (XYPlot rendering one or more TimeSeries) ? This is not as "easy" as it sounds since there should be some tolerance as well (if the points are only one pixels, it's quite difficult to click on them).
I'm using the chart inside a ChartComposite (for SWT) so I can register a ChartMouseListener on it but from there, I have no idea to check whether the user clicked on a point (and I don't even know if this is possible).

Thanks,
Cédric

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Retrieve point under mouse click

Post by david.gilbert » Thu Mar 20, 2014 6:36 pm

If you use a ChartMouseListener, the ChartMouseEvent should have a reference to the ChartEntity that was under the mouse pointer. I don't know how well that works with the SWT ChartComposite, because it is a long time since I used SWT (that could possibly change in the coming months). And it varies a bit by renderer...if the points are small, then there isn't a big region for the "hotspot".
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

atmatap
Posts: 5
Joined: Fri Mar 21, 2014 10:05 am
antibot: No, of course not.

Re: Retrieve point under mouse click

Post by atmatap » Fri Mar 21, 2014 10:48 am

this is my code,

Code: Select all

ChartEntity ce = null;
ce = cp.getEntityForPoint(x, y);

if (ce instanceof XYItemEntity) {

			XYItemEntity e = (XYItemEntity) ce;
			XYDataset d = e.getDataset();
			int s = e.getSeriesIndex();
			int i = e.getItem();
			Number valueX = d.getX(s, i);
			Number valueY = d.getY(s, i);
}

Locked