How do I put multiple lines in one scene / scene?

I am looking for a way to fit 3 LineCharts

into one window. I mean, I want them to be next to each other or to each other.

I was looking for a way to do this, but I couldn't find anything. I tried to find how to put multiple scenes in one stage ... How to put multiple scenes in one LineCharts

scene ... etc ... Without success.

This is my code:

 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();
 }

      

+3


source to share


1 answer


Problem

There is only one scene at a time (window), so you cannot add multiple scenes to the same step. But you can change the scene of the scene.

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

Decision

In Scene Builder, you can see a possible solution in the preview. Add three LineCharts to the FlowPane and then add the FlowPane to the scene.



Hierarchy

There were some security issues in your code, so I created a whole example to show you how you could do this.

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;
  }
}

      

Result

Result

+4


source







All Articles