views:

53

answers:

1

I would like to display the x,y data for a given point on a scatter plot that I have created using JFreeChart. I have looked online and in the developer's guide as well and am still having trouble doing this.

I create the scatter plot using ChartFactory

chart = ChartFactory.createScatterPlot( title, xlabel, ylabel, data, plotOrientation.VERTICAL,
    false, true, false );

I have tried to implement the chartMouseClicked event.

public void chartMouseClicked(ChartMouseEvent event) {

 ChartEntity entity = event.getEntity();

 If (entity != null) {
    XYItemEntity ent = (XYItemEntity) entity;

    int sindex = ent.getSeriesIndex();
    int iindex = ent.getItem();

    System.out.println("x = " + data.getXValue(sindex, iindex));
 }
}

where data is an implementation of the XYDataSet related to the plot.

This does not seem to give me any numbers. What am I doing wrong?

Thanks

A: 

Ah, all the red was because I was not checking to see if it was an instance of XYItemEntity.

Ammended code:

public void chartMouseClicked(ChartMouseEvent event) {

 ChartEntity entity = event.getEntity();

 If (entity != null && entity instanceof XYItemEntity) {
   XYItemEntity ent = (XYItemEntity) entity;

   int sindex = ent.getSeriesIndex();
   int iindex = ent.getItem();

   System.out.println("x = " + data.getXValue(sindex, iindex));
   System.out.println("y = " + data.getYValue(sindex, iindex));
  }
 }

This seems to work now!

Anu