ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-如何将多个LineCharts放入一个场景/舞台?

2019-10-28 06:02:54  阅读:226  来源: 互联网

标签:scene linechart javafx java


我正在寻找一种将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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有