0

I would like to catch click event over JFreeChart and show by console the x and y value of the nearest data, not just clicks within the boundary of a ChartEntity.

If it is not possible (y value is not exist for that x value), I would like to get the x value.

EDIT:

Marker shows so far to click. Click handler code:

Point2D po = chartPanel.translateScreenToJava2D(event.getTrigger().getPoint());
Rectangle2D plotArea = chartPanel.getScreenDataArea();
XYPlot plot = (XYPlot) chart.getPlot();
chartX = plot.getDomainAxis().java2DToValue(po.getX(), plotArea, plot.getDomainAxisEdge());
chartY = plot.getRangeAxis().java2DToValue(po.getY(), plotArea, plot.getRangeAxisEdge());
plot.removeDomainMarker(marker);
marker = new ValueMarker(chartX);
marker.setPaint(Color.red);
plot.addDomainMarker(marker);
System.out.println("X:" + chartX + ", Y:" + chartY);
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Amparo
  • 784
  • 1
  • 4
  • 21
  • If this is not a duplicate, please edit your question to include a [mcve] that shows your revised approach based on this [approach](http://stackoverflow.com/a/20107935/230513). – trashgod Dec 20 '16 at 16:02
  • @trashgod I dont want x,y coordinates but x,y values – Amparo Dec 20 '16 at 16:05
  • As shown [here](http://stackoverflow.com/a/20107935/230513): Get the dataset from the `XYItemEntity`; get the values from the dataset. – trashgod Dec 20 '16 at 16:44
  • @trashgod, thank you for your interest. The code you link only works when you clic on exact point. I need clic over any place and get the x value. – Amparo Dec 20 '16 at 17:13
  • Did you try enabling the `chartMouseMoved()` handler? – trashgod Dec 20 '16 at 23:45
  • @trashgod, what do you mean? I have implemented ChartMouseListener, so I have a chartMouseMoved method but it is empty. I only need get X value when I clic on the chart (chatMouseClicked method). – Amparo Dec 21 '16 at 08:20
  • I've never done it this way; a [mcve] might make it easier for someone else to answer. – trashgod Dec 21 '16 at 09:49
  • 1
    Reopened; possible duplicate of [*Dispay mouse coordinates near to mouse as hints (on mouse move)*](http://stackoverflow.com/q/21172794/230513). – trashgod Dec 21 '16 at 10:10

0 Answers0