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();
}
autoRangeoff on the x-axis, and set the range of the axis when you add new points. - James_D