title: Recreate bar chart without it remembering data
body: <p><strong>Task</strong></p>
  
  <p>I have a BarChart which I have to fill with different data over and over again. The data must be in a predefined order. Of course I only want to change the data and not create a bar chart every time.</p>
  
  <p><strong>Problem</strong></p>
  
  <p>Once you have a category with a given name that already existed in earlier data, the order is wrong. </p>
  
  <p>I created an example. By clicking on the "Insert Before" button, a given number of bars is added. After that the "static" bar is added. </p>
  
  <p>The "Insert After" button adds the "static" bar first and then the other bars.</p>
  
  <pre><code>public class BarChartSample extends Application {
  
      int count = 1;
  
      @Override
      public void start(Stage stage) {
  
          final CategoryAxis xAxis = new CategoryAxis();
          final NumberAxis yAxis = new NumberAxis();
          final BarChart&lt;String, Number&gt; bc = new BarChart&lt;String, Number&gt;(xAxis, yAxis);
          bc.setAnimated(false);
  
          // create new chart data where the data are inserted before the "static" bar
          Button insertBeforeButton = new Button("Insert Before");
          insertBeforeButton.setOnAction(e -&gt; {
  
              XYChart.Series series1 = new XYChart.Series();
  
              for (int i = 0; i &lt; count; i++) {
                  series1.getData().add(new XYChart.Data("New " + i, 50));
              }
  
              series1.getData().add(new XYChart.Data("Static", 100));
  
              bc.getData().setAll(series1);
  
              count++;
  
          });
  
          // create new chart data where the data are inserted after the "static" bar
          Button insertAfterButton = new Button("Insert After");
          insertAfterButton.setOnAction(e -&gt; {
  
              XYChart.Series series1 = new XYChart.Series();
  
              series1.getData().add(new XYChart.Data("Static", 100));
  
              for (int i = 0; i &lt; count; i++) {
                  series1.getData().add(new XYChart.Data("New " + i, 50));
              }
  
              bc.getData().setAll(series1);
  
              count++;
  
          });
  
          // borderpane for chart and toolbar
          BorderPane bp = new BorderPane();
          bp.setCenter(bc);
  
          // toolbar
          HBox hb = new HBox();
          hb.getChildren().addAll(insertBeforeButton, insertAfterButton);
          bp.setTop(hb);
  
          Scene scene = new Scene(bp, 400, 200);
  
          stage.setScene(scene);
          stage.show();
      }
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>When you start the program and click on "Insert Before", you get this:</p>
  
  <p><img src="https://i.stack.imgur.com/d5Pdh.png" alt="enter image description here"></p>
  
  <p>When you restart the program and click on "Insert After", you get this:</p>
  
  <p><img src="https://i.stack.imgur.com/hmTPZ.png" alt="enter image description here"></p>
  
  <p>When you restart the program and click on "Insert Before" and then on "Insert After", you get this:</p>
  
  <p><img src="https://i.stack.imgur.com/Dtk3m.png" alt="enter image description here"></p>
  
  <p>which is wrong. It should be this:</p>
  
  <p><img src="https://i.stack.imgur.com/Hstxc.png" alt="enter image description here"></p>
  
  <p>Is there a way to clear the barchart's memory? Obviously setData isn't sufficient.</p>
  
  <p>I suspect it has something to do with the special removal methods in the bar chart and that the data weren't removed totally when the new data are added. There are some dubious methods like "seriesBeingRemovedIsAdded" in the source code of the BarChart class in JavaFX.</p>
  
  <p>Thank you very much for the help!</p>