1 /*
   2  * Copyright (c) 2008, 2014, Oracle and/or its affiliates.
   3  * All rights reserved. Use is subject to license terms.
   4  *
   5  * This file is available and licensed under the following license:
   6  *
   7  * Redistribution and use in source and binary forms, with or without
   8  * modification, are permitted provided that the following conditions
   9  * are met:
  10  *
  11  *  - Redistributions of source code must retain the above copyright
  12  *    notice, this list of conditions and the following disclaimer.
  13  *  - Redistributions in binary form must reproduce the above copyright
  14  *    notice, this list of conditions and the following disclaimer in
  15  *    the documentation and/or other materials provided with the distribution.
  16  *  - Neither the name of Oracle Corporation nor the names of its
  17  *    contributors may be used to endorse or promote products derived
  18  *    from this software without specific prior written permission.
  19  *
  20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31  */
  32 package ensemble.samples.charts.bar.stacked;
  33 
  34 
  35 import javafx.application.Application;
  36 import static javafx.collections.FXCollections.*;
  37 import javafx.collections.ObservableList;
  38 import javafx.scene.Parent;
  39 import javafx.scene.Scene;
  40 import javafx.scene.chart.CategoryAxis;
  41 import javafx.scene.chart.NumberAxis;
  42 import javafx.scene.chart.StackedBarChart;
  43 import javafx.stage.Stage;
  44 
  45 
  46 /**
  47  * A sample that displays data in a stacked bar chart.
  48  *
  49  * @sampleName Stacked Bar Chart
  50  * @preview preview.png
  51  * @see javafx.scene.chart.StackedBarChart
  52  * @see javafx.scene.chart.CategoryAxis
  53  * @see javafx.scene.chart.NumberAxis
  54  * @related /Charts/Bar/Bar Chart
  55  * @playground chart.data
  56  * 
  57  * @playground - (name="xAxis")
  58  * @playground xAxis.autoRanging
  59  * @playground xAxis.gapStartAndEnd
  60  * @playground xAxis.startMargin
  61  * @playground xAxis.endMargin
  62  * 
  63  * @playground xAxis.animated
  64  * @playground xAxis.side
  65  * @playground xAxis.tickLabelFill
  66  * @playground xAxis.tickLabelGap
  67  * @playground xAxis.tickLabelRotation (min=-180,max=180,step=1)
  68  * @playground xAxis.tickLabelsVisible
  69  * @playground xAxis.tickLength
  70  * @playground xAxis.tickMarkVisible
  71  * 
  72  * @playground - (name="yAxis")
  73  * @playground yAxis.autoRanging
  74  * @playground yAxis.forceZeroInRange
  75  * @playground yAxis.lowerBound (min=-3000,max=0,step=1)
  76  * @playground yAxis.upperBound (min=0,max=8000,step=1)
  77  * @playground yAxis.tickUnit (max=5000,step=1)
  78  * @playground yAxis.minorTickCount (max=16)
  79  * @playground yAxis.minorTickLength (max=15)
  80  * @playground yAxis.minorTickVisible
  81  * 
  82  * @playground yAxis.animated
  83  * @playground yAxis.label
  84  * @playground yAxis.side
  85  * @playground yAxis.tickLabelFill
  86  * @playground yAxis.tickLabelGap
  87  * @playground yAxis.tickLabelRotation (min=-180,max=180,step=1)
  88  * @playground yAxis.tickLabelsVisible
  89  * @playground yAxis.tickLength
  90  * @playground yAxis.tickMarkVisible
  91  * 
  92  * @playground - (name="chart")
  93  * @playground chart.horizontalGridLinesVisible
  94  * @playground chart.horizontalZeroLineVisible
  95  * @playground chart.verticalGridLinesVisible
  96  * @playground chart.verticalZeroLineVisible
  97  * 
  98  * @playground chart.animated
  99  * @playground chart.legendSide
 100  * @playground chart.legendVisible
 101  * @playground chart.title
 102  * @playground chart.titleSide
 103  */
 104 public class StackedBarChartApp extends Application {
 105     
 106     private StackedBarChart chart;
 107     private CategoryAxis xAxis;
 108     private NumberAxis yAxis;
 109 
 110     public Parent createContent() {
 111         String[] years = {"2007", "2008", "2009"};
 112         xAxis = new CategoryAxis(observableArrayList(years));
 113         yAxis = new NumberAxis("Units Sold", 0.0d, 10000.0d, 1000.0d);
 114         
 115         ObservableList<StackedBarChart.Series> barChartData = 
 116                 observableArrayList(
 117                     new StackedBarChart.Series("Region 1", 
 118                         observableArrayList(
 119                             new StackedBarChart.Data(years[0], 567d),
 120                             new StackedBarChart.Data(years[1], 1292d),
 121                             new StackedBarChart.Data(years[2], 1292d)
 122                         )
 123                     ),
 124                     new StackedBarChart.Series("Region 2", 
 125                         observableArrayList(
 126                             new StackedBarChart.Data(years[0], 956),
 127                             new StackedBarChart.Data(years[1], 1665),
 128                             new StackedBarChart.Data(years[2], 2559)
 129                         )
 130                     ),
 131                     new StackedBarChart.Series("Region 3", 
 132                         observableArrayList(
 133                             new StackedBarChart.Data(years[0], 1154),
 134                             new StackedBarChart.Data(years[1], 1927),
 135                             new StackedBarChart.Data(years[2], 2774)
 136                         )
 137                     )
 138                 );
 139 
 140         chart = new StackedBarChart(xAxis, yAxis, barChartData, 25.0d);
 141         return chart;
 142     }
 143     
 144     
 145     @Override public void start(Stage primaryStage) throws Exception {
 146         primaryStage.setScene(new Scene(createContent()));
 147         primaryStage.show();
 148     }
 149 
 150     /**
 151      * Java main for when running without JavaFX launcher
 152      * @param args command line arguments
 153      */
 154     public static void main(String[] args) {
 155         launch(args);
 156     }    
 157 }