I'm writing a program in which on button click data in a pie chart rotates (slice on 10-12 o'clock moves to 12-2 etc). Code below (kinda) works, it rotates, but eats the temp slice and creates whole paragraph of errors. It is my first time trying JavaFX and I'm not really sure how to manage that.
private BorderPane layout;
private Scene scene;
ObservableList<PieChart.Data> pieChartData =
FXCollections.observableArrayList(
new PieChart.Data("Post-production age", 424236),
new PieChart.Data("Production age", 1030060),
new PieChart.Data("Production age2", 1030060),
new PieChart.Data("Production age3", 1030060),
new PieChart.Data("Pre-production age", 310319));
PieChart chart = new PieChart(pieChartData);
@Override public void start(Stage stage) {
layout = new BorderPane();
scene = new Scene(layout,720,480);
stage.setTitle("People");
stage.setWidth(500);
stage.setHeight(500);
Button button = new Button();
button.setText("rotate");
layout.setBottom(button);
layout.setCenter(chart);
button.setOnAction(e -> {
rotate();
});
chart.setStartAngle(90);
chart.setTitle("Economical age groups");
stage.setScene(scene);
stage.show();
}
public void rotate(){
ObservableList<PieChart.Data> pieChartDataTemp = pieChartData;
int sizeOne = pieChartDataTemp.size();
PieChart.Data tempData = pieChartDataTemp.get(sizeOne-1);
pieChartDataTemp.add(0,tempData);
if(pieChartDataTemp.size()>sizeOne) pieChartDataTemp.remove(pieChartDataTemp.size()-1 );
PieChart chartTemp = new PieChart(pieChartDataTemp);
layout.setCenter(chartTemp);
chartTemp.setStartAngle(90);
}
Here's the stack trace:
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Children: duplicate children added: parent = Chart$1@5cfeee33[styleClass=chart-content]
at javafx.graphics/javafx.scene.Parent$3.onProposedChange(Parent.java:558)
at javafx.base/com.sun.javafx.collections.VetoableListDecorator.add(VetoableListDecorator.java:206)
at javafx.controls/javafx.scene.chart.PieChart.dataItemAdded(PieChart.java:417)
at javafx.controls/javafx.scene.chart.PieChart.lambda$new$0(PieChart.java:168)
at javafx.base/com.sun.javafx.collections.ListListenerHelper$SingleChange.fireValueChangedEvent(ListListenerHelper.java:164)
at javafx.base/com.sun.javafx.collections.ListListenerHelper.fireValueChangedEvent(ListListenerHelper.java:73)
at javafx.base/javafx.collections.ObservableListBase.fireChange(ObservableListBase.java:233)
at javafx.base/javafx.collections.ListChangeBuilder.commit(ListChangeBuilder.java:482)
at javafx.base/javafx.collections.ListChangeBuilder.endChange(ListChangeBuilder.java:541)
at javafx.base/javafx.collections.ObservableListBase.endChange(ObservableListBase.java:205)
at javafx.base/javafx.collections.ModifiableObservableListBase.add(ModifiableObservableListBase.java:155)
at task.Main.rotate(Main.java:54)
at task.Main.lambda$start$0(Main.java:39)
(...and so on)