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.horizontal;
  33 
  34 
  35 import java.util.Arrays;
  36 import javafx.application.Application;
  37 import javafx.collections.FXCollections;
  38 import javafx.scene.Parent;
  39 import javafx.scene.Scene;
  40 import javafx.scene.chart.BarChart;
  41 import javafx.scene.chart.CategoryAxis;
  42 import javafx.scene.chart.NumberAxis;
  43 import javafx.scene.chart.XYChart;
  44 import javafx.stage.Stage;
  45 
  46 
  47 /**
  48  * Horizontal bar chart with a variety of actions and settable properties for
  49  * experimenting with the charts features.
  50  *
  51  * @sampleName Horizontal Bar Chart
  52  * @preview preview.png
  53  * @see javafx.scene.chart.BarChart
  54  * @see javafx.scene.chart.CategoryAxis
  55  * @see javafx.scene.chart.NumberAxis
  56  * @playground chart.data
  57  * @playground - (name="xAxis")
  58  * @playground xAxis.autoRanging
  59  * @playground xAxis.forceZeroInRange
  60  * @playground xAxis.lowerBound (min=-1000,max=3000,step=1)
  61  * @playground xAxis.upperBound (max=4000,step=1)
  62  * @playground xAxis.tickUnit (max=3000,step=1)
  63  * @playground xAxis.minorTickCount (max=16)
  64  * @playground xAxis.minorTickLength (max=15)
  65  * @playground xAxis.minorTickVisible
  66  * @playground xAxis.animated
  67  * @playground xAxis.label
  68  * @playground xAxis.side
  69  * @playground xAxis.tickLabelFill
  70  * @playground xAxis.tickLabelGap
  71  * @playground xAxis.tickLabelRotation (min=-180,max=180,step=1)
  72  * @playground xAxis.tickLabelsVisible
  73  * @playground xAxis.tickLength
  74  * @playground xAxis.tickMarkVisible
  75  * @playground - (name="yAxis")
  76  * @playground yAxis.autoRanging
  77  * @playground yAxis.gapStartAndEnd
  78  * @playground yAxis.startMargin
  79  * @playground yAxis.endMargin
  80  * @playground yAxis.animated
  81  * @playground yAxis.label
  82  * @playground yAxis.side
  83  * @playground yAxis.tickLabelFill
  84  * @playground yAxis.tickLabelGap
  85  * @playground yAxis.tickLabelRotation (min=-180,max=180,step=1)
  86  * @playground yAxis.tickLabelsVisible
  87  * @playground yAxis.tickLength
  88  * @playground yAxis.tickMarkVisible
  89  * @playground - (name="chart")
  90  * @playground chart.horizontalGridLinesVisible
  91  * @playground chart.horizontalZeroLineVisible
  92  * @playground chart.verticalGridLinesVisible
  93  * @playground chart.verticalZeroLineVisible
  94  * @playground chart.animated
  95  * @playground chart.legendSide
  96  * @playground chart.legendVisible
  97  * @playground chart.title
  98  * @playground chart.titleSide
  99  */
 100 public class HorizontalBarChartApp extends Application {
 101 
 102     private BarChart<Number, String> chart;
 103     private NumberAxis xAxis;
 104     private CategoryAxis yAxis;
 105 
 106     public Parent createContent() {
 107         final String[] years = {"2007", "2008", "2009"};
 108         xAxis = new NumberAxis();
 109         yAxis = new CategoryAxis();
 110         chart = new BarChart<>(xAxis, yAxis);
 111         chart.setTitle("Horizontal Bar Chart Example");
 112         yAxis.setLabel("Year");
 113         yAxis.setCategories(FXCollections.<String>observableArrayList(Arrays
 114                 .asList(years)));
 115         xAxis.setLabel("Price");
 116         
 117         // add starting data
 118         XYChart.Series<Number, String> series1 = new XYChart.Series<>();
 119         series1.setName("Data Series 1");
 120         series1.getData().addAll(
 121                 new XYChart.Data<Number, String>(567, years[0]),
 122                 new XYChart.Data<Number, String>(1292, years[1]),
 123                 new XYChart.Data<Number, String>(2180, years[2]));
 124         
 125         XYChart.Series<Number, String> series2 = new XYChart.Series<>();
 126         series2.setName("Data Series 2");
 127         series2.getData().addAll(
 128                 new XYChart.Data<Number, String>(956, years[0]),
 129                 new XYChart.Data<Number, String>(1665, years[1]),
 130                 new XYChart.Data<Number, String>(2450, years[2]));
 131         
 132         XYChart.Series<Number, String> series3 = new XYChart.Series<>();
 133         series3.setName("Data Series 3");
 134         series3.getData().addAll(
 135                 new XYChart.Data<Number, String>(800, years[0]),
 136                 new XYChart.Data<Number, String>(1000, years[1]),
 137                 new XYChart.Data<Number, String>(2800, years[2]));
 138         
 139         chart.getData().add(series1);
 140         chart.getData().add(series2);
 141         chart.getData().add(series3);
 142         return chart;
 143     }
 144 
 145     @Override
 146     public void start(Stage primaryStage) throws Exception {
 147         primaryStage.setScene(new Scene(createContent()));
 148         primaryStage.show();
 149     }
 150 
 151     /**
 152      * Java main for when running without JavaFX launcher
 153      * @param args command line arguments
 154      */
 155     public static void main(String[] args) {
 156         launch(args);
 157     }
 158 }