To reproduce click button "Draw chart" several times.
You will see that series are rendered inconsistently (There is example attached).
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class BugBarChart extends Application {
static int SLOT_WIDTH = 600;
static int SLOT_HEIGHT = 400;
static int WND_WIDTH = 800;
static int WND_HEIGHT = 800;
BarChart barChart;
Button changeData;
Button reset;
List<XYChart.Data> data;
Pane buttonsPane;
Pane contentPane;
VBox root;
Button btnDrawChart;
Runnable action;
Runnable changeSeries;
@Override
public void start(Stage primaryStage) {
data = getData();
changeSeries = new Runnable() {
@Override
public void run() {
if (barChart == null) {
return;
}
int overall_counter = 0;
barChart.setAnimated(true);
end:
for (Iterator<Series> it = barChart.getData().iterator(); it.hasNext();) {
Series series = it.next();
int size = series.getData().size();
for (int i = 0; i < size / 2; i++) {
if (overall_counter >= data.size()) {
break end;
}
series.getData().set(i * 2 + 1, data.get(overall_counter++));
}
}
}
};
action = new Runnable() {
@Override
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {
contentPane.getChildren().clear();
barChart = getBarChart();
contentPane.getChildren().add(barChart);
Platform.runLater(changeSeries);
}
});
}
};
btnDrawChart = new Button("Draw chart");
btnDrawChart.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
action.run();
}
});
buttonsPane = new FlowPane();
buttonsPane.setMaxWidth(WND_WIDTH);
buttonsPane.setMinWidth(WND_WIDTH);
buttonsPane.getChildren().add(btnDrawChart);
contentPane = new Pane();
root = new VBox();
root.getChildren().addAll(buttonsPane, contentPane);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.setTitle(System.getProperty("java.runtime.version") + "; " + System.getProperty("javafx.runtime.version"));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private BarChart getBarChart() {
CategoryAxis x_axis = new CategoryAxis(FXCollections.<String>observableArrayList("First", "Second", "Third", "Fourth"));
NumberAxis y_axis = new NumberAxis("Y-Axis", 0.0d, 10.0d, 2.0d);
Series s1 = new Series(FXCollections.observableArrayList(
new XYChart.Data("First", 4),
new XYChart.Data("Second", 5),
new XYChart.Data("Fourth", 6)));
s1.setName("Set 1");
Series s2 = new Series(FXCollections.observableArrayList(
new XYChart.Data("First", 4),
new XYChart.Data("Second", 3),
new XYChart.Data("Fourth", 2)));
s2.setName("Set 2");
Series s3 = new Series(FXCollections.observableArrayList(
new XYChart.Data("First", 4),
new XYChart.Data("Second", 6),
new XYChart.Data("Fourth", 8)));
s3.setName("Set 3");
ObservableList chartData = FXCollections.observableArrayList(s1, s2, s3);
BarChart chart = new BarChart(x_axis, y_axis, chartData);
chart.setMaxSize(SLOT_WIDTH, SLOT_HEIGHT);
chart.setPrefSize(SLOT_WIDTH, SLOT_HEIGHT);
chart.setTitle("BarChart");
chart.setStyle("-fx-border-color: darkgray;");
return chart;
}
private List<XYChart.Data> getData() {
List<XYChart.Data> series_data = new ArrayList<>();
series_data.add(new XYChart.Data("Third", 5));
series_data.add(new XYChart.Data("Third", 8));
series_data.add(new XYChart.Data("Third", 2));
return series_data;
}
}
You will see that series are rendered inconsistently (There is example attached).
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class BugBarChart extends Application {
static int SLOT_WIDTH = 600;
static int SLOT_HEIGHT = 400;
static int WND_WIDTH = 800;
static int WND_HEIGHT = 800;
BarChart barChart;
Button changeData;
Button reset;
List<XYChart.Data> data;
Pane buttonsPane;
Pane contentPane;
VBox root;
Button btnDrawChart;
Runnable action;
Runnable changeSeries;
@Override
public void start(Stage primaryStage) {
data = getData();
changeSeries = new Runnable() {
@Override
public void run() {
if (barChart == null) {
return;
}
int overall_counter = 0;
barChart.setAnimated(true);
end:
for (Iterator<Series> it = barChart.getData().iterator(); it.hasNext();) {
Series series = it.next();
int size = series.getData().size();
for (int i = 0; i < size / 2; i++) {
if (overall_counter >= data.size()) {
break end;
}
series.getData().set(i * 2 + 1, data.get(overall_counter++));
}
}
}
};
action = new Runnable() {
@Override
public void run() {
Platform.runLater(new Runnable() {
@Override
public void run() {
contentPane.getChildren().clear();
barChart = getBarChart();
contentPane.getChildren().add(barChart);
Platform.runLater(changeSeries);
}
});
}
};
btnDrawChart = new Button("Draw chart");
btnDrawChart.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent t) {
action.run();
}
});
buttonsPane = new FlowPane();
buttonsPane.setMaxWidth(WND_WIDTH);
buttonsPane.setMinWidth(WND_WIDTH);
buttonsPane.getChildren().add(btnDrawChart);
contentPane = new Pane();
root = new VBox();
root.getChildren().addAll(buttonsPane, contentPane);
Scene scene = new Scene(root, 800, 600);
primaryStage.setScene(scene);
primaryStage.setTitle(System.getProperty("java.runtime.version") + "; " + System.getProperty("javafx.runtime.version"));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
private BarChart getBarChart() {
CategoryAxis x_axis = new CategoryAxis(FXCollections.<String>observableArrayList("First", "Second", "Third", "Fourth"));
NumberAxis y_axis = new NumberAxis("Y-Axis", 0.0d, 10.0d, 2.0d);
Series s1 = new Series(FXCollections.observableArrayList(
new XYChart.Data("First", 4),
new XYChart.Data("Second", 5),
new XYChart.Data("Fourth", 6)));
s1.setName("Set 1");
Series s2 = new Series(FXCollections.observableArrayList(
new XYChart.Data("First", 4),
new XYChart.Data("Second", 3),
new XYChart.Data("Fourth", 2)));
s2.setName("Set 2");
Series s3 = new Series(FXCollections.observableArrayList(
new XYChart.Data("First", 4),
new XYChart.Data("Second", 6),
new XYChart.Data("Fourth", 8)));
s3.setName("Set 3");
ObservableList chartData = FXCollections.observableArrayList(s1, s2, s3);
BarChart chart = new BarChart(x_axis, y_axis, chartData);
chart.setMaxSize(SLOT_WIDTH, SLOT_HEIGHT);
chart.setPrefSize(SLOT_WIDTH, SLOT_HEIGHT);
chart.setTitle("BarChart");
chart.setStyle("-fx-border-color: darkgray;");
return chart;
}
private List<XYChart.Data> getData() {
List<XYChart.Data> series_data = new ArrayList<>();
series_data.add(new XYChart.Data("Third", 5));
series_data.add(new XYChart.Data("Third", 8));
series_data.add(new XYChart.Data("Third", 2));
return series_data;
}
}