1
1
package com .zoicapital .stockchartsfx ;
2
2
/*
3
- Copyright 2014 Zoi Capital, LLC
3
+ Copyright 2014 Zoi Capital, LLC
4
4
5
- Licensed under the Apache License, Version 2.0 (the "License");
6
- you may not use this file except in compliance with the License.
7
- You may obtain a copy of the License at
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
8
9
- http://www.apache.org/licenses/LICENSE-2.0
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
10
11
- Unless required by applicable law or agreed to in writing, software
12
- distributed under the License is distributed on an "AS IS" BASIS,
13
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
- See the License for the specific language governing permissions and
15
- limitations under the License.
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
16
*/
17
+
17
18
import java .text .SimpleDateFormat ;
18
19
import java .util .ArrayList ;
19
20
import java .util .Iterator ;
38
39
import javafx .scene .shape .Path ;
39
40
import javafx .util .Duration ;
40
41
41
-
42
- /**
43
- * A candlestick chart is a style of bar-chart used primarily to describe
44
- * price movements of a security, derivative, or currency over time.
42
+ /**
43
+ * A candlestick chart is a style of bar-chart used primarily to describe price
44
+ * movements of a security, derivative, or currency over time.
45
45
*
46
- * The Data Y value is used for the opening price and then the close, high
47
- * and low values are stored in the Data's extra value property using a
46
+ * The Data Y value is used for the opening price and then the close, high and
47
+ * low values are stored in the Data's extra value property using a
48
48
* CandleStickExtraValues object.
49
+ *
50
+ *
49
51
*/
50
52
public class CandleStickChart extends XYChart <String , Number > {
51
53
@@ -54,31 +56,48 @@ public class CandleStickChart extends XYChart<String, Number> {
54
56
protected int maxBarsToDisplay ;
55
57
protected ObservableList <XYChart .Series <String , Number >> dataSeries ;
56
58
protected BarData lastBar ;
59
+ protected NumberAxis yAxis ;
60
+ protected CategoryAxis xAxis ;
57
61
62
+
63
+
64
+ /**
65
+ *
66
+ * @param title The chart title
67
+ * @param bars The bars data to display in the chart.
68
+ */
58
69
public CandleStickChart (String title , List <BarData > bars ) {
59
70
this (title , bars , Integer .MAX_VALUE );
60
71
}
61
72
73
+
74
+ /**
75
+ *
76
+ * @param title The chart title
77
+ * @param bars The bars to display in the chart
78
+ * @param maxBarsToDisplay The maximum number of bars to display in the chart.
79
+ */
62
80
public CandleStickChart (String title , List <BarData > bars , int maxBarsToDisplay ) {
63
81
this (title , new CategoryAxis (), new NumberAxis (), bars , maxBarsToDisplay );
64
82
}
65
83
66
- // -------------- CONSTRUCTORS ----------------------------------------------
67
84
/**
68
85
* Construct a new CandleStickChart with the given axis.
69
86
*
87
+ * @param title The chart title
70
88
* @param xAxis The x axis to use
71
89
* @param yAxis The y axis to use
72
- * @param bars
73
- * @param maxBarsToDisplay
90
+ * @param bars The bars to display on the chart
91
+ * @param maxBarsToDisplay The maximum number of bars to display on the chart.
74
92
*/
75
- public CandleStickChart (String title , Axis < String > xAxis , Axis < Number > yAxis , List <BarData > bars , int maxBarsToDisplay ) {
93
+ public CandleStickChart (String title , CategoryAxis xAxis , NumberAxis yAxis , List <BarData > bars , int maxBarsToDisplay ) {
76
94
super (xAxis , yAxis );
95
+ this .xAxis = xAxis ;
96
+ this .yAxis = yAxis ;
77
97
this .maxBarsToDisplay = maxBarsToDisplay ;
78
- //yAxis.setLabel("Yxis");
79
- NumberAxis y = (NumberAxis ) yAxis ;
98
+
80
99
yAxis .autoRangingProperty ().set (true );
81
- y .forceZeroInRangeProperty ().setValue (Boolean .FALSE );
100
+ yAxis .forceZeroInRangeProperty ().setValue (Boolean .FALSE );
82
101
setTitle (title );
83
102
setAnimated (true );
84
103
getStylesheets ().add (getClass ().getResource ("/styles/CandleStickChartStyles.css" ).toExternalForm ());
@@ -101,15 +120,20 @@ public CandleStickChart(String title, Axis<String> xAxis, Axis<Number> yAxis, Li
101
120
lastBar = sublist .get (sublist .size () - 1 );
102
121
}
103
122
104
- protected List < BarData > getSubList ( List < BarData > bars , int maxBars ) {
105
- List < BarData > sublist ;
106
- if ( bars . size () > maxBars ) {
107
- return bars . subList ( bars . size () - 1 - maxBars , bars . size () - 1 );
108
- } else {
109
- return bars ;
110
- }
123
+
124
+ /**
125
+ * Defines a formatter to use when formatting the y-axis values.
126
+ * @param formatter The formatter to use when formatting the y-axis values.
127
+ */
128
+ public void setYAxisFormatter ( DecimalAxisFormatter formatter ) {
129
+ yAxis . setTickLabelFormatter ( formatter );
111
130
}
112
131
132
+
133
+ /**
134
+ * Appends a new bar on to the end of the chart.
135
+ * @param bar The bar to append to the chart
136
+ */
113
137
public void addBar (BarData bar ) {
114
138
115
139
if (dataSeries .get (0 ).getData ().size () >= maxBarsToDisplay ) {
@@ -126,8 +150,16 @@ public void addBar(BarData bar) {
126
150
lastBar = new BarData (bar .getDateTime (), bar .getClose (), bar .getClose (), bar .getClose (), bar .getClose (), 0 );
127
151
Data <String , Number > data = new XYChart .Data <>(label , lastBar .getOpen (), lastBar );
128
152
dataSeries .get (0 ).getData ().add (data );
153
+
154
+
155
+
129
156
}
130
157
158
+
159
+ /**
160
+ * Update the "Last" price of the most recent bar
161
+ * @param price The Last price of the most recent bar.
162
+ */
131
163
public void updateLast (double price ) {
132
164
if (lastBar != null ) {
133
165
lastBar .update (price );
@@ -141,6 +173,17 @@ public void updateLast(double price) {
141
173
}
142
174
}
143
175
176
+
177
+
178
+ protected List <BarData > getSubList (List <BarData > bars , int maxBars ) {
179
+ List <BarData > sublist ;
180
+ if (bars .size () > maxBars ) {
181
+ return bars .subList (bars .size () - 1 - maxBars , bars .size () - 1 );
182
+ } else {
183
+ return bars ;
184
+ }
185
+ }
186
+
144
187
// -------------- METHODS ------------------------------------------------------------------------------------------
145
188
/**
146
189
* Called to update and layout the content for the plot
@@ -428,5 +471,3 @@ public void update(double open, double close, double high, double low) {
428
471
protected static CandleStickChart chart ;
429
472
430
473
}
431
-
432
-
0 commit comments