开发者

JFreeChart: Displaying data on a scatter plot on mouse click

开发者 https://www.devze.com 2023-01-19 08:47 出处:网络
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开发者_C百科 the developer\'s guide as well and am still havin

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开发者_C百科 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


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消