How to identify elements in a Scatter Plot
How to identify elements in a Scatter Plot
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
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
Re: How to identify elements in a Scatter Plot
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.
Re: How to identify elements in a Scatter Plot
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!
Re: How to identify elements in a Scatter Plot
The documentation of java2DToValue is a bit confusing:
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?
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.
-
- 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
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.
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
Re: How to identify elements in a Scatter Plot
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.
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.
-
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm
Re: How to identify elements in a Scatter Plot
Cross-posted here.
-
- 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
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!
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
-
- Posts: 513
- Joined: Wed Sep 12, 2007 3:18 pm
Re: How to identify elements in a Scatter Plot
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
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