javafx折线图中左侧的Shift绘制数据

饶哈马斯

我正在尝试在 javafx 中开发动态折线图并编写此代码。代码运行良好,但绘制的数据不断填充图表区域并变得一团糟。我想要的是开始在 xAxis 的左侧移动或移动数据,但不知道该怎么做。此外,代码正在生成 Exception "Duplicate Series added"怎么了 ?

 @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();
}
饶哈马斯

我花了几个小时后才得到它。使用xAxis.setForceZeroInRange(false);和设置series.getData().remove(0);when series.getData().size()>20.Also Exception 在使用此最终代码后被删除。

@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();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章