CandlestickSeries.js
9.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
* (c) 2010-2018 Torstein Honsi
*
* License: www.highcharts.com/license
*/
'use strict';
import H from './Globals.js';
import './Utilities.js';
var defaultPlotOptions = H.defaultPlotOptions,
each = H.each,
merge = H.merge,
seriesType = H.seriesType,
seriesTypes = H.seriesTypes;
/**
* A candlestick chart is a style of financial chart used to describe price
* movements over time.
*
* @sample stock/demo/candlestick/ Candlestick chart
*
* @extends plotOptions.ohlc
* @excluding borderColor,borderRadius,borderWidth
* @product highstock
* @optionparent plotOptions.candlestick
*/
var candlestickOptions = {
/**
* The specific line color for up candle sticks. The default is to inherit
* the general `lineColor` setting.
*
* @type {Color}
* @sample {highstock} stock/plotoptions/candlestick-linecolor/
* Candlestick line colors
* @default null
* @since 1.3.6
* @product highstock
* @apioption plotOptions.candlestick.upLineColor
*/
/**
* @default ohlc
* @apioption plotOptions.candlestick.dataGrouping.approximation
*/
states: {
/**
* @extends plotOptions.column.states.hover
* @product highstock
*/
hover: {
/**
* The pixel width of the line/border around the candlestick.
*
* @type {Number}
* @default 2
* @product highstock
*/
lineWidth: 2
}
},
/**
* @extends plotOptions.ohlc.tooltip
*/
tooltip: defaultPlotOptions.ohlc.tooltip,
threshold: null,
/**
* The color of the line/border of the candlestick.
*
* In styled mode, the line stroke can be set with the
* `.highcharts-candlestick-series .highcahrts-point` rule.
*
* @type {Color}
* @see [upLineColor](#plotOptions.candlestick.upLineColor)
* @sample {highstock} stock/plotoptions/candlestick-linecolor/
* Candlestick line colors
* @default #000000
* @product highstock
*/
lineColor: '#000000',
/**
* The pixel width of the candlestick line/border. Defaults to `1`.
*
*
* In styled mode, the line stroke width can be set with the
* `.highcharts-candlestick-series .highcahrts-point` rule.
*
* @type {Number}
* @default 1
* @product highstock
*/
lineWidth: 1,
/**
* The fill color of the candlestick when values are rising.
*
* In styled mode, the up color can be set with the
* `.highcharts-candlestick-series .highcharts-point-up` rule.
*
* @type {Color}
* @sample {highstock} stock/plotoptions/candlestick-color/ Custom colors
* @sample {highstock} highcharts/css/candlestick/ Colors in styled mode
* @default #ffffff
* @product highstock
*/
upColor: '#ffffff',
stickyTracking: true
};
/**
* The candlestick series type.
*
* @constructor seriesTypes.candlestick
* @augments seriesTypes.ohlc
*/
seriesType('candlestick', 'ohlc', merge(
defaultPlotOptions.column,
candlestickOptions
), /** @lends seriesTypes.candlestick */ {
/**
* Postprocess mapping between options and SVG attributes
*/
pointAttribs: function (point, state) {
var attribs = seriesTypes.column.prototype.pointAttribs.call(
this,
point,
state
),
options = this.options,
isUp = point.open < point.close,
stroke = options.lineColor || this.color,
stateOptions;
attribs['stroke-width'] = options.lineWidth;
attribs.fill = point.options.color ||
(isUp ? (options.upColor || this.color) : this.color);
attribs.stroke = point.lineColor ||
(isUp ? (options.upLineColor || stroke) : stroke);
// Select or hover states
if (state) {
stateOptions = options.states[state];
attribs.fill = stateOptions.color || attribs.fill;
attribs.stroke = stateOptions.lineColor || attribs.stroke;
attribs['stroke-width'] =
stateOptions.lineWidth || attribs['stroke-width'];
}
return attribs;
},
/**
* Draw the data points
*/
drawPoints: function () {
var series = this,
points = series.points,
chart = series.chart,
reversedYAxis = series.yAxis.reversed;
each(points, function (point) {
var graphic = point.graphic,
plotOpen,
plotClose,
topBox,
bottomBox,
hasTopWhisker,
hasBottomWhisker,
crispCorr,
crispX,
path,
halfWidth,
isNew = !graphic;
if (point.plotY !== undefined) {
if (!graphic) {
point.graphic = graphic = chart.renderer.path()
.add(series.group);
}
graphic
.attr(
series.pointAttribs(point, point.selected && 'select')
) // #3897
.shadow(series.options.shadow);
// Crisp vector coordinates
crispCorr = (graphic.strokeWidth() % 2) / 2;
crispX = Math.round(point.plotX) - crispCorr; // #2596
plotOpen = point.plotOpen;
plotClose = point.plotClose;
topBox = Math.min(plotOpen, plotClose);
bottomBox = Math.max(plotOpen, plotClose);
halfWidth = Math.round(point.shapeArgs.width / 2);
hasTopWhisker = reversedYAxis ?
bottomBox !== point.yBottom :
Math.round(topBox) !== Math.round(point.plotHigh);
hasBottomWhisker = reversedYAxis ?
Math.round(topBox) !== Math.round(point.plotHigh) :
bottomBox !== point.yBottom;
topBox = Math.round(topBox) + crispCorr;
bottomBox = Math.round(bottomBox) + crispCorr;
// Create the path. Due to a bug in Chrome 49, the path is first
// instanciated with no values, then the values pushed. For
// unknown reasons, instanciating the path array with all the
// values would lead to a crash when updating frequently
// (#5193).
path = [];
path.push(
'M',
crispX - halfWidth, bottomBox,
'L',
crispX - halfWidth, topBox,
'L',
crispX + halfWidth, topBox,
'L',
crispX + halfWidth, bottomBox,
'Z', // Ensure a nice rectangle #2602
'M',
crispX, topBox,
'L',
// #460, #2094
crispX, hasTopWhisker ?
Math.round(
reversedYAxis ? point.yBottom : point.plotHigh
) :
topBox,
'M',
crispX, bottomBox,
'L',
// #460, #2094
crispX, hasBottomWhisker ?
Math.round(
reversedYAxis ? point.plotHigh : point.yBottom
) :
bottomBox
);
graphic[isNew ? 'attr' : 'animate']({ d: path })
.addClass(point.getClassName(), true);
}
});
}
});
/**
* A `candlestick` series. If the [type](#series.candlestick.type)
* option is not specified, it is inherited from [chart.type](
* #chart.type).
*
* @type {Object}
* @extends series,plotOptions.candlestick
* @excluding dataParser,dataURL
* @product highstock
* @apioption series.candlestick
*/
/**
* An array of data points for the series. For the `candlestick` series
* type, points can be given in the following ways:
*
* 1. An array of arrays with 5 or 4 values. In this case, the values
* correspond to `x,open,high,low,close`. If the first value is a string,
* it is applied as the name of the point, and the `x` value is inferred.
* The `x` value can also be omitted, in which case the inner arrays
* should be of length 4\. Then the `x` value is automatically calculated,
* either starting at 0 and incremented by 1, or from `pointStart`
* and `pointInterval` given in the series options.
*
* ```js
* data: [
* [0, 7, 2, 0, 4],
* [1, 1, 4, 2, 8],
* [2, 3, 3, 9, 3]
* ]
* ```
*
* 2. An array of objects with named values. The following snippet shows only a
* few settings, see the complete options set below. If the total number of data
* points exceeds the series' [turboThreshold](
* #series.candlestick.turboThreshold), this option is not available.
*
* ```js
* data: [{
* x: 1,
* open: 9,
* high: 2,
* low: 4,
* close: 6,
* name: "Point2",
* color: "#00FF00"
* }, {
* x: 1,
* open: 1,
* high: 4,
* low: 7,
* close: 7,
* name: "Point1",
* color: "#FF00FF"
* }]
* ```
*
* @type {Array<Object|Array>}
* @extends series.ohlc.data
* @excluding y
* @product highstock
* @apioption series.candlestick.data
*/