JavaFX Duplicate Series Added

后端 未结 4 1362
后悔当初
后悔当初 2020-12-07 03:11

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. T

相关标签:
4条回答
  • 2020-12-07 03:37

    I think it is a JavaFX bug. I had the same problem. I "solved" it removing the chart (linePlot in your case) and creating it again.

    0 讨论(0)
  • 2020-12-07 03:49

    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"
    
    0 讨论(0)
  • 2020-12-07 03:50

    Only add your series once to the chart initially when creating your chart.

    Update the data without adding the series again:

       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();
    }
    
    0 讨论(0)
  • 2020-12-07 03:55

    This seems to be a bug when changing the chart data with animation. As mentioned by @Anurag if you turn off the animation you will not face the issue.

    e.g.

    linePlot.setAnimated(false);
    
    0 讨论(0)
提交回复
热议问题