Coordinates of mouse click in a ChartPanel

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Jörg Marti

Coordinates of mouse click in a ChartPanel

Post by Jörg Marti » Fri Jan 30, 2004 7:53 pm

How can I get the coordinates of mouse click in a ChartPanel? I tryed with translateJava2DToValue(), but I coudn't find out the right paramters.

chartPanel.addChartMouseListener(new ChartMouseListener() {
public void chartMouseClicked(ChartMouseEvent e) {
int x = e.getTrigger().getX(); int y = e.getTrigger().getY();
double xValue = chartPanel.getChart().getXYPlot().getDomainAxis(). translateJava2DToValue((double)x, ??, ??);
}
});

Thanks for help !
Jörg

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

Post by david.gilbert » Mon Feb 02, 2004 3:19 pm

First, you need to adjust the mouse coordinates for the chart panel's insets, if any.

Second, you need the rectangle (in Java2D coordinates) enclosed by the axes, which I refer to as the 'data area'. Since the chart panel may be displaying a scaled version of the chart (scaled up or down to fit), the method to use is getScaledDataArea().

Finally, you need to know the location of the axis you are using to translate from Java2D to data values (usually RectangleEdge.BOTTOM for the domain axis and RectangleEdge.LEFT for the range axis, but your chart may be different).

With those three pieces of information, the translateJava2DToValue() method should return the value you are looking for.
David Gilbert
JFreeChart Project Leader

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

Guest

RectangleEdge.LEFT

Post by Guest » Mon Feb 02, 2004 4:48 pm

Hi,

I tried to use RectangleEdge.LEFT but I get a depreciation warning, is there a more upto date method?

Thanks

PJ

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

Post by david.gilbert » Mon Feb 02, 2004 4:53 pm

Perhaps you are calling translateJava2DtoValue() rather than translateJava2DToValue() - spot the difference? It is just the capitalisation, and I deprecated the first method for consistency with the naming of methods elsewhere.

Or, if you are using code from CVS, it could be that the translateJava2DToValue() method has been renamed java2DToValue(), just because I wanted a shorter method name (to avoid some very long lines in the source code).
David Gilbert
JFreeChart Project Leader

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

Guest

Post by Guest » Mon Feb 02, 2004 5:56 pm

It was the capitalisation,

Thanks for you reply.

Guest

Post by Guest » Wed Feb 18, 2004 5:29 pm

Hi again,

I have had some success with JFreeChart's ChartMouseListener, I can successfully get the "series" x,y position of a mouse click by using:

chartPanel.getScaledDataArea()
and the translateJava2DToValue method.

However I have discovered a problem when I zoom in on the x (time) axis, I get the wrong values.

It seems the dataArea does not update on a zoom on the Horizontal axes, where as it does with the vertical axes (although at the moment the vertical axis still gives the wrong position).

When I print out the Rectangle I get the following

Click with No Zoom
[x=57.09375,y=38.89999961853027,w=429.90625,h=161.85000038146973]

Click after Horizontal axis zoom
[x=57.09375,y=38.89999961853027,w=429.90625,h=161.85000038146973]

Click after Vertical axis zoom
[x=63.09375,y=38.89999961853027,w=423.90625,h=161.85000038146973]

I though it was a bit wierd that the rectangle did not change after a horizontal zoom,

Is the getScaledDataArea of chartPanel supposed to be used as a method to rescale mouse clicks, or do I have to do this some other way?

Many thanks

PJ


Here is the rest of my code.


{
Point point = chartMouseEvent.getTrigger().getPoint();
boolean controlDown = chartMouseEvent.getTrigger().isControlDown();
if(controlDown){
//add a new point to the model editor series
int x = point.x;
int y = point.y;
Rectangle2D dataArea = chartPanel.getScaledDataArea();


double y1 = timeChart.getXYPlot().getRangeAxis().translateJava2DToValue(y, dataArea, RectangleEdge.LEFT);
DateAxis dateAxis = (DateAxis)timeChart.getXYPlot().getDomainAxis();

double x1 = ((DateAxis)timeChart.getXYPlot().getDomainAxis()).translateJava2DToValue(x, dataArea, RectangleEdge.BOTTOM);

System.out.println("dataArea : " + dataArea);


long x2 = (long)x1;
Date sss = new Date(x2);
timeChartModel.insertNewEditableTimeDataPoint(new Date(x2),y1);
timeChartModel.setPointSelected(true);
}
else{
timeChartModel.setPointSelected(false);
}
}

garyn87048
Posts: 13
Joined: Sat Nov 29, 2003 5:38 am

Post by garyn87048 » Mon Feb 23, 2004 11:22 pm

Very cool! Can this technique be used with charts that are generated by a servlet?

wellber

Zoom with Servlet

Post by wellber » Wed Mar 17, 2004 3:18 pm

can somebody answer this question?

krheinwald
Posts: 15
Joined: Thu Mar 27, 2003 10:06 am

Post by krheinwald » Fri Mar 19, 2004 9:25 pm

This works for me:

Code: Select all

addMouseListener(new MouseAdapter() {            
    public void mousePressed(MouseEvent evt) {
        setLocation(xAxis.translateJava2DtoValue(evt.getX(), chartPanel.getScaledDataArea(), RectangleEdge.BOTTOM), 
                    yAxis.translateJava2DtoValue(evt.getY(), chartPanel.getScaledDataArea(), RectangleEdge.LEFT));
     }                                                                                            
});

Locked