是否可以在散点图上为每个点添加GUID,以便当用户点击该点时我可以处理GUID并检索一些信息?
已编辑:添加样本:
package demo;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.util.Random;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartMouseEvent;
import org.jfree.chart.ChartMouseListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.entity.XYItemEntity;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class ScatterMouseClick extends JFrame {
private static final long serialVersionUID = 1L;
private static final int N = 24;
private static final Random rand = new Random(System.currentTimeMillis());
private final XYSeries series = new XYSeries("Scatter Series");
public ScatterMouseClick(String s) {
super(s);
final ChartPanel chartPanel = createDemoPanel();
this.add(chartPanel, BorderLayout.CENTER);
}
private ChartPanel createDemoPanel() {
JFreeChart jfreechart = ChartFactory.createScatterPlot(
"Scatter Mouse click Demo", "X", "Y", createSampleData(),
PlotOrientation.VERTICAL, true, true, false);
XYPlot xyPlot = (XYPlot) jfreechart.getPlot();
xyPlot.setDomainCrosshairVisible(true);
xyPlot.setRangeCrosshairVisible(true);
XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(0, Color.blue);
adjustAxis((NumberAxis) xyPlot.getDomainAxis(), true);
adjustAxis((NumberAxis) xyPlot.getRangeAxis(), false);
xyPlot.setBackgroundPaint(Color.white);
ChartPanel chartPanel = new ChartPanel(jfreechart);
chartPanel.addChartMouseListener(new ChartMouseListener() {
public void chartMouseClicked(ChartMouseEvent e) {
if (e.getEntity() instanceof XYItemEntity) {
XYItemEntity item = (XYItemEntity) e.getEntity();
System.out.println("You clicked at point of series:["
+ item.getSeriesIndex() + "] at location: ["
+ item.getItem() + "] with UTL Text: "
+ item.getURLText());
}
}
public void chartMouseMoved(ChartMouseEvent e) {
}
});
return chartPanel;
}
private void adjustAxis(NumberAxis axis, boolean vertical) {
axis.setRange(-3.0, 3.0);
axis.setTickUnit(new NumberTickUnit(0.5));
axis.setVerticalTickLabels(vertical);
}
private XYDataset createSampleData() {
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
for (int i = 0; i < N * N; i++) {
//TODO HOW ADD URL text information here
series.add(rand.nextGaussian(), rand.nextGaussian());
}
xySeriesCollection.addSeries(series);
return xySeriesCollection;
}
public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
ScatterMouseClick demo = new ScatterMouseClick("Scatter Mouse click Demo");
demo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
demo.pack();
demo.setLocationRelativeTo(null);
demo.setVisible(true);
}
});
}
}
答案 0 :(得分:1)
使用ChartMouseListener
,如图here所示。您可以根据需要检查ChartEntity
。如果您在ChartFactory
中启用了网址生成功能,getURLText()
可能很方便。
附录:使用您的示例生成网址的最简单修改是通过将最终参数ChartFactory
设置为urls
来在true
中启用它们:
JFreeChart jfreechart = ChartFactory.createScatterPlot(
"Scatter Mouse click Demo", "X", "Y", createSampleData(),
PlotOrientation.VERTICAL, true, true, true);
您可以看到工厂如何将生成器添加到渲染器here。您可以指定备用constructor parameters,也可以覆盖generateURL()
方法来自定义结果。
控制台:
You clicked at point of series:[0] at location: [0] with URL Text: index.html?series=0&item=0 You clicked at point of series:[0] at location: [1] with URL Text: index.html?series=0&item=1 You clicked at point of series:[0] at location: [2] with URL Text: index.html?series=0&item=2
附录:根据现已删除的评论,请注意getURLText()
仅检索生成器放置的内容。或者,请注意XYItemEntity
提供对父XYDataset
的完全访问权限,但您可能必须将其强制转换为子类以检索特定数据。