How to identify elements in a Scatter Plot

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
elindauer
Posts: 7
Joined: Tue Sep 18, 2012 12:12 am
antibot: No, of course not.

How to identify elements in a Scatter Plot

Post by elindauer » Tue Sep 18, 2012 12:16 am

Hi!

Newbie here. I've created a ScatterPlot using a subclass of AbstractXYDataset. My chart is dynamic, and I am constantly adding and removing points in the graph.

I'm trying to write a method that is capable of taking an x,y point (as from a mouseMoved or mouseClicked event) and translate that into the nearest point in the graph. Is there a way to identify the x,y coordinates of the points in the graph? Any hints on making this thread-safe given that I am frequently removing data in another thread would be appreciated.

Thanks!

Eric

remiohead
Posts: 201
Joined: Fri Oct 02, 2009 3:53 pm
antibot: No, of course not.

Re: How to identify elements in a Scatter Plot

Post by remiohead » Tue Sep 18, 2012 12:48 pm

The methods java2DToValue() and valueToJava2D() in NumberAxis will translate the x,y values. So long as you update the charts and handle the mouse stuff on the EDT you won't have any threading issues.

elindauer
Posts: 7
Joined: Tue Sep 18, 2012 12:12 am
antibot: No, of course not.

Re: How to identify elements in a Scatter Plot

Post by elindauer » Tue Sep 18, 2012 4:02 pm

Thanks! Updating the chart on the EDT is a key point that I was missing. I'll give this a try and report back results!

elindauer
Posts: 7
Joined: Tue Sep 18, 2012 12:12 am
antibot: No, of course not.

Re: How to identify elements in a Scatter Plot

Post by elindauer » Tue Sep 18, 2012 5:36 pm

The documentation of java2DToValue is a bit confusing:

Code: Select all

public double java2DToValue(double java2DValue,
                   java.awt.geom.Rectangle2D area,
                   org.jfree.ui.RectangleEdge edge)
Converts a coordinate in Java2D space to the corresponding data value, assuming that the axis runs along one edge of the specified dataArea.
I can't figure out what this means. If I have a mouseMoved event with an (x,y), what arguments should I use for the "area" and "edge"? What do these arguments represent?

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

Re: How to identify elements in a Scatter Plot

Post by david.gilbert » Tue Sep 18, 2012 8:27 pm

When the chart is drawn on the screen, the rectangle defined by the axes is what I call the "dataArea". The JFreeChart object doesn't retain this info, so if you want to convert a screen coordinate into a data value, you'll need to know this rectangle...that's the area argument that you need to supply (you'll most likely retrieve it from the ChartRenderingInfo object that is populated at the chart drawing time.

The next thing is that the axis does not know the orientation of the plot (is the x-axis horizontal, which is the usual case, or vertical?)...but if you specify the edge of the rectangle along which the axis lies, then the method can tell if it needs to look at the x or y screen coordinate, and whether the axis is aligned horizontally or vertically...that's what the edge argument is for.
David Gilbert
JFreeChart Project Leader

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

elindauer
Posts: 7
Joined: Tue Sep 18, 2012 12:12 am
antibot: No, of course not.

Re: How to identify elements in a Scatter Plot

Post by elindauer » Tue Sep 18, 2012 10:28 pm

Thanks, that is very helpful, those arguments make sense.

Since it took me a while to figure it out, I'll say here for future readers that the method in question is ChartPanel.getChartRenderingInfo().getChartArea(). The edges are static constants like RectangleEdge.BOTTOM for the standard x-axis and RectangleEdge.LEFT for the standard y-axis.

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: How to identify elements in a Scatter Plot

Post by John Matthews » Thu Sep 20, 2012 2:54 pm

Cross-posted here.

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

Re: How to identify elements in a Scatter Plot

Post by david.gilbert » Fri Sep 21, 2012 8:05 pm

Hi John,

Thanks for your efforts over at stackoverflow.com (which is an awesome site and, thanks to you, a fantastic JFreeChart resource) and the cross-referencing you do here. It's definitely appreciated!
David Gilbert
JFreeChart Project Leader

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

John Matthews
Posts: 513
Joined: Wed Sep 12, 2007 3:18 pm

Re: How to identify elements in a Scatter Plot

Post by John Matthews » Fri Sep 21, 2012 9:52 pm

David,

Thank you for a great library! As you know, I'm a longtime fan, but there are still so many corners that I have yet to explore. Don't hesitate to contact me if you see anything that needs attention.

Cordially,

John

Locked