How to get point of the chart under the mouse?

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
NickKuz
Posts: 2
Joined: Thu Sep 03, 2009 8:49 pm
antibot: No, of course not.

How to get point of the chart under the mouse?

Post by NickKuz » Sun Jul 18, 2010 6:44 pm

I suppose that it is very simple question. I am sorry.
Chart - ScatterPlot

How to get point under the mouse.
For example:
X axis- from 0 to 1.
Y axis- from 10 to 20

If i click in the center of the chart, i must achieve point (0.5;15)
Is there any function that returns the real point of chart?
Or i must operate X and Y from the mouse event? In this case. How i can get the width of the Y axis legend? And the height of Title of chart?

Thank you.

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: How to get point of the chart under the mouse?

Post by skunk » Sun Jul 18, 2010 6:54 pm

In my code I use the following

Code: Select all

Rectangle2D dataArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
Point2D p = chartPanel.translateScreenToJava2D(pt);
double chartX = xyplot.getDomainAxis().java2DToValue(p.getX(), dataArea, xyplot.getDomainAxisEdge());
double chartY = xyplot.getRangeAxis().java2DToValue(p.getY(), dataArea, xyplot.getRangeAxisEdge());

vimal
Posts: 8
Joined: Sat Jul 24, 2010 3:38 pm
antibot: No, of course not.

Re: How to get point of the chart under the mouse?

Post by vimal » Sat Jul 24, 2010 4:06 pm

Hi skunk,

Sorry for the stupid question but what is 'pt' here? and how do we get it??

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: How to get point of the chart under the mouse?

Post by skunk » Sat Jul 24, 2010 5:14 pm

pt is an instance of java.awt.Point in screen coordinates. Call getPoint() on the MouseEvent object that is passed as an argument to all mouse event handlers.

vimal
Posts: 8
Joined: Sat Jul 24, 2010 3:38 pm
antibot: No, of course not.

Re: How to get point of the chart under the mouse?

Post by vimal » Sat Jul 24, 2010 6:25 pm

Hi Skunk,

Thanks a lot. You are great.

Sorry for asking so many question, but i have one more question here,
i m creating data like-
TimeSeries localTimeSeries1 = new TimeSeries("Series 1");
localTimeSeries1.add(new Month(1, 2002), 500.19999999999999D);

but, mouse click on plot giving me x and y both value in double ( as you suggested.)
now i want to add this point to timeseries as-

double chartX = xy.getDomainAxis().java2DToValue(p.getX(), dataArea, xy.getDomainAxisEdge());
double chartY = xy.getRangeAxis().java2DToValue(p.getY(), dataArea, xy.getRangeAxisEdge());

localTimeSeries1.add(chartX, chartY);

can you guide me how to convert chartX value in correct format like new Month(1,2002) does to add new point to series.

Thanking You.

warm regards,
Vimal

skunk
Posts: 1087
Joined: Thu Jun 02, 2005 10:14 pm
Location: Brisbane, Australia

Re: How to get point of the chart under the mouse?

Post by skunk » Sat Jul 24, 2010 7:07 pm

Code: Select all

Date date = new Date((long)chartX);
Then use the date variable to initialize an instance of java.util.Calendar and go mad...

vimal
Posts: 8
Joined: Sat Jul 24, 2010 3:38 pm
antibot: No, of course not.

Re: How to get point of the chart under the mouse?

Post by vimal » Sun Jul 25, 2010 7:56 am

Thanks a lot skunk !!

Locked