如何将JavaFX Chart包含到现有GUI中?

时间:2019-06-22 15:05:37

标签: java swing user-interface javafx charts

我有两个类,第一个包含现有的GUI,第二个用于创建折线图。 现在我的问题是如何在不创建像现在这样的新窗口的情况下将创建的图表包含在现有GUI中? 图表创建方法:

public void start(Stage s) throws Exception {
    s.setTitle("JavaFX Realtime Chart Demo");

    //defining the axes
    final CategoryAxis xAxis = new CategoryAxis(); // we are gonna plot against time
    final NumberAxis yAxis = new NumberAxis(0,10,1);
    xAxis.setLabel("Time/s");
    xAxis.setAnimated(false); // axis animations are removed
    yAxis.setLabel("Value");
    yAxis.setAnimated(false); // axis animations are removed

    //creating the line chart with two axis created above
    final LineChart<String, Number> lineChart = new LineChart<>(xAxis, yAxis);
    lineChart.setTitle("Realtime JavaFX Charts");
    lineChart.setAnimated(true); // disable animations

    //defining a series to display data
    XYChart.Series<String, Number> series = new XYChart.Series<>();
    series.setName("U/s");
    XYChart.Series<String, Number> series2 = new XYChart.Series<>();
    series2.setName("km/h");
    // add series to chart
    lineChart.getData().addAll(series,series2);


    // setup scene
    Scene scene = new Scene(lineChart, 800, 600);
    s.setScene(scene);

    // show the stage        
    s.show();

    // this is used to display time in HH:mm:ss format
    final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss");

    // setup a scheduled executor to periodically put data into the chart
    scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();

    // put dummy data onto graph per second
    scheduledExecutorService.scheduleAtFixedRate(() -> {
        // get a random integer between 0-10



        wert = G.gebeInt();
        wert_2 = G.gebeV();
        // Update the chart
        Platform.runLater(() -> {
            // get current time
            Date now = new Date();
            // put random number with current time

            series.getData().add(new XYChart.Data<>(simpleDateFormat.format(now), wert));
            series2.getData().add(new XYChart.Data<>(simpleDateFormat.format(now), wert_2));

            if (series.getData().size() > WINDOW_SIZE) {
                series.getData().remove(0);
            }
            if (series2.getData().size() > WINDOW_SIZE) {
                series2.getData().remove(0);
            }
        });
    }, 0, 2, TimeUnit.SECONDS);

}

我将Swing用于GUI,并使用JavaFX创建图表,在本例中为LineChart。

0 个答案:

没有答案