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