java-如何将多个LineCharts放入一个场景/舞台?
作者:互联网
我正在寻找一种将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();
}
解决方法:
问题
一个阶段(窗口)中只有一个场景,因此您不能在同一阶段添加多个场景.但是您可以更改舞台的场景.
解
在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;
}
}
结果
标签:scene,linechart,javafx,java 来源: https://codeday.me/bug/20191028/1950286.html