In the example below you can see that, depending on the selected check box, program ploting series aSeries or bSeries . For the first time chart is ploted ok, but when I trying to plot chart again, it receives an exception java.lang.IllegalArgumentException: Duplicate series added. Before plot again I clean previous series.
I checked the solution on this page JavaFX Duplicate Series Added, I turned off the animations but it not help me.
Also check this: JavaFX Chart - Java.lang.IllegalArgumentException : Duplicate series added but i don't exactly know what is the difference i think i'm doing the same.
Example:
public class MainController implements Initializable {
XYChart.Series<String, Number> aSeries = new XYChart.Series<String, Number>();
XYChart.Series<String, Number> bSeries = new XYChart.Series<String, Number>();
@FXML
private LineChart<String, Number> lineChart;
@FXML
private CheckBox aCheckBox, bCheckBox;
@Override
public void initialize(URL location, ResourceBundle resources) {
lineChart.setAnimated(false);
aCheckBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
chartConfiguration();
}
});
bCheckBox.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
chartConfiguration();
}
});
}
@SuppressWarnings("unchecked")
private void chartConfiguration() {
if (!aCheckBox.isSelected() && aSeries.getData().size() > 0) {
aSeries.getData().clear();
}
if (!bCheckBox.isSelected() && bSeries.getData().size() > 0) {
bSeries.getData().clear();
}
if (aCheckBox.isSelected() && aSeries.getData().size() == 0) {
aSeries.getData().add(new XYChart.Data<String, Number>("Jan", 50.0));
aSeries.getData().add(new XYChart.Data<String, Number>("Feb", 70.0));
lineChart.getData().addAll(aSeries);
}
if (bCheckBox.isSelected() && bSeries.getData().size() == 0) {
bSeries.getData().add(new XYChart.Data<String, Number>("Jan", 30.0));
bSeries.getData().add(new XYChart.Data<String, Number>("Feb", 50.0));
lineChart.getData().addAll(bSeries);
}
}
}
My question is why program throws Duplicate series added and how can i fix this?
EDIT:
Like Sedrik suggest I modificate the code but after one plotting when i trying to plot next draw , nothing happened...
Change code below:
}
@SuppressWarnings("unchecked")
private void chartConfiguration() {
if (!aCheckBox.isSelected() && aSeries.getData().size() > 0) {
//aSeries.getData().clear();
lineChart.getData().clear();
}
if (!bCheckBox.isSelected() && bSeries.getData().size() > 0) {
//bSeries.getData().clear();
lineChart.getData().clear();
}
if (aCheckBox.isSelected() && aSeries.getData().size() == 0) {
aSeries.getData().add(new XYChart.Data<String, Number>("Jan", 50.0));
aSeries.getData().add(new XYChart.Data<String, Number>("Feb", 70.0));
lineChart.getData().addAll(aSeries);
}
if (bCheckBox.isSelected() && bSeries.getData().size() == 0) {
bSeries.getData().add(new XYChart.Data<String, Number>("Jan", 30.0));
bSeries.getData().add(new XYChart.Data<String, Number>("Feb", 50.0));
lineChart.getData().addAll(bSeries);
}
}