0
votes

I am trying to develop dynamic line chart in javafx and written this code. The code is working fine but the plotted data keeps populating the chart area and becomes a mess . what i want is to start shifting or moving data on left side of xAxis , but don't know how to do that . Also the code is generating Exception "Duplicate Series added". Whats wrong ?

 @FXML
LineChart<Number, Number> lineChart;

public void chartLine() {

    XYChart.Series<Number, Number> series = new XYChart.Series<>();
    Task<Void> task = new Task<Void>() {
        @Override protected Void call() throws Exception {
            final int[] j = {0};
            for (int i=0; i<10000; i++) {
                if (isCancelled()) break;


                Platform.runLater(new Runnable() {
                    @Override public void run() {
                        series.getData().add(new XYChart.Data(j[0]++, Math.random()));
                        lineChart.getData().add(series);
                    }
                });
                Thread.sleep(500);
            }
            return null;
        }
    };


    Thread th = new Thread(task);
    th.setDaemon(true);
    th.start();
}
1
it says duplicate series added ! but chart still works fine . - Rao Hammas
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Duplicate series added - Rao Hammas
@fabian any idea ? - Rao Hammas
The error says "duplicate series added": so don't repeatedly add the same series. If you want to control what's displayed, turn autoRange off on the x-axis, and set the range of the axis when you add new points. - James_D
@James_D ok i got that ...just one thing i want to know , is my code efficient or nearly efficient or bad ? i mean i want to confirm if i have used threading correctly ? because i saw people doing same thing what i have done using observable list and their code is more complex and lengthy than mine code ! thanks for the reply. - Rao Hammas

1 Answers

0
votes

i got it after spending hours on it . Using xAxis.setForceZeroInRange(false); and setting series.getData().remove(0); when series.getData().size()>20 .Also Exception is removed after using this final code .

@FXML
LineChart<Number, Number> lineChart;
@FXML
private NumberAxis xAxis;
@FXML
private NumberAxis yAxis;
public void chartLine() {

    XYChart.Series<Number, Number> series = new XYChart.Series<>();
    xAxis.setForceZeroInRange(false);
    lineChart.setLegendVisible(false);


    Task<Void> task = new Task<Void>() {
        @Override protected Void call() throws Exception {
            final int[] j = {0};
            for (int i=0; i<10000; i++) {
                if (isCancelled()) break;


                Platform.runLater(() -> {
                    series.getData().add(new XYChart.Data(j[0]++, Math.random()));
                    if (series.getData().size()>20) {
                        series.getData().remove(0);
                    }
                });
                Thread.sleep(2000);
            }
            return null;
        }
    };
    lineChart.getData().add(series);
    Thread th = new Thread(task);
    th.setDaemon(true);
    th.start();
}