【问题标题】:JfreeChart scatter plot click to popup informationJfreeChart散点图点击弹出信息
【发布时间】:2013-04-02 18:14:38
【问题描述】:

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

this is another post on how to display point information

【问题讨论】:

标签: jfreechart scatter-plot


【解决方案1】:

使用ChartMouseListener,如图here。您可以根据需要检查ChartEntity。如果您在 ChartFactory 中启用 URL 生成,getURLText() 可能会很方便。

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

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

您可以看到工厂如何将生成器添加到渲染器here。您可以指定备用constructor parameters,也可以覆盖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 提供对父 XYDataset 的完全访问权限,尽管您可能必须将其强制转换为您的子类以检索特定数据。

【讨论】:

  • 我有获取 ChartEntity 的代码,但是如何从它中提取 GUID?
  • ChartEntity 可以访问您的XYURLGenerator 留下的实体的 URL 文本。
  • 我看到这个演示做了类似ww.nothingbutclouds.com/websvn/… 的事情,但我仍然无法弄清楚如何提取工具提示或 URL 文本......如果你能提供一段代码 - 那将是很棒
  • 请编辑您的问题以包含一个sscce 以显示您的ChartMouseListener XYURLGenerator
  • 编辑添加sscce,有系统出来打印URL文本,
猜你喜欢
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多