JFreeChart - 如何将域十字线值转换为 TimeSeriesDataItem

发布于 2025-01-07 07:02:15 字数 218 浏览 3 评论 0原文

我有一个 XYPlot,显示 TimeSeriesCollection 中的多个 TimeSeries。我希望能够获取与域十字准线单击相对应的 TimeSeriesDataItems 列表。

看起来我可以添加一个 PlotChangeListener 来通知十字线值的变化,然后我可以调用 XYPlot.getDomainCrosshairValue(),但我不知道如何使用该十字线值来获取正确的时间序列项

I have an XYPlot displaying multiple TimeSeries in a TimeSeriesCollection. I'd like to be able to get the list of TimeSeriesDataItems that correspond to a domain crosshair click.

It looks like I can add a PlotChangeListener to be notified of a change in the crosshair value and I can then call XYPlot.getDomainCrosshairValue(), but I can't see how to then use that crosshair value to get the right time series items

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

复古式 2025-01-14 07:02:15

我最终设法弄清楚了。我本来就走在正确的轨道上;添加一个 PlotChangeListener 以接收更改通知,然后获取十字准线值。因为我的域轴是 DateAxis,所以作为十字线值返回的值实际上是十字线所在日期的毫秒值。由此,我可以构造一个 Day 实例,该实例可用于从 TimeSeriesCollection 中的每个 TimeSeries 检索 TimeSeriesDataItem。

plot.addChangeListener(new PlotChangeListener() {

    public void plotChanged(PlotChangeEvent event) {
        double crosshairXValue = this.plot.getDomainCrosshairValue();

        if (crosshairXValue == 0) {
            return;
        }

        Date date = new Date((long) crosshairXValue);
        Day day = new Day(date);

        for (Iterator<TimeSeries> itr = timeseriesCollection.getSeries().iterator(); itr.hasNext(); ) {

            TimeSeries timeSeries = itr.next();
            TimeSeriesDataItem dataItem = timeSeries.getDataItem(day);

            Number balance = dataItem.getValue();

            System.out.println("Balance for day " + day + " is " + balance);
            //TODO publish balances to listeners

        }
    }

});

然后,您还必须向 ChartPanel 添加 MouseClickListener,以便您可以将点击委托给图表。否则你的绘图更改监听器将永远不会被调用。

chartPanel.addChartMouseListener(new ChartMouseListener() {

        public void chartMouseMoved(ChartMouseEvent event) {
            // Do nothing
        }

        public void chartMouseClicked(ChartMouseEvent event) {
            int x = event.getTrigger().getX();
            int y = event.getTrigger().getY();
            chart.handleClick(x, y, chartPanel.getChartRenderingInfo());
        }

    });

I managed to figure it out eventually. I was on the right track originally; add a PlotChangeListener to be notified of a change and then get the crosshair value. Because my domain axis is a DateAxis the value returned as the crosshair value is actually the millisecond value of the date that the crosshair falls on. From that, I can construct a Day instance that can be used to retrieve the TimeSeriesDataItem from each TimeSeries in my TimeSeriesCollection.

plot.addChangeListener(new PlotChangeListener() {

    public void plotChanged(PlotChangeEvent event) {
        double crosshairXValue = this.plot.getDomainCrosshairValue();

        if (crosshairXValue == 0) {
            return;
        }

        Date date = new Date((long) crosshairXValue);
        Day day = new Day(date);

        for (Iterator<TimeSeries> itr = timeseriesCollection.getSeries().iterator(); itr.hasNext(); ) {

            TimeSeries timeSeries = itr.next();
            TimeSeriesDataItem dataItem = timeSeries.getDataItem(day);

            Number balance = dataItem.getValue();

            System.out.println("Balance for day " + day + " is " + balance);
            //TODO publish balances to listeners

        }
    }

});

Then you also have to add a MouseClickListener to the ChartPanel so that you can delegate clicks to the chart. Otherwise your plot change listener will never be invoked.

chartPanel.addChartMouseListener(new ChartMouseListener() {

        public void chartMouseMoved(ChartMouseEvent event) {
            // Do nothing
        }

        public void chartMouseClicked(ChartMouseEvent event) {
            int x = event.getTrigger().getX();
            int y = event.getTrigger().getY();
            chart.handleClick(x, y, chartPanel.getChartRenderingInfo());
        }

    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文