JFreeChart:单击鼠标即可在散点图上显示数据 [英] JFreeChart: Displaying data on a scatter plot on mouse click
问题描述
我想在我使用JFreeChart创建的散点图上显示给定点的x,y数据.我在网上以及开发人员指南中都进行了查找,但仍然很难做到这一点.
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.
我使用ChartFactory创建散点图
I create the scatter plot using ChartFactory
chart = ChartFactory.createScatterPlot( title, xlabel, ylabel, data, plotOrientation.VERTICAL,
false, true, false );
我试图实现chartMouseClicked事件.
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));
}
}
其中数据是与绘图相关的XYDataSet的实现.
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?
谢谢
推荐答案
啊,所有的红色是因为我没有检查它是否是XYItemEntity的实例.
Ah, all the red was because I was not checking to see if it was an instance of XYItemEntity.
扩展代码:
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));
}
}
现在看来可以了!
这篇关于JFreeChart:单击鼠标即可在散点图上显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!