如何将数据从java程序传递到javafx应用程序图表?

问题描述:

我是JavaFX的新手,但我不是Java新手。我有一个庞大的复杂系统,可以在循环中产生一些结果。我想要实现的是在JavaFX图表上绘制每次迭代的结果。我这样做对java jFreeChart库没有任何问题,但现在我正在尝试切换到JavaFX。图表看起来更花哨,我喜欢风格的处理方式。无论如何,我正在努力了解如何在JavaFX应用程序中向XYChart.Series对象添加点。 oracle网站上的所有教程都以应用程序知道先验的一些固定点开始,并使用以下内容添加它们:

I am new to JavaFX but I am not new to Java. I have a big complex system that produces some results in a loop. What I am trying to achieve is to plot the results of each iteration on a JavaFX chart. I was doing this with no problem with the java jFreeChart libraries, but now I am trying to switch to JavaFX. Charts looks more fancy and I like the way style is handled. Anyway, I am struggling in trying to understand how to add points to a XYChart.Series object in a JavaFX application. All tutorials on the oracle website start with some fixed points that the application knows a-priori and they are added using something like:

`series.getData().add(new XYChart.Data(1, 23));`

但我想要实现的是有点不同。在我的情况下,我的应用程序产生一些结果,一旦它们生成(随机时间)我想在图表上绘制它们。
我用javafx.application.Application启动一个线程,但当我尝试向Series对象添加一些点时,我得到一个
java.lang.IllegalStateException:不在FX上申请线程; currentThread = main
例外。
将数据点传递给JavaFX图表的正确方法是什么?我认为最接近这种方法的方法是覆盖Event类型,Event对象并创建一个完整的事件处理结构......但对于我想要归档的简单事情,这看起来太复杂了!
您能否告诉我,您认为最好/最简单的方法是什么?

But what I am trying to achieve is a bit different. In my case my application produces some results and as soon as they are produced (random time) I want to plot them on a chart. I launch a thread with the javafx.application.Application, but when I try to add some points to the Series object I get a java.lang.IllegalStateException: Not on FX application thread; currentThread = main exception. What is the correct way to pass data points to a JavaFX chart? I thought that the closest way to do this is to override the Event type, Event object and create a whole Event handling structure... but this looks way too complicated for the simple thing I am trying to archive! Can you please tell me, in your opinion, what is the best/simplest way for doing this?

编辑:
这里有一些代码供您查看并给我一些建议:

Here is some code for you to have a look and give me some advice:

public class Chart extends Application {

    private final static XYChart.Series series = new XYChart.Series();

    public static void addValue(double gen, double val) {
        series.getData().add(new XYChart.Data(gen, val));
    }

    @Override
    public void start(Stage primaryStage) {

        primaryStage.setTitle("Chart");

        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();

        final LineChart<Number,Number> lineChart = 
                new LineChart<Number,Number>(xAxis,yAxis);


        //defining training set series
        series.setName("Training");


        Scene scene = new Scene(lineChart, 800, 600);
        lineChart.getData().add(series);
        primaryStage.setScene(scene);

        primaryStage.show();

    }

}


class Launcher extends Thread {

    @Override
    public void run() {
        Application.launch(Chart.class);
    }

    public static void main(String[] args) throws InterruptedException {
        new Launcher().start();
        System.out.println("Now doing something else...");

        for (int i = 0; i < 1000; i++) {
            double trainValue = Math.random();
            Chart.addValue(i, trainValue);
            Thread.sleep(500);
        }
    }

}


确保在JavaFX线程中执行向图表添加点的代码
您可以将其添加到在JavaFX线程上执行的Runnable-Object中:

to be sure that the code to add points to the chart is executed within the JavaFX thread you could add it into a Runnable-Object which gets executed on the JavaFX thread:

Platform.runLater(new Runnable() {
    @Override
    public void run() {
       // code to add points to the chart
    }
});

希望这会有所帮助。
Torsten

Hope this helps. Torsten