-
Bug
-
Resolution: Unresolved
-
P3
-
8u45, 9
-
Windows 7, Java 8u45
I have a bar chart on a display and want to change the data, i. e. only the data without recreating the bar chart.
The problem is that the bar chart somehow still considers the old data, even if I use chart.setData(...). The problem seems to be a race condition. When you clear the chart, wait a bit and use setData(...) afterwards, it works as expected.
Example of the problem:
My categories in the bar chart are A, B, C, D. When I clear the chart and set new data with categories E, F, A, B, then the bar chart shows the data as A, B, E, F instead of E, F, A, B.
Here's a stackoverflow bug with more details:
http://stackoverflow.com/questions/30052685/recreate-bar-chart-without-it-remembering-data
Here's example code:
public class BarChartSample3 extends Application {
@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("Show 1st Series");
insertBeforeButton.setOnAction(e -> {
XYChart.Series firstSeries = new XYChart.Series();
firstSeries.getData().add(new XYChart.Data("A", 20));
firstSeries.getData().add(new XYChart.Data("B", 20));
firstSeries.getData().add(new XYChart.Data("Static", 100));
bc.getData().setAll(firstSeries);
});
// create new chart data where the data are inserted after the "static"
// bar
Button insertAfterButton = new Button("Show 2nd Series");
insertAfterButton.setOnAction(e -> {
XYChart.Series secondSeries = new XYChart.Series();
secondSeries.getData().add(new XYChart.Data("Static", 100));
secondSeries.getData().add(new XYChart.Data("A", 60));
secondSeries.getData().add(new XYChart.Data("B", 60));
bc.getData().setAll(secondSeries);
});
// 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 hit the "Show 1st Series" button, the chart gets filled with bars in order A,B,Static. If you hit the "Show 2nd Series" button, the order of the bars remains like it is in the 1st series, even thought the order in the data is Static,A,B.
The problem is that the bar chart somehow still considers the old data, even if I use chart.setData(...). The problem seems to be a race condition. When you clear the chart, wait a bit and use setData(...) afterwards, it works as expected.
Example of the problem:
My categories in the bar chart are A, B, C, D. When I clear the chart and set new data with categories E, F, A, B, then the bar chart shows the data as A, B, E, F instead of E, F, A, B.
Here's a stackoverflow bug with more details:
http://stackoverflow.com/questions/30052685/recreate-bar-chart-without-it-remembering-data
Here's example code:
public class BarChartSample3 extends Application {
@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("Show 1st Series");
insertBeforeButton.setOnAction(e -> {
XYChart.Series firstSeries = new XYChart.Series();
firstSeries.getData().add(new XYChart.Data("A", 20));
firstSeries.getData().add(new XYChart.Data("B", 20));
firstSeries.getData().add(new XYChart.Data("Static", 100));
bc.getData().setAll(firstSeries);
});
// create new chart data where the data are inserted after the "static"
// bar
Button insertAfterButton = new Button("Show 2nd Series");
insertAfterButton.setOnAction(e -> {
XYChart.Series secondSeries = new XYChart.Series();
secondSeries.getData().add(new XYChart.Data("Static", 100));
secondSeries.getData().add(new XYChart.Data("A", 60));
secondSeries.getData().add(new XYChart.Data("B", 60));
bc.getData().setAll(secondSeries);
});
// 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 hit the "Show 1st Series" button, the chart gets filled with bars in order A,B,Static. If you hit the "Show 2nd Series" button, the order of the bars remains like it is in the 1st series, even thought the order in the data is Static,A,B.
- relates to
-
JDK-8127602 XYChart changes categories order spontaneously on Remove Data Item
-
- Closed
-