OHLCSeries.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. var seriesTypes = BaseSeries.seriesTypes;
  12. import Point from '../Core/Series/Point.js';
  13. import './ColumnSeries.js';
  14. /**
  15. * The ohlc series type.
  16. *
  17. * @private
  18. * @class
  19. * @name Highcharts.seriesTypes.ohlc
  20. *
  21. * @augments Highcharts.Series
  22. */
  23. BaseSeries.seriesType('ohlc', 'column'
  24. /**
  25. * An OHLC chart is a style of financial chart used to describe price
  26. * movements over time. It displays open, high, low and close values per
  27. * data point.
  28. *
  29. * @sample stock/demo/ohlc/
  30. * OHLC chart
  31. *
  32. * @extends plotOptions.column
  33. * @excluding borderColor, borderRadius, borderWidth, crisp, stacking,
  34. * stack
  35. * @product highstock
  36. * @optionparent plotOptions.ohlc
  37. */
  38. , {
  39. /**
  40. * The approximate pixel width of each group. If for example a series
  41. * with 30 points is displayed over a 600 pixel wide plot area, no
  42. * grouping is performed. If however the series contains so many points
  43. * that the spacing is less than the groupPixelWidth, Highcharts will
  44. * try to group it into appropriate groups so that each is more or less
  45. * two pixels wide. Defaults to `5`.
  46. *
  47. * @type {number}
  48. * @default 5
  49. * @product highstock
  50. * @apioption plotOptions.ohlc.dataGrouping.groupPixelWidth
  51. */
  52. /**
  53. * The pixel width of the line/border. Defaults to `1`.
  54. *
  55. * @sample {highstock} stock/plotoptions/ohlc-linewidth/
  56. * A greater line width
  57. *
  58. * @type {number}
  59. * @default 1
  60. * @product highstock
  61. *
  62. * @private
  63. */
  64. lineWidth: 1,
  65. tooltip: {
  66. pointFormat: '<span style="color:{point.color}">\u25CF</span> ' +
  67. '<b> {series.name}</b><br/>' +
  68. 'Open: {point.open}<br/>' +
  69. 'High: {point.high}<br/>' +
  70. 'Low: {point.low}<br/>' +
  71. 'Close: {point.close}<br/>'
  72. },
  73. threshold: null,
  74. states: {
  75. /**
  76. * @extends plotOptions.column.states.hover
  77. * @product highstock
  78. */
  79. hover: {
  80. /**
  81. * The pixel width of the line representing the OHLC point.
  82. *
  83. * @type {number}
  84. * @default 3
  85. * @product highstock
  86. */
  87. lineWidth: 3
  88. }
  89. },
  90. /**
  91. * Determines which one of `open`, `high`, `low`, `close` values should
  92. * be represented as `point.y`, which is later used to set dataLabel
  93. * position and [compare](#plotOptions.series.compare).
  94. *
  95. * @sample {highstock} stock/plotoptions/ohlc-pointvalkey/
  96. * Possible values
  97. *
  98. * @type {string}
  99. * @default close
  100. * @validvalue ["open", "high", "low", "close"]
  101. * @product highstock
  102. * @apioption plotOptions.ohlc.pointValKey
  103. */
  104. /**
  105. * @default close
  106. * @apioption plotOptions.ohlc.colorKey
  107. */
  108. /**
  109. * Line color for up points.
  110. *
  111. * @type {Highcharts.ColorString|Highcharts.GradientColorObject|Highcharts.PatternObject}
  112. * @product highstock
  113. * @apioption plotOptions.ohlc.upColor
  114. */
  115. stickyTracking: true
  116. },
  117. /**
  118. * @lends Highcharts.seriesTypes.ohlc
  119. */
  120. {
  121. /* eslint-disable valid-jsdoc */
  122. directTouch: false,
  123. pointArrayMap: ['open', 'high', 'low', 'close'],
  124. toYData: function (point) {
  125. // return a plain array for speedy calculation
  126. return [point.open, point.high, point.low, point.close];
  127. },
  128. pointValKey: 'close',
  129. pointAttrToOptions: {
  130. stroke: 'color',
  131. 'stroke-width': 'lineWidth'
  132. },
  133. /**
  134. * @private
  135. * @function Highcarts.seriesTypes.ohlc#init
  136. * @return {void}
  137. */
  138. init: function () {
  139. seriesTypes.column.prototype.init.apply(this, arguments);
  140. this.options.stacking = void 0; // #8817
  141. },
  142. /**
  143. * Postprocess mapping between options and SVG attributes
  144. *
  145. * @private
  146. * @function Highcharts.seriesTypes.ohlc#pointAttribs
  147. * @param {Highcharts.OHLCPoint} point
  148. * @param {string} state
  149. * @return {Highcharts.SVGAttributes}
  150. */
  151. pointAttribs: function (point, state) {
  152. var attribs = seriesTypes.column.prototype.pointAttribs.call(this, point, state), options = this.options;
  153. delete attribs.fill;
  154. if (!point.options.color &&
  155. options.upColor &&
  156. point.open < point.close) {
  157. attribs.stroke = options.upColor;
  158. }
  159. return attribs;
  160. },
  161. /**
  162. * Translate data points from raw values x and y to plotX and plotY
  163. *
  164. * @private
  165. * @function Highcharts.seriesTypes.ohlc#translate
  166. * @return {void}
  167. */
  168. translate: function () {
  169. var series = this, yAxis = series.yAxis, hasModifyValue = !!series.modifyValue, translated = [
  170. 'plotOpen',
  171. 'plotHigh',
  172. 'plotLow',
  173. 'plotClose',
  174. 'yBottom'
  175. ]; // translate OHLC for
  176. seriesTypes.column.prototype.translate.apply(series);
  177. // Do the translation
  178. series.points.forEach(function (point) {
  179. [point.open, point.high, point.low, point.close, point.low]
  180. .forEach(function (value, i) {
  181. if (value !== null) {
  182. if (hasModifyValue) {
  183. value = series.modifyValue(value);
  184. }
  185. point[translated[i]] =
  186. yAxis.toPixels(value, true);
  187. }
  188. });
  189. // Align the tooltip to the high value to avoid covering the
  190. // point
  191. point.tooltipPos[1] =
  192. point.plotHigh + yAxis.pos - series.chart.plotTop;
  193. });
  194. },
  195. /**
  196. * Draw the data points
  197. *
  198. * @private
  199. * @function Highcharts.seriesTypes.ohlc#drawPoints
  200. * @return {void}
  201. */
  202. drawPoints: function () {
  203. var series = this, points = series.points, chart = series.chart,
  204. /**
  205. * Extend vertical stem to open and close values.
  206. */
  207. extendStem = function (path, halfStrokeWidth, openOrClose) {
  208. var start = path[0];
  209. var end = path[1];
  210. // We don't need to worry about crisp - openOrClose value
  211. // is already crisped and halfStrokeWidth should remove it.
  212. if (typeof start[2] === 'number') {
  213. start[2] = Math.max(openOrClose + halfStrokeWidth, start[2]);
  214. }
  215. if (typeof end[2] === 'number') {
  216. end[2] = Math.min(openOrClose - halfStrokeWidth, end[2]);
  217. }
  218. };
  219. points.forEach(function (point) {
  220. var plotOpen, plotClose, crispCorr, halfWidth, path, graphic = point.graphic, crispX, isNew = !graphic, strokeWidth;
  221. if (typeof point.plotY !== 'undefined') {
  222. // Create and/or update the graphic
  223. if (!graphic) {
  224. point.graphic = graphic = chart.renderer.path()
  225. .add(series.group);
  226. }
  227. if (!chart.styledMode) {
  228. graphic.attr(series.pointAttribs(point, (point.selected && 'select'))); // #3897
  229. }
  230. // crisp vector coordinates
  231. strokeWidth = graphic.strokeWidth();
  232. crispCorr = (strokeWidth % 2) / 2;
  233. // #2596:
  234. crispX = Math.round(point.plotX) - crispCorr;
  235. halfWidth = Math.round(point.shapeArgs.width / 2);
  236. // the vertical stem
  237. path = [
  238. ['M', crispX, Math.round(point.yBottom)],
  239. ['L', crispX, Math.round(point.plotHigh)]
  240. ];
  241. // open
  242. if (point.open !== null) {
  243. plotOpen = Math.round(point.plotOpen) + crispCorr;
  244. path.push(['M', crispX, plotOpen], ['L', crispX - halfWidth, plotOpen]);
  245. extendStem(path, strokeWidth / 2, plotOpen);
  246. }
  247. // close
  248. if (point.close !== null) {
  249. plotClose = Math.round(point.plotClose) + crispCorr;
  250. path.push(['M', crispX, plotClose], ['L', crispX + halfWidth, plotClose]);
  251. extendStem(path, strokeWidth / 2, plotClose);
  252. }
  253. graphic[isNew ? 'attr' : 'animate']({ d: path })
  254. .addClass(point.getClassName(), true);
  255. }
  256. });
  257. },
  258. animate: null // Disable animation
  259. /* eslint-enable valid-jsdoc */
  260. },
  261. /**
  262. * @lends Highcharts.seriesTypes.ohlc.prototype.pointClass.prototype
  263. */
  264. {
  265. /* eslint-disable valid-jsdoc */
  266. /**
  267. * Extend the parent method by adding up or down to the class name.
  268. * @private
  269. * @function Highcharts.seriesTypes.ohlc#getClassName
  270. * @return {string}
  271. */
  272. getClassName: function () {
  273. return Point.prototype.getClassName.call(this) +
  274. (this.open < this.close ?
  275. ' highcharts-point-up' :
  276. ' highcharts-point-down');
  277. }
  278. /* eslint-enable valid-jsdoc */
  279. });
  280. /**
  281. * A `ohlc` series. If the [type](#series.ohlc.type) option is not
  282. * specified, it is inherited from [chart.type](#chart.type).
  283. *
  284. * @extends series,plotOptions.ohlc
  285. * @excluding dataParser, dataURL
  286. * @product highstock
  287. * @apioption series.ohlc
  288. */
  289. /**
  290. * An array of data points for the series. For the `ohlc` series type,
  291. * points can be given in the following ways:
  292. *
  293. * 1. An array of arrays with 5 or 4 values. In this case, the values correspond
  294. * to `x,open,high,low,close`. If the first value is a string, it is applied
  295. * as the name of the point, and the `x` value is inferred. The `x` value can
  296. * also be omitted, in which case the inner arrays should be of length 4\.
  297. * Then the `x` value is automatically calculated, either starting at 0 and
  298. * incremented by 1, or from `pointStart` and `pointInterval` given in the
  299. * series options.
  300. * ```js
  301. * data: [
  302. * [0, 6, 5, 6, 7],
  303. * [1, 9, 4, 8, 2],
  304. * [2, 6, 3, 4, 10]
  305. * ]
  306. * ```
  307. *
  308. * 2. An array of objects with named values. The following snippet shows only a
  309. * few settings, see the complete options set below. If the total number of
  310. * data points exceeds the series'
  311. * [turboThreshold](#series.ohlc.turboThreshold), this option is not
  312. * available.
  313. * ```js
  314. * data: [{
  315. * x: 1,
  316. * open: 3,
  317. * high: 4,
  318. * low: 5,
  319. * close: 2,
  320. * name: "Point2",
  321. * color: "#00FF00"
  322. * }, {
  323. * x: 1,
  324. * open: 4,
  325. * high: 3,
  326. * low: 6,
  327. * close: 7,
  328. * name: "Point1",
  329. * color: "#FF00FF"
  330. * }]
  331. * ```
  332. *
  333. * @type {Array<Array<(number|string),number,number,number>|Array<(number|string),number,number,number,number>|*>}
  334. * @extends series.arearange.data
  335. * @excluding y, marker
  336. * @product highstock
  337. * @apioption series.ohlc.data
  338. */
  339. /**
  340. * The closing value of each data point.
  341. *
  342. * @type {number}
  343. * @product highstock
  344. * @apioption series.ohlc.data.close
  345. */
  346. /**
  347. * The opening value of each data point.
  348. *
  349. * @type {number}
  350. * @product highstock
  351. * @apioption series.ohlc.data.open
  352. */
  353. ''; // adds doclets above to transpilat