How to put multiple LineCharts into one Scene/Stage?

4.1k views Asked by At

I am looking for a way to put 3 LineCharts into single window. I mean I want to have them next to each other, or one under another.

I was searching for the way to do it but I wasn't able to find anything. I tried to search how to put multiple scenes into one stage... How to put multiple LineCharts into one scene... etc... Without any 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();
 }
1

There are 1 answers

0
aw-think On BEST ANSWER

Problem

There are only one scene in one stage (window), so you are not able to add more than one scene to the same stage. But you are able to change the scene of a stage.

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

Solution

In Scene Builder you are able to see the possible solution in a preview. Add the three LineCharts to a FlowPane, and after that, add the FlowPane to the scene.

Hierarchy

There was some type safety problems in your code, so I created a whole example to show you how you could do it.

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