1

I'm using JFreeChart to draw XYPlot. What I'm currently trying to do:

  1. User clicks on the plot and the app gives him back the coordinates of the chosen point; I think that I've got it right. Here is some code:

        public void chartMouseClicked(ChartMouseEvent arg0) {
               Rectangle2D plotArea = chPanel.getScreenDataArea();
               XYPlot plot = (XYPlot) chart.getPlot();
               double chartX = plot.getDomainAxis().java2DToValue(arg0.getTrigger().getPoint().getX(), plotArea, plot.getDomainAxisEdge());
               double chartY = plot.getRangeAxis().java2DToValue(arg0.getTrigger().getPoint().getY(), plotArea, plot.getRangeAxisEdge());
               JOptionPane.showMessageDialog(null, "   x: "+chartX+ "  y: "+chartY);}       
    
  2. I need to determine if user clicked on the line or not. The target is to mark the clicked spot if it is on the drawn graph.

I would be grateful for any clue.

Carl Manaster
  • 39,912
  • 17
  • 102
  • 155
Jandrejc
  • 499
  • 3
  • 9
  • 18

2 Answers2

1

Instead of working with coordinates, add a ChartMouseListener as shown here. You can examine the ChartMouseEvent and any corresponding ChartEntity for a variety of details.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • As I understand this works for BarChart, selecting entire Bar. I need to select choosen point on XY Chart, so I'm not sure if I can use it. Correct me if i'm wrong. – Jandrejc Apr 14 '12 at 19:24
0
ShapeUtilities.intersects(java.awt.geom.Rectangle2D rect1, java.awt.geom.Rectangle2D rect2) 

where rect2 can be "0" rectangle (for your points).

Alex
  • 11,451
  • 6
  • 37
  • 52