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