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

查看:26
本文介绍了添加了 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() 后打印 linePlot.getData()toString() 函数时,它打印一个空数组,所以看起来好像应该没有问题.我的猜测是这不是改变线路的正确方法,但这是我的新手尝试.似乎我应该能够更改系列中的数据(无需删除和读取它们),但是我的情节不会更新.

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 中的所有系列,并将它们与您现有的数据系列进行比较.在此期间,如果您清除了数据,由于长时间的淡入淡出效果,该系列仍将在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天全站免登陆