AreaChart from ObservableList> : AreaChart « JavaFX « Java






AreaChart from ObservableList>

 

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.chart.*;
import javafx.scene.chart.XYChart.Series;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class Main extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        CategoryAxis xAxis = new CategoryAxis();
        NumberAxis yAxis = new NumberAxis();
        AreaChart areaChart = new AreaChart(xAxis, yAxis);
        areaChart.setData(getChartData());
        areaChart.setTitle("speculations");
        primaryStage.setTitle("AreaChart example");

        StackPane root = new StackPane();
        root.getChildren().add(areaChart);
        primaryStage.setScene(new Scene(root, 400, 250));
        primaryStage.show();
    }

    private ObservableList<XYChart.Series<String, Double>> getChartData() {
        double aValue = 1.56;
        double bValue = 1.06;
        ObservableList<XYChart.Series<String, Double>> answer = FXCollections.observableArrayList();
        Series<String, Double> aSeries = new Series<String, Double>();
        Series<String, Double> bSeries = new Series<String, Double>();
        aSeries.setName("a");
        bSeries.setName("b");
        
        for (int i = 2011; i < 2021; i++) {
            aSeries.getData().add(new XYChart.Data(Integer.toString(i), aValue));
            aValue = aValue + Math.random() - .5;
            bSeries.getData().add(new XYChart.Data(Integer.toString(i), bValue));
            bValue = bValue + Math.random() - .5;
        }
        answer.addAll(aSeries, bSeries);
        return answer;
    }
}

   
  








Related examples in the same category

1.AreaChart Demo
2.AreaChart Demo
3.Areachart sample