2

是否可以将 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);
            }
        });
    }
}

这是关于如何显示点信息的另一篇文章

4

1 回答 1

1

使用 a ,如此ChartMouseListener所示。您可以根据需要检查。如果您在 中启用 URL 生成,可能会很方便。ChartEntityChartFactorygetURLText()

附录:使用您的示例生成 URL 的最简单修改是ChartFactory通过将最终参数 , 设置urls为来启用它们true

JFreeChart jfreechart = ChartFactory.createScatterPlot(
    "Scatter Mouse click Demo", "X", "Y", createSampleData(),
    PlotOrientation.VERTICAL, true, true, true);

您可以在此处查看工厂如何将生成器添加到渲染器。您可以指定备用构造函数参数,也可以覆盖该generateURL()方法以自定义结果。

安慰:

您点击了系列点:[0] 位置:[0]
    带有 URL 文本:index.html?series=0&item=0
您点击了系列点:[0],位置:[1]
    带有 URL 文本:index.html?series=0&item=1
您点击了系列点:[0] 在位置:[2]
    带有 URL 文本:index.html?series=0&item=2

附录:基于现在删除的评论,请注意getURLText()仅检索生成器放在那里的内容。或者,请注意它XYItemEntity提供了对 parent 的完全访问权限XYDataset,尽管您可能必须将其强制转换为您的子类以检索特定数据。

于 2013-04-02T21:51:15.857 回答