Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8125209

Charts - duplicate children added when sorting the chart data

    XMLWordPrintable

Details

    Description

      I want to sort the values of a XYChart based on the X value, because the data comes unsorted.

      I get:

      Caused by: java.lang.IllegalArgumentException: Children: duplicate children added: parent = Group@158fd70[styleClass=plot-content]
      at javafx.scene.Parent$1.onProposedChange(Parent.java:307)
      at com.sun.javafx.collections.VetoableObservableList.add(VetoableObservableList.java:165)
      at com.sun.javafx.collections.ObservableListWrapper.add(ObservableListWrapper.java:144)
      at javafx.scene.chart.LineChart.dataItemAdded(LineChart.java:221)
      at javafx.scene.chart.XYChart.dataItemsChanged(XYChart.java:474)
      at javafx.scene.chart.XYChart.access$2500(XYChart.java:72)
      at javafx.scene.chart.XYChart$Series$1.onChanged(XYChart.java:1462)
      at com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:134)
      at com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:48)
      at com.sun.javafx.collections.ObservableListWrapper.callObservers(ObservableListWrapper.java:97)
      at com.sun.javafx.collections.ObservableListWrapper.sort(ObservableListWrapper.java:403)
      at javafx.collections.FXCollections.sort(FXCollections.java:491)


      See this example:


      import javafx.application.Application;
      import javafx.collections.FXCollections;
      import javafx.collections.ObservableList;
      import javafx.scene.Scene;
      import javafx.scene.chart.LineChart;
      import javafx.scene.chart.NumberAxis;
      import javafx.scene.chart.XYChart;
      import javafx.stage.Stage;

      import java.util.Comparator;

      public class TestApp3 extends Application {


          @Override
          public void start(Stage stage) throws Exception {

              NumberAxis xAxis = new NumberAxis();
              NumberAxis yAxis = new NumberAxis();

              LineChart<Number, Number> lineChart = new LineChart<Number, Number>(xAxis, yAxis);

              XYChart.Series<Number, Number> series = new XYChart.Series<Number, Number>();
              series.setData(list);

              lineChart.getData().add(series);

              FXCollections.sort(list, new Comparator<XYChart.Data<Number, Number>>() {
                  @Override
                  public int compare(XYChart.Data<Number, Number> o1, XYChart.Data<Number, Number> o2) {
                      return Integer.compare(o1.getXValue().intValue(), o2.getXValue().intValue());
                  }
              });

              Scene scene = new Scene(lineChart);
              stage.setScene(scene);
              stage.show();
          }

          private ObservableList<XYChart.Data<Number, Number>> list = FXCollections.observableArrayList(new XYChart.Data<Number, Number>(4, 1), new XYChart.Data<Number, Number>(1, 1), new XYChart.Data<Number, Number>(2, 2), new XYChart.Data<Number, Number>(3, 3));

          public static void main(String[] args) {
              launch(args);
          }
      }




      If I sort before I add the series, no error output is given at all, but the stage doesn't show up.


      Attachments

        Activity

          People

            psomashe Parvathi Somashekar (Inactive)
            cschudtjfx Christian Schudt (Inactive)
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:
              Imported: