package de.cmsol.fx; /** * Copyright (c) 2008, 2012 Oracle and/or its affiliates. * All rights reserved. Use is subject to license terms. */ import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.stage.Stage; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.chart.*; import javafx.scene.layout.AnchorPane; /** * A sample that displays data in a stacked bar chart. * * @see javafx.scene.chart.StackedBarChart * @see javafx.scene.chart.CategoryAxisBuilder * @see javafx.scene.chart.NumberAxis * @related charts/bar/BarChart * */ public class StackedBarChartSample extends Application { @SuppressWarnings({ "rawtypes", "unchecked" }) private void init(Stage primaryStage) { AnchorPane anchor = new AnchorPane(); primaryStage.setScene(new Scene(anchor)); primaryStage.setWidth(436); String[] years = {"Year-2007", "Year-2008", "Year-2009", "Year-2010", "Year-2011"}; CategoryAxis xAxis = CategoryAxisBuilder.create() .categories(FXCollections.observableArrayList(years)).build(); NumberAxis yAxis = NumberAxisBuilder.create() .label("Units Sold") .lowerBound(0.0d) .upperBound(10000.0d) .tickUnit(1000.0d).build(); ObservableList barChartData = FXCollections.observableArrayList( new StackedBarChart.Series("Region 1", FXCollections.observableArrayList( new StackedBarChart.Data(years[0], 567d), new StackedBarChart.Data(years[1], 1292d), new StackedBarChart.Data(years[2], 1292d) )), new StackedBarChart.Series("Region 2", FXCollections.observableArrayList( new StackedBarChart.Data(years[0], 956), new StackedBarChart.Data(years[1], 1665), new StackedBarChart.Data(years[2], 2559) )), new StackedBarChart.Series("Region 3", FXCollections.observableArrayList( new StackedBarChart.Data(years[0], 1154), new StackedBarChart.Data(years[1], 1927), new StackedBarChart.Data(years[2], 2774) )) ); StackedBarChart chart = new StackedBarChart(xAxis, yAxis, barChartData, 25.0d); AnchorPane.setBottomAnchor(chart,0d); AnchorPane.setTopAnchor(chart,0d); AnchorPane.setLeftAnchor(chart,0d); AnchorPane.setRightAnchor(chart,0d); anchor.getChildren().add(chart); } @Override public void start(Stage primaryStage) throws Exception { init(primaryStage); primaryStage.show(); } public static void main(String[] args) { launch(args); } }