I just wanted to calculate the Plot value according to the mouse coordinates in a XYPlot. A task, which has been solved a lot of times when one googles for it. I also managed to calculate the right value, although there is a question left behind.
This is how I calculate the plot value (vertical x-axis), the code just prints the value to the console on a mouse click on the chart:
Code: Select all
chartPanel.addMouseListener( new MouseAdapter() {
@Override public void mouseClicked( MouseEvent e ) {
Point2D p = chartPanel.translateScreenToJava2D(e.getPoint());
// (*)
Rectangle2D plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
XYPlot plot = (XYPlot)chartPanel.getChart().getPlot();
double yValue = plot.getRangeAxis().java2DToValue(p.getY(), plotArea, plot.getRangeAxisEdge());
System.out.println("yValue: " + yValue);
}
} );
Code: Select all
// Rectangle2D plotArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
Rectangle2D plotArea = chartPanel.getScreenDataArea();
So my question is, does anyone know, why the "chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea()" works, while the use of "chartPanel.getScreenDataArea()" does not?
(If you miss any information about my plot/chart, please say so and I will post some more info.)
Thanks a lot for your help!
Best Regards,
Timo