Saturday, April 28, 2012

JavaFX 2.1: javafx.scene.chart.StackedAreaChart

JavaFX 2.1 introduce a new StackedAreaChart class. StackedAreaChart is a variation of AreaChart that displays trends of the contribution of each value. (over time e.g.) The areas are stacked so that each series adjoins but does not overlap the preceding series. This contrasts with the Area chart where each series overlays the preceding series. The cumulative nature of the StackedAreaChart gives an idea of the total Y data value at any given point along the X axis. Since data points across multiple series may not be common, StackedAreaChart interpolates values along the line joining the data points whenever necessary.


In the example, some column in series 2 are missed. From the graph, it can be noticed how JavaFX display the StackedAreaChart with missed column.

package javafx21;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.StackedAreaChart;
import javafx.scene.chart.XYChart;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class Charts extends Application {
    


    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        launch(args);
    }
    
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("java-buddy.blogspot.com");
        Group root = new Group();
         
        final NumberAxis xAxis = new NumberAxis();
        final NumberAxis yAxis = new NumberAxis();
        final StackedAreaChart<Number,Number> stackedAreaChart = new StackedAreaChart<>(xAxis,yAxis);
        final XYChart.Series<Number,Number> series1 = new XYChart.Series<>();
         
        xAxis.setLabel("Month");
        yAxis.setLabel("Value");

        stackedAreaChart.setTitle("StackedAreaChart");
        
        //Series 1

        series1.setName("XYChart.Series 1");
         
        series1.getData().add(new XYChart.Data(1, 100));
        series1.getData().add(new XYChart.Data(2, 200));
        series1.getData().add(new XYChart.Data(3, 50));
        series1.getData().add(new XYChart.Data(4, 75));
        series1.getData().add(new XYChart.Data(5, 110));
        series1.getData().add(new XYChart.Data(6, 300));
        series1.getData().add(new XYChart.Data(7, 111));
        series1.getData().add(new XYChart.Data(8, 30));
        series1.getData().add(new XYChart.Data(9, 75));
        series1.getData().add(new XYChart.Data(10, 55));
        series1.getData().add(new XYChart.Data(11, 225));
        series1.getData().add(new XYChart.Data(12, 99));

        //Series 2
        XYChart.Series<Number,Number> series2 = new XYChart.Series();
        series2.setName("XYChart.Series 2");
         
        series2.getData().add(new XYChart.Data(1, 50));
        series2.getData().add(new XYChart.Data(2, 200));
        series2.getData().add(new XYChart.Data(3, 260));
        series2.getData().add(new XYChart.Data(4, 100));
        //series2.getData().add(new XYChart.Data(5, 100));
        series2.getData().add(new XYChart.Data(6, 100));
        series2.getData().add(new XYChart.Data(7, 0));
        //series2.getData().add(new XYChart.Data(8, 0));
        series2.getData().add(new XYChart.Data(9, 0));
        series2.getData().add(new XYChart.Data(10, 100));
        //series2.getData().add(new XYChart.Data(11, 150));
        series2.getData().add(new XYChart.Data(12, 200));
        
        //
        
        stackedAreaChart.getData().addAll(series1, series2);
             
        root.getChildren().addAll(stackedAreaChart);
 
        primaryStage.setScene(new Scene(root, 500, 400));
        primaryStage.show();
    }
}


Related:
- Define the range and space of NumberAxis in JavaFX charts
- JavaFX 2.1: javafx.scene.chart.StackedBarChart


No comments:

Post a Comment