|
1 /* |
|
2 YUI 3.10.3 (build 2fb5187) |
|
3 Copyright 2013 Yahoo! Inc. All rights reserved. |
|
4 Licensed under the BSD License. |
|
5 http://yuilibrary.com/license/ |
|
6 */ |
|
7 |
|
8 YUI.add('series-candlestick', function (Y, NAME) { |
|
9 |
|
10 /** |
|
11 * Provides functionality for creating a candlestick series. |
|
12 * |
|
13 * @module charts |
|
14 * @submodule series-candlestick |
|
15 */ |
|
16 /** |
|
17 * The CandlestickSeries class renders columns (candles) and lines (wicks) representing the open, high, low and close |
|
18 * values for a chart. |
|
19 * |
|
20 * @class CandlestickSeries |
|
21 * @extends RangeSeries |
|
22 * @constructor |
|
23 * @param {Object} config (optional) Configuration parameters. |
|
24 * @submodule series-candlestick |
|
25 */ |
|
26 function CandlestickSeries() |
|
27 { |
|
28 CandlestickSeries.superclass.constructor.apply(this, arguments); |
|
29 } |
|
30 |
|
31 CandlestickSeries.NAME = "candlestickSeries"; |
|
32 |
|
33 CandlestickSeries.ATTRS = { |
|
34 /** |
|
35 * Read-only attribute indicating the type of series. |
|
36 * |
|
37 * @attribute type |
|
38 * @type String |
|
39 * @readOnly |
|
40 * @default candlestick |
|
41 */ |
|
42 type: { |
|
43 value: "candlestick" |
|
44 }, |
|
45 |
|
46 /** |
|
47 * The graphic in which drawings will be rendered. |
|
48 * |
|
49 * @attribute graphic |
|
50 * @type Graphic |
|
51 */ |
|
52 graphic: { |
|
53 lazyAdd: false, |
|
54 |
|
55 setter: function(val) { |
|
56 //woraround for Attribute order of operations bug |
|
57 if(!this.get("rendered")) { |
|
58 this.set("rendered", true); |
|
59 } |
|
60 this.set("upcandle", val.addShape({ |
|
61 type: "path" |
|
62 })); |
|
63 this.set("downcandle", val.addShape({ |
|
64 type: "path" |
|
65 })); |
|
66 this.set("wick", val.addShape({ |
|
67 type: "path" |
|
68 })); |
|
69 return val; |
|
70 } |
|
71 }, |
|
72 |
|
73 /** |
|
74 * Reference to the candlestick used when the close value is higher than the open value. |
|
75 * |
|
76 * @attribute upcandle |
|
77 * @type Path |
|
78 */ |
|
79 upcandle: {}, |
|
80 |
|
81 /** |
|
82 * Reference to the candlestick used when the open value is higher than the close value. |
|
83 * |
|
84 * @attribute downcandle |
|
85 * @type Path |
|
86 */ |
|
87 downcandle: {}, |
|
88 |
|
89 /** |
|
90 * Reference to the line drawn between the high and low values. |
|
91 * |
|
92 * @attribute wick |
|
93 * @type Path |
|
94 */ |
|
95 wick: {} |
|
96 |
|
97 /** |
|
98 * Style properties used for drawing candles and wicks. This attribute is inherited from `RangeSeries`. Below are the default values: |
|
99 * <dl> |
|
100 * <dt>upcandle</dt><dd>Properties for a candle representing a period that closes higher than it opens. |
|
101 * <dl> |
|
102 * <dt>fill</dt><dd>A hash containing the following values: |
|
103 * <dl> |
|
104 * <dt>color</dt><dd>Color of the fill. The default value is "#00aa00".</dd> |
|
105 * </dd> |
|
106 * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the marker fill. The default value is 1.</dd> |
|
107 * </dl> |
|
108 * </dd> |
|
109 * <dt>border</dt><dd>A hash containing the following values: |
|
110 * <dl> |
|
111 * <dt>color</dt><dd>Color of the border. The default value is "#000000".</dd> |
|
112 * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the marker border. The default value is 1.</dd> |
|
113 * <dt>weight</dt><dd>Number indicating the width of the border. The default value is 0.</dd> |
|
114 * </dl> |
|
115 * </dd> |
|
116 * </dl> |
|
117 * </dd> |
|
118 * <dt>downcandle</dt><dd>Properties for a candle representing a period that opens higher than it closes. |
|
119 * <dl> |
|
120 * <dt>fill</dt><dd>A hash containing the following values: |
|
121 * <dl> |
|
122 * <dt>color</dt><dd>Color of the fill. The default value is "#aa0000".</dd> |
|
123 * </dd> |
|
124 * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the marker fill. The default value is 1.</dd> |
|
125 * </dl> |
|
126 * </dd> |
|
127 * <dt>border</dt><dd>A hash containing the following values: |
|
128 * <dl> |
|
129 * <dt>color</dt><dd>Color of the border. The default value is "#000000".</dd> |
|
130 * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the marker border. The default value is 1.</dd> |
|
131 * <dt>weight</dt><dd>Number indicating the width of the border. The default value is 0.</dd> |
|
132 * </dl> |
|
133 * </dd> |
|
134 * </dl> |
|
135 * </dd> |
|
136 * <dt>wick</dt><dd>Properties for the wick, which is a line drawn from the high point of the period to the low point of the period. |
|
137 * <dl> |
|
138 * <dt>color</dt><dd>The color of the wick. The default value is "#000000".</dd> |
|
139 * <dt>weight</dt><dd>The weight of the wick. The default value is 1.</dd> |
|
140 * <dt>alpha</dt><dd>Number from 0 to 1 indicating the opacity of the wick. The default value is 1.</dd> |
|
141 * </dl> |
|
142 * </dd> |
|
143 * </dl> |
|
144 * |
|
145 * @attribute styles |
|
146 * @type Object |
|
147 */ |
|
148 }; |
|
149 |
|
150 Y.extend(CandlestickSeries, Y.RangeSeries, { |
|
151 /** |
|
152 * Draws markers for an Candlestick series. |
|
153 * |
|
154 * @method |
|
155 * @param {Array} xcoords The xcoordinates to be plotted. |
|
156 * @param {Array} opencoords The coordinates representing the open values. |
|
157 * @param {Array} highcoords The coordinates representing the high values. |
|
158 * @param {Array} lowcoords The coordinates representing the low values. |
|
159 * @param {Array} closecoords The coordinates representing the close values. |
|
160 * @param {Number} len The number of x coordinates to plot. |
|
161 * @param {Number} width The width of each candlestick marker. |
|
162 * @param {Number} halfwidth Half the width of each candlestick marker. |
|
163 * @param {Object} styles The styles for the series. |
|
164 * @private |
|
165 */ |
|
166 _drawMarkers: function(xcoords, opencoords, highcoords, lowcoords, closecoords, len, width, halfwidth, styles) |
|
167 { |
|
168 var upcandle = this.get("upcandle"), |
|
169 downcandle = this.get("downcandle"), |
|
170 wick = this.get("wick"), |
|
171 cx, |
|
172 opencoord, |
|
173 highcoord, |
|
174 lowcoord, |
|
175 closecoord, |
|
176 left, |
|
177 right, |
|
178 top, |
|
179 bottom, |
|
180 leftPadding = styles.padding.left, |
|
181 up; |
|
182 upcandle.set(styles.upcandle); |
|
183 downcandle.set(styles.downcandle); |
|
184 wick.set(styles.wick); |
|
185 upcandle.clear(); |
|
186 downcandle.clear(); |
|
187 wick.clear(); |
|
188 for(i = 0; i < len; i = i + 1) |
|
189 { |
|
190 cx = xcoords[i] + leftPadding; |
|
191 left = cx - halfwidth; |
|
192 right = cx + halfwidth; |
|
193 opencoord = opencoords[i]; |
|
194 highcoord = highcoords[i]; |
|
195 lowcoord = lowcoords[i]; |
|
196 closecoord = closecoords[i]; |
|
197 up = opencoord > closecoord; |
|
198 top = up ? closecoord : opencoord; |
|
199 bottom = up ? opencoord : closecoord; |
|
200 height = bottom - top; |
|
201 candle = up ? upcandle : downcandle; |
|
202 candle.drawRect(left, top, width, height); |
|
203 wick.moveTo(cx, highcoord); |
|
204 wick.lineTo(cx, lowcoord); |
|
205 } |
|
206 upcandle.end(); |
|
207 downcandle.end(); |
|
208 wick.end(); |
|
209 wick.toBack(); |
|
210 }, |
|
211 |
|
212 /** |
|
213 * Toggles visibility |
|
214 * |
|
215 * @method _toggleVisible |
|
216 * @param {Boolean} visible indicates visibilitye |
|
217 * @private |
|
218 */ |
|
219 _toggleVisible: function(visible) |
|
220 { |
|
221 this.get("upcandle").set("visible", visible); |
|
222 this.get("downcandle").set("visible", visible); |
|
223 this.get("wick").set("visible", visible); |
|
224 }, |
|
225 |
|
226 /** |
|
227 * Destructor implementation for the CartesianSeries class. Calls destroy on all Graphic instances. |
|
228 * |
|
229 * @method destructor |
|
230 * @protected |
|
231 */ |
|
232 destructor: function() |
|
233 { |
|
234 var upcandle = this.get("upcandle"), |
|
235 downcandle = this.get("downcandle"), |
|
236 wick = this.get("wick"); |
|
237 if(upcandle) |
|
238 { |
|
239 upcandle.destroy(); |
|
240 } |
|
241 if(downcandle) |
|
242 { |
|
243 downcandle.destroy(); |
|
244 } |
|
245 if(wick) |
|
246 { |
|
247 wick.destroy(); |
|
248 } |
|
249 }, |
|
250 |
|
251 /** |
|
252 * Gets the default value for the `styles` attribute. Overrides |
|
253 * base implementation. |
|
254 * |
|
255 * @method _getDefaultStyles |
|
256 * @return Object |
|
257 * @private |
|
258 */ |
|
259 _getDefaultStyles: function() |
|
260 { |
|
261 var styles = { |
|
262 upcandle: { |
|
263 fill: { |
|
264 color: "#00aa00", |
|
265 alpha: 1 |
|
266 }, |
|
267 stroke: { |
|
268 color: "#000000", |
|
269 alpha: 1, |
|
270 weight: 0 |
|
271 } |
|
272 }, |
|
273 downcandle: { |
|
274 fill: { |
|
275 color: "#aa0000", |
|
276 alpha: 1 |
|
277 }, |
|
278 stroke: { |
|
279 color: "#000000", |
|
280 alpha: 1, |
|
281 weight: 0 |
|
282 } |
|
283 }, |
|
284 wick: { |
|
285 stroke: { |
|
286 color: "#000000", |
|
287 alpha: 1, |
|
288 weight: 1 |
|
289 } |
|
290 } |
|
291 }; |
|
292 return this._mergeStyles(styles, CandlestickSeries.superclass._getDefaultStyles()); |
|
293 } |
|
294 }); |
|
295 Y.CandlestickSeries = CandlestickSeries; |
|
296 |
|
297 |
|
298 }, '3.10.3', {"requires": ["series-range"]}); |