如何将多个折线图放入一个场景/舞台中?

3

我正在寻找将三个 LineCharts 放在一个窗口中的方法。 我的意思是我想让它们并排或者一个在另一个下面。

我一直在寻找方法,但是我找不到任何可行的方法。 我尝试搜索如何将多个场景放入一个舞台中... 如何将多个 LineCharts 放入一个场景中... 等等... 但都没有成功。

这是我的代码:

 private void drawGraph(Stage stage, Double[] axisValues) {
    //defining the axes
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Time");
    //creating the chart
    final LineChart<Number,Number> lineChart = 
            new LineChart<Number,Number>(xAxis,yAxis);

    lineChart.setTitle("Axis' values");
    //defining a series
    XYChart.Series series = new XYChart.Series();
    series.setName("X Axis");
    //populating the series with data
   for (int i = 1; i<33; i++){
       series.getData().add(new XYChart.Data(i, axisValues[i]));
    }

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

    stage.setScene(scene);
    stage.show();
 }

不要将它添加到场景中,而是将这三个图表添加到 FlowPane 中,然后再将 FlowPane 添加到场景中。 - aw-think
谢谢,随意回答问题。 - Ondrej Tokar
这里有一个使用GridPane的答案。http://stackoverflow.com/a/27350879/2855515 - brian
1个回答

6

问题

一个窗口(stage)只能包含一个场景(scene),所以您不能在同一个窗口中添加多个场景。但是您可以更改一个窗口的场景。

http://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm#CIHBIHHA

解决方案

在Scene Builder中,您可以在预览中查看可能的解决方案。将三个LineCharts添加到FlowPane中,然后将FlowPane添加到场景中。

Hierarchy

您的代码存在一些类型安全问题,因此我创建了一个完整示例来向您展示如何解决。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class FlowChart extends Application {

  @Override
  public void start(Stage primaryStage) {

    Double[] data = {0.1, 0.4, 0.5, 0.7, 0.9, 1.0};

    LineChart<Number, Number> lc = createLineChart(data);
    LineChart<Number, Number> lc1 = createLineChart(data);
    LineChart<Number, Number> lc2 = createLineChart(data);

    FlowPane root = new FlowPane();
    root.getChildren().addAll(lc, lc1, lc2);

    Scene scene = new Scene(root, 800, 600);

    primaryStage.setTitle("Hello World!");
    primaryStage.setScene(scene);
    primaryStage.show();
  }

  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) {
    launch(args);
  }

  private LineChart<Number, Number> createLineChart(Double[] axisValues) {
    //defining the axes
    final NumberAxis xAxis = new NumberAxis();
    final NumberAxis yAxis = new NumberAxis();
    xAxis.setLabel("Time");
    //creating the chart
    final LineChart<Number, Number> lineChart = new LineChart<>(xAxis, yAxis);

    lineChart.setTitle("Axis' values");
    //defining a series
    XYChart.Series<Number, Number> series = new LineChart.Series<>();
    series.setName("X Axis");
    //populating the series with data
    for (int i = 0; i < axisValues.length; i++) {
      XYChart.Data<Number, Number> data = new LineChart.Data<>(i, axisValues[i]);
      series.getData().add(data);
    }
    lineChart.getData().add(series);
    return lineChart;
  }
}

结果

结果


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接