我正在寻找一种方法将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();
}
答案 0 :(得分:4)
一个舞台(窗口)中只有一个场景,因此您无法在同一个舞台上添加多个场景。但是你可以改变舞台的场景。
在Scene Builder中,您可以在预览中看到可能的解决方案。将三个LineCharts添加到FlowPane,然后将FlowPane添加到场景中。
您的代码中存在一些类型安全问题,因此我创建了一个完整的示例来向您展示如何执行此操作。
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;
}
}