2

I have an XYLineChart built with JFreeChart. I need, given that chart and a ChartMouseEvent, retrieve the X value of the displayde series closest to the point where the mouse has been clicked.

Thanks to a previous post I have been able to retrieve the offset of the grey chart (the coordinates of the green point in the image) and its dimension with the following method:

Rectangle2D greyChartArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();

I also know the max X values of the displayed serie:

double maxXValue = seriesCollection.getDomainUpperBound(true); //where seriesCollection is an XYSeriesCollection object

XYLineChart

Now the problem is that for converting a mouse coordinate (Point) into a corresponding value in the chart, I need to know at how many units (double) correspond a pixel on the screen. Unfortunately there is a gap between the maximum X value (60 in this case) and the grey chart width (look at the big blue line), so I can't achieve a perfect conversion.

Then I have two questions:

  1. How to calculate exactly the gap in pixel between the last displayed x value and the whole grey chart ? ( big blue line length)
  2. Am I doing something wrong? Is there any simpler way to achieve this goals, possibly avoiding all this calculus? I'm a JFreeChart newbie and the documentation of that library isn't enough, so maybe I'm missing some features that could help me.
Community
  • 1
  • 1
Heisenbug
  • 38,762
  • 28
  • 132
  • 190

3 Answers3

4

Recalling this example, you can obtain model coordinates from the cross-hair values in a ChartProgressListener. The cross-hairs don't have to be visible.

chartPanel.getChart().addProgressListener(new ChartProgressListener() {

    @Override
    public void chartProgress(ChartProgressEvent e) {
        XYPlot xyPlot = (XYPlot) chartPanel.getChart().getPlot();
        System.out.println(e.getType()
            + ": " + xyPlot.getDomainCrosshairValue()
            + ", " + xyPlot.getRangeCrosshairValue());
    }
});
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
2
  final XYPlot plot = getChart().getXYPlot();
  final ValueAxis domainAxis = plot.getDomainAxis();
  final ValueAxis rangeAxis = plot.getRangeAxis();
  final Rectangle2D plotRectangle = SWTUtils.toAwtRectangle(getScreenDataArea());
  final double chartX = domainAxis.java2DToValue(relativeX, plotRectangle, plot.getDomainAxisEdge());
  final double chartY = rangeAxis.java2DToValue(relativeY, plotRectangle, plot.getRangeAxisEdge());

We have used this to get the data coordinates from the mouse coordinates.

Kathir
  • 6,136
  • 8
  • 36
  • 67
1

Have a look at this JFreeChart get mouse coordinates. If you know the coordinate, you can take the x and y-coordinates from your plot, and get the corresponding values from the axises:

JFreeChart chart = yourChart;
Rectangle2D greyChartArea = chartPanel.getChartRenderingInfo().getPlotInfo().getDataArea();
XYPlot plot = (XYPlot) chart.getPlot();

double valueX = ((NumberAxis) plot.getRangeAxis()).java2DToValue(chartY,plot.getRangeAxisEdge();
double valueY = ((NumberAxis) plot.getDomainAxis()).java2DToValue(chartX,plot.getDomainAxisEdge();

That should do it.

Community
  • 1
  • 1
Jes
  • 2,748
  • 18
  • 22
  • +1 : thanks to you too. I set Kathir's answer as correct because you have inverted valueX with valueY. Nothing personal but I had to choose an answer. Thanks again, both of you have been very helpful! – Heisenbug Aug 26 '11 at 16:48
  • What version of JFreeChart are you running? – Jes Sep 30 '12 at 21:42