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.candlestick; 33 34 import javafx.application.Application; 35 import javafx.collections.FXCollections; 36 import javafx.collections.ObservableList; 37 import javafx.scene.Parent; 38 import javafx.scene.Scene; 39 import javafx.scene.chart.NumberAxis; 40 import javafx.scene.chart.XYChart; 41 import javafx.stage.Stage; 42 43 /** 44 * A custom candlestick chart. This sample shows how to extend XYChart base class 45 * to create your own two axis chart type. 46 * 47 * @sampleName Candle Stick Chart 48 * @preview preview.png 49 * @see javafx.scene.chart.NumberAxis 50 * @see javafx.scene.chart.XYChart 51 * @docUrl http://docs.oracle.com/javafx/2/charts/jfxpub-charts.htm Using JavaFX Charts Tutorial 52 * @related /Charts/Scatter/Scatter Chart 53 * @related /Charts/Scatter/Advanced Scatter Chart 54 * @highlight 55 * @playground chart.data 56 * 57 * @playground - (name="xAxis") 58 * @playground xAxis.autoRanging 59 * @playground xAxis.forceZeroInRange 60 * @playground xAxis.lowerBound (min=-100,max=100,step=1) 61 * @playground xAxis.upperBound (step=1) 62 * @playground xAxis.tickUnit (step=0.5) 63 * @playground xAxis.minorTickCount (max=16) 64 * @playground xAxis.minorTickLength (max=15) 65 * @playground xAxis.minorTickVisible 66 * 67 * @playground xAxis.animated 68 * @playground xAxis.label 69 * @playground xAxis.side 70 * @playground xAxis.tickLabelFill 71 * @playground xAxis.tickLabelGap 72 * @playground xAxis.tickLabelRotation (min=-180,max=180,step=1) 73 * @playground xAxis.tickLabelsVisible 74 * @playground xAxis.tickLength 75 * @playground xAxis.tickMarkVisible 76 * 77 * @playground - (name="yAxis") 78 * @playground yAxis.autoRanging 79 * @playground yAxis.forceZeroInRange 80 * @playground yAxis.lowerBound (min=-100,max=30,step=1) 81 * @playground yAxis.upperBound (step=1) 82 * @playground yAxis.tickUnit (step=0.5) 83 * @playground yAxis.minorTickCount (max=16) 84 * @playground yAxis.minorTickLength (max=15) 85 * @playground yAxis.minorTickVisible 86 * 87 * @playground yAxis.animated 88 * @playground yAxis.label 89 * @playground yAxis.side 90 * @playground yAxis.tickLabelFill 91 * @playground yAxis.tickLabelGap 92 * @playground yAxis.tickLabelRotation (min=-180,max=180,step=1) 93 * @playground yAxis.tickLabelsVisible 94 * @playground yAxis.tickLength 95 * @playground yAxis.tickMarkVisible 96 * 97 * @playground - (name="chart") 98 * @playground chart.horizontalGridLinesVisible 99 * @playground chart.horizontalZeroLineVisible 100 * @playground chart.verticalGridLinesVisible 101 * @playground chart.verticalZeroLineVisible 102 * 103 * @playground chart.animated 104 * @playground chart.legendSide 105 * @playground chart.legendVisible 106 * @playground chart.title 107 * @playground chart.titleSide 108 */ 109 public class CandleStickChartApp extends Application { 110 111 // DAY, OPEN, CLOSE, HIGH, LOW, AVERAGE 112 private static double[][] data = new double[][]{ 113 {1, 25, 20, 32, 16, 20}, 114 {2, 26, 30, 33, 22, 25}, 115 {3, 30, 38, 40, 20, 32}, 116 {4, 24, 30, 34, 22, 30}, 117 {5, 26, 36, 40, 24, 32}, 118 {6, 28, 38, 45, 25, 34}, 119 {7, 36, 30, 44, 28, 39}, 120 {8, 30, 18, 36, 16, 31}, 121 {9, 40, 50, 52, 36, 41}, 122 {10, 30, 34, 38, 28, 36}, 123 {11, 24, 12, 30, 8, 32.4}, 124 {12, 28, 40, 46, 25, 31.6}, 125 {13, 28, 18, 36, 14, 32.6}, 126 {14, 38, 30, 40, 26, 30.6}, 127 {15, 28, 33, 40, 28, 30.6}, 128 {16, 25, 10, 32, 6, 30.1}, 129 {17, 26, 30, 42, 18, 27.3}, 130 {18, 20, 18, 30, 10, 21.9}, 131 {19, 20, 10, 30, 5, 21.9}, 132 {20, 26, 16, 32, 10, 17.9}, 133 {21, 38, 40, 44, 32, 18.9}, 134 {22, 26, 40, 41, 12, 18.9}, 135 {23, 30, 18, 34, 10, 18.9}, 136 {24, 12, 23, 26, 12, 18.2}, 137 {25, 30, 40, 45, 16, 18.9}, 138 {26, 25, 35, 38, 20, 21.4}, 139 {27, 24, 12, 30, 8, 19.6}, 140 {28, 23, 44, 46, 15, 22.2}, 141 {29, 28, 18, 30, 12, 23}, 142 {30, 28, 18, 30, 12, 23.2}, 143 {31, 28, 18, 30, 12, 22} 144 }; 145 146 private CandleStickChart chart; 147 private NumberAxis xAxis; 148 private NumberAxis yAxis; 149 150 public Parent createContent() { 151 xAxis = new NumberAxis(0,32,1); 152 xAxis.setMinorTickCount(0); 153 yAxis = new NumberAxis(); 154 chart = new CandleStickChart(xAxis,yAxis); 155 // setup chart 156 xAxis.setLabel("Day"); 157 yAxis.setLabel("Price"); 158 // add starting data 159 XYChart.Series<Number,Number> series = new XYChart.Series<Number,Number>(); 160 for (int i=0; i< data.length; i++) { 161 double[] day = data[i]; 162 series.getData().add( 163 new XYChart.Data<Number,Number>(day[0],day[1],new CandleStickExtraValues(day[2],day[3],day[4],day[5])) 164 ); 165 } 166 ObservableList<XYChart.Series<Number,Number>> data = chart.getData(); 167 if (data == null) { 168 data = FXCollections.observableArrayList(series); 169 chart.setData(data); 170 } else { 171 chart.getData().add(series); 172 } 173 return chart; 174 } 175 176 @Override public void start(Stage primaryStage) throws Exception { 177 primaryStage.setScene(new Scene(createContent())); 178 primaryStage.show(); 179 } 180 181 /** 182 * Java main for when running without JavaFX launcher 183 * @param args command line arguments 184 */ 185 public static void main(String[] args) { launch(args); } 186 }