Recreate the histogram without storing data

Task

I have a BarChart, which I have to fill with different data again and again. Data must be in a predefined order. Of course, I only want to change the data and not create a chart every time.

Problem

Once you have a category with a given name that already existed in the earlier data, the order is wrong.

I created an example. By clicking the "Insert To" button, a certain number of bars is added. After that the "static" bar is added.

The Paste After button adds a “static” panel first, and then the rest of the columns.

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<String, Number> bc = new BarChart<String, Number>(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 -> { XYChart.Series series1 = new XYChart.Series(); for (int i = 0; i < 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 -> { XYChart.Series series1 = new XYChart.Series(); series1.getData().add(new XYChart.Data("Static", 100)); for (int i = 0; i < 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); } } 

When you run the program and click "Paste Until", you will get the following:

enter image description here

When you restart the program and click "Paste After", you will get the following:

enter image description here

When you restart the program and click "Paste Before" and then "Paste After", you will get the following:

enter image description here

which is wrong. It should be like this:

enter image description here

Is there a way to clear barcode memory? Obviously, setData is not enough.

I suspect that it has something to do with special deletion methods on the histogram and that the data was not completely deleted when adding new data. There are some dubious methods, such as "seriesBeingRemovedIsAdded" in the source code of the BarChart class in JavaFX.

Thank you for help!

0
source share
1 answer

Well ... calling layout() again seems to solve the problem

 bc.getData().clear(); bc.layout(); bc.getData().addAll( series1 ); 

in the second cycle, "Insert after."

+4
source

All Articles