JavaFX重复系列已添加 [英] JavaFX Duplicate Series Added

查看:218
本文介绍了JavaFX重复系列已添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习JavaFX,我正在尝试创建一个显示折线图的应用程序,并允许用户更改某些变量,然后更改绘制的线条。我这样做的方法是删除系列(和系列中的数据点),然后重新填充系列并再次添加它们,如下所示。

I am currently learning JavaFX and am trying to create an app that shows a line chart and allows the user to change certain variables which then changes the plotted line. The way I do this is by removing the series (and the data points within the series) and then refilling the series and adding them again as shown below.

    public void plot(double[] xArr, double[] yExactArr, double[] yApproxArr) {
        linePlot.getData().clear();

        if (!exactValues.getData().isEmpty()) {
            exactValues.getData().remove(0, xArr.length - 1);
            approxValues.getData().remove(0, xArr.length - 1);
        }

        for (int i = 0; i < xArr.length; i++) {
            exactValues.getData().add(new XYChart.Data(xArr[i], yExactArr[i]));
            approxValues.getData().add(new XYChart.Data(xArr[i], yApproxArr[i]));
        }


        linePlot.getData().addAll(exactValues, approxValues);
        mainStage.show();
    }

然而,当我这样做时,我收到以下错误:

However, when I do this I am getting the following error:

    Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Duplicate series added

第二次调用 addAll()时就会发生这种情况。当我在调用 clear之后打印 toString()函数 linePlot.getData()时( ),它打印一个空数组,所以看起来应该没有问题。我的猜测是这不是改变线路的正确方法,但这是我的新手尝试。看起来我应该只能更改系列中的数据(不删除和读取它们)但是我的情节不会更新。

This occurs as soon as addAll() is called the second time around. When I print the toString() function of linePlot.getData() after calling clear(), it prints an empty array, so it seems like there shouldn't be a problem. My guess is this isn't the proper way of going about changing the line but this is my newbie attempt. It seems like I should be able to just change the data within the series (without removing and readding them) but then my plot doesn't update.

任何想法/建议?

推荐答案

如果您有以下问题,也可能发生此问题:

This issue could also happen if you have :

animated="true"

为折线图设置。



XYChart 类检查重复项时,它会从 displayedSeries 中获取所有系列,并将它们与您的系列进行比较现有数据系列。在此期间,如果您已清除数据,由于延长的淡入淡出效果,系列仍将位于显示的系列中,并导致重复系列添加错误。




。如果是这样的话,只需设置:

set for your line chart.

When the class XYChart checks for duplicates, it takes all the series from displayedSeries and compares them with your existing data series. During this time, if you have cleared the data, the series will still be in the displayedSeries due to the prolonged fading effect and result in the "duplicate series added" error.

If that is the case, simply set :

animated="false"

这篇关于JavaFX重复系列已添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆