JFreeChart: Displaying data on a scatter plot on mouse click

只愿长相守 提交于 2020-01-16 16:12:05

问题


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


回答1:


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!



来源:https://stackoverflow.com/questions/3909636/jfreechart-displaying-data-on-a-scatter-plot-on-mouse-click

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!