CandlestickSeries.js 10 KB

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