Saturday, April 28, 2012

Define the range and space of NumberAxis in JavaFX charts

In the last post of StackedAreaChart, the month axis is displayed from 0~13. In order to specify the horizontal axis, we can use 3-parameters/4-parameters constructor of NumberAxis.

public NumberAxis(double lowerBound, double upperBound, double tickUnit)
Create a non-auto-ranging NumberAxis with the given upper bound, lower bound and tick unit.
Parameters:
  • lowerBound - The lower bound for this axis, ie min plottable value
  • upperBound - The upper bound for this axis, ie max plottable value
  • tickUnit - The tick unit, ie space between tickmarks

Example:

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();
        //Use 3 parameter constructor to define the min, max and space of the axis
        final NumberAxis xAxis = new NumberAxis(1, 12, 1);
        
        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();
    }
}


No comments:

Post a Comment