Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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.

Any ideas/recommendations?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
253 views
Welcome To Ask or Share your Answers For Others

1 Answer

This issue could also happen if you have :

animated="true"

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"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...