CandlestickSeries.js 10 KB

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