在JavaFX中实时更新LineChart [英] Live-Update LineChart in JavaFX

查看:707
本文介绍了在JavaFX中实时更新LineChart的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有更新JavaFX UI的问题 - 我想在已经显示的情况下更新我的场景中的折线图和一些标签。
我的任务是做一些计算(在其他类中调用返回数据集的函数)并将更新的系列添加到图表中。

I have problem with updating JavaFX UI - I want to update line chart and some labels on my scene when it's already shown. My task is to do some calculations (calling function in other class which returns dataseries) and add updated series to the chart.

以下代码(这是在循环中)可能会呈现我想要做的事情:

The following code (which is in a loop) may present what i want to do:

//double x - x value of the point i want to add to my chart
//double y - y value of the point i want to add to my chart
//string s  - some result from function
mySeries.getData().add(new XYChart.Data(x, y));
someLabel.setText(s);

我的程序在一段时间后冻结并仅提供最终解决方案,但我希望看到图表完全在它们被添加之后,而不是在执行结束时。如果过程太快,我想在将下一个点添加到图表之前添加Thread.sleep(1000)。

My program freezes and give only final solution after some time, but i want to see the points on the chart exactly after they're added, not at the end of the execution. If the process is too quick, i would like to add Thread.sleep(1000) before adding the next point to the chart.

我知道它与某些事情有关线程,并发和任务,但我还没有找到解决方案。我尝试使用我在这里找到的一些代码,但我仍然不知道正确答案。

I know it has something to do with threads, concurrency and tasks, but i wasn't able to find a solution yet. I tried to use some code I found here but still I don't know the correct answer.

推荐答案

每个用户操作,例如单击一个按钮,将在UI线程内通知您的动作侦听器。 UI线程中的逻辑应该尽可能快。我认为您正在对用户事件做出反应,然后在UI线程中执行长时间运行的任务。尝试将您的代码放在后台线程中。此外,您需要将UI更新重新放回UI线程中。您可以使用Platform.runLater(...)执行此操作。

Every user action, e.g. click a button, will notify your action listener within the UI thread. Logic in the UI thread should be as fast as possible. I think you are reacting on a user event and then execute a long running task in the UI thread. Try to put your code in a background thread. Further you need to put the UI updates back again in the UI thread. You can do this with "Platform.runLater(...)".

这样的事情:

public class Test extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Scene scene = new Scene(createChart());
        primaryStage.setScene(scene);
        primaryStage.setHeight(800);
        primaryStage.setWidth(1200);
        primaryStage.show();
    }

    private Parent createChart() {
        LineChart<Number, Number> lc = new LineChart<>(new NumberAxis(), new NumberAxis());
        XYChart.Series<Number, Number> series = new XYChart.Series<>();
        lc.getData().add(series);

        new Thread(() -> {
            try {
                Thread.sleep(5000);
                for (int i = 0; i < 15; i++) {
                    int finalI = i;
                    Platform.runLater(() -> series.getData().add(new XYChart.Data<>(1 + finalI, 1 + finalI)));
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        return lc;
    }

}

这篇关于在JavaFX中实时更新LineChart的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆