Coordinates of mouse click in a ChartPanel
Coordinates of mouse click in a ChartPanel
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
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
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
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.
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


RectangleEdge.LEFT
Hi,
I tried to use RectangleEdge.LEFT but I get a depreciation warning, is there a more upto date method?
Thanks
PJ
I tried to use RectangleEdge.LEFT but I get a depreciation warning, is there a more upto date method?
Thanks
PJ
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
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).
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
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


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);
}
}
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);
}
}
-
- Posts: 13
- Joined: Sat Nov 29, 2003 5:38 am
-
- Posts: 15
- Joined: Thu Mar 27, 2003 10:06 am
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));
}
});