PriceIndication.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /**
  2. * (c) 2009-2021 Sebastian Bochann
  3. *
  4. * Price indicator for Highcharts
  5. *
  6. * License: www.highcharts.com/license
  7. *
  8. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  9. */
  10. 'use strict';
  11. import Series from '../Core/Series/Series.js';
  12. import U from '../Core/Utilities.js';
  13. var addEvent = U.addEvent, isArray = U.isArray, merge = U.merge;
  14. /**
  15. * The line marks the last price from visible range of points.
  16. *
  17. * @sample {highstock} stock/indicators/last-visible-price
  18. * Last visible price
  19. *
  20. * @declare Highcharts.SeriesLastVisiblePriceOptionsObject
  21. * @product highstock
  22. * @requires modules/price-indicator
  23. * @apioption plotOptions.series.lastVisiblePrice
  24. */
  25. /**
  26. * Enable or disable the indicator.
  27. *
  28. * @type {boolean}
  29. * @product highstock
  30. * @default false
  31. * @apioption plotOptions.series.lastVisiblePrice.enabled
  32. */
  33. /**
  34. * @declare Highcharts.SeriesLastVisiblePriceLabelOptionsObject
  35. * @extends yAxis.crosshair.label
  36. * @since 7.0.0
  37. * @apioption plotOptions.series.lastVisiblePrice.label
  38. */
  39. /**
  40. * @since 7.0.0
  41. * @apioption plotOptions.series.lastVisiblePrice.label.align
  42. */
  43. /**
  44. * @since 7.0.0
  45. * @apioption plotOptions.series.lastVisiblePrice.label.backgroundColor
  46. */
  47. /**
  48. * The border color for the `lastVisiblePrice` label.
  49. *
  50. * @type {Highcharts.ColorType}
  51. * @since 7.0.0
  52. * @product highstock
  53. * @apioption plotOptions.series.lastVisiblePrice.label.borderColor
  54. */
  55. /**
  56. * The border corner radius of the `lastVisiblePrice` label.
  57. *
  58. * @type {number}
  59. * @default 3
  60. * @since 7.0.0
  61. * @product highstock
  62. * @apioption plotOptions.series.lastVisiblePrice.label.borderRadius
  63. */
  64. /**
  65. * Flag to enable `lastVisiblePrice` label.
  66. *
  67. *
  68. * @type {boolean}
  69. * @default false
  70. * @since 7.0
  71. * @product highstock
  72. * @apioption plotOptions.series.lastVisiblePrice.label.enabled
  73. */
  74. /**
  75. * A format string for the `lastVisiblePrice` label. Defaults to `{value}` for
  76. * numeric axes and `{value:%b %d, %Y}` for datetime axes.
  77. *
  78. * @type {string}
  79. * @since 7.0
  80. * @product highstock
  81. * @apioption plotOptions.series.lastVisiblePrice.label.format
  82. */
  83. /**
  84. * @since 7.0.0
  85. * @apioption plotOptions.series.lastVisiblePrice.label.formatter
  86. */
  87. /**
  88. * @since 7.0.0
  89. * @apioption plotOptions.series.lastVisiblePrice.label.padding
  90. */
  91. /**
  92. * @since 7.0.0
  93. * @apioption plotOptions.series.lastVisiblePrice.label.shape
  94. */
  95. /**
  96. * Text styles for the `lastVisiblePrice` label.
  97. *
  98. * @type {Highcharts.CSSObject}
  99. * @default {"color": "white", "fontWeight": "normal", "fontSize": "11px", "textAlign": "center"}
  100. * @since 7.0
  101. * @product highstock
  102. * @apioption plotOptions.series.lastVisiblePrice.label.style
  103. */
  104. /**
  105. * The border width for the `lastVisiblePrice` label.
  106. *
  107. * @type {number}
  108. * @default 0
  109. * @since 7.0
  110. * @product highstock
  111. * @apioption plotOptions.series.lastVisiblePrice.label.borderWidth
  112. */
  113. /**
  114. * Padding inside the `lastVisiblePrice` label.
  115. *
  116. * @type {number}
  117. * @default 8
  118. * @since 7.0
  119. * @product highstock
  120. * @apioption plotOptions.series.lastVisiblePrice.label.padding
  121. */
  122. /**
  123. * The line marks the last price from all points.
  124. *
  125. * @sample {highstock} stock/indicators/last-price
  126. * Last price
  127. *
  128. * @declare Highcharts.SeriesLastPriceOptionsObject
  129. * @product highstock
  130. * @requires modules/price-indicator
  131. * @apioption plotOptions.series.lastPrice
  132. */
  133. /**
  134. * Enable or disable the indicator.
  135. *
  136. * @type {boolean}
  137. * @product highstock
  138. * @default false
  139. * @apioption plotOptions.series.lastPrice.enabled
  140. */
  141. /**
  142. * @declare Highcharts.SeriesLastPriceLabelOptionsObject
  143. * @extends yAxis.crosshair.label
  144. * @since 7.0.0
  145. * @apioption plotOptions.series.lastPrice.label
  146. */
  147. /**
  148. * @since 7.0.0
  149. * @apioption plotOptions.series.lastPrice.label.align
  150. * */
  151. /**
  152. * @since 7.0.0
  153. * @apioption plotOptions.series.lastPrice.label.backgroundColor
  154. * */
  155. /**
  156. * The border color of `lastPrice` label.
  157. * @since 7.0.0
  158. * @apioption plotOptions.series.lastPrice.label.borderColor
  159. * */
  160. /**
  161. * The border radius of `lastPrice` label.
  162. * @since 7.0.0
  163. * @apioption plotOptions.series.lastPrice.label.borderRadius
  164. * */
  165. /**
  166. * The border width of `lastPrice` label.
  167. * @since 7.0.0
  168. * @apioption plotOptions.series.lastPrice.label.borderWidth
  169. * */
  170. /**
  171. * Flag to enable `lastPrice` label.
  172. * @since 7.0.0
  173. * @apioption plotOptions.series.lastPrice.label.enabled
  174. * */
  175. /**
  176. * A format string for the `lastPrice` label. Defaults to `{value}` for
  177. * numeric axes and `{value:%b %d, %Y}` for datetime axes.
  178. *
  179. * @type {string}
  180. * @since 7.0
  181. * @product highstock
  182. * @apioption plotOptions.series.lastPrice.label.format
  183. */
  184. /**
  185. * @since 7.0.0
  186. * @apioption plotOptions.series.lastPrice.label.formatter
  187. */
  188. /**
  189. * @since 7.0.0
  190. * @apioption plotOptions.series.lastPrice.label.padding
  191. */
  192. /**
  193. * @since 7.0.0
  194. * @apioption plotOptions.series.lastPrice.label.shape
  195. */
  196. /**
  197. * Text styles for the `lastPrice` label.
  198. *
  199. * @type {Highcharts.CSSObject}
  200. * @default {"color": "white", "fontWeight": "normal", "fontSize": "11px", "textAlign": "center"}
  201. * @since 7.0
  202. * @product highstock
  203. * @apioption plotOptions.series.lastPrice.label.style
  204. */
  205. /**
  206. * The border width for the `lastPrice` label.
  207. *
  208. * @type {number}
  209. * @default 0
  210. * @since 7.0
  211. * @product highstock
  212. * @apioption plotOptions.series.lastPrice.label.borderWidth
  213. */
  214. /**
  215. * Padding inside the `lastPrice` label.
  216. *
  217. * @type {number}
  218. * @default 8
  219. * @since 7.0
  220. * @product highstock
  221. * @apioption plotOptions.series.lastPrice.label.padding
  222. */
  223. /**
  224. * The color of the line of last price.
  225. * By default, the line has the same color as the series.
  226. *
  227. * @type {string}
  228. * @product highstock
  229. * @apioption plotOptions.series.lastPrice.color
  230. *
  231. */
  232. /* eslint-disable no-invalid-this */
  233. addEvent(Series, 'afterRender', function () {
  234. var series = this, seriesOptions = series.options, pointRange = seriesOptions.pointRange, lastVisiblePrice = seriesOptions.lastVisiblePrice, lastPrice = seriesOptions.lastPrice;
  235. if ((lastVisiblePrice || lastPrice) &&
  236. seriesOptions.id !== 'highcharts-navigator-series') {
  237. var xAxis = series.xAxis, yAxis = series.yAxis, origOptions = yAxis.crosshair, origGraphic = yAxis.cross, origLabel = yAxis.crossLabel, points = series.points, yLength = series.yData.length, pLength = points.length, x = series.xData[series.xData.length - 1], y = series.yData[yLength - 1], lastPoint = void 0, yValue = void 0, crop = void 0;
  238. if (lastPrice && lastPrice.enabled) {
  239. yAxis.crosshair = yAxis.options.crosshair = seriesOptions.lastPrice;
  240. if (!series.chart.styledMode &&
  241. yAxis.crosshair &&
  242. yAxis.options.crosshair &&
  243. seriesOptions.lastPrice) {
  244. // Set the default color from the series, #14888.
  245. yAxis.crosshair.color = yAxis.options.crosshair.color = seriesOptions.lastPrice.color || series.color;
  246. }
  247. yAxis.cross = series.lastPrice;
  248. yValue = isArray(y) ? y[3] : y;
  249. yAxis.drawCrosshair(null, ({
  250. x: x,
  251. y: yValue,
  252. plotX: xAxis.toPixels(x, true),
  253. plotY: yAxis.toPixels(yValue, true)
  254. }));
  255. // Save price
  256. if (series.yAxis.cross) {
  257. series.lastPrice = series.yAxis.cross;
  258. series.lastPrice.addClass('highcharts-color-' + series.colorIndex); // #15222
  259. series.lastPrice.y = yValue;
  260. }
  261. }
  262. if (lastVisiblePrice && lastVisiblePrice.enabled && pLength > 0) {
  263. crop = (points[pLength - 1].x === x) || pointRange === null ? 1 : 2;
  264. yAxis.crosshair = yAxis.options.crosshair = merge({
  265. color: 'transparent' // line invisible by default
  266. }, seriesOptions.lastVisiblePrice);
  267. yAxis.cross = series.lastVisiblePrice;
  268. lastPoint = points[pLength - crop];
  269. if (series.crossLabel) {
  270. series.crossLabel.destroy();
  271. }
  272. // Set to undefined to avoid collision with
  273. // the yAxis crosshair #11480
  274. // Delete the crossLabel each time the code is invoked, #13876.
  275. delete yAxis.crossLabel;
  276. // Save price
  277. yAxis.drawCrosshair(null, lastPoint);
  278. if (yAxis.cross) {
  279. series.lastVisiblePrice = yAxis.cross;
  280. if (typeof lastPoint.y === 'number') {
  281. series.lastVisiblePrice.y = lastPoint.y;
  282. }
  283. }
  284. series.crossLabel = yAxis.crossLabel;
  285. }
  286. // Restore crosshair:
  287. yAxis.crosshair = yAxis.options.crosshair = origOptions;
  288. yAxis.cross = origGraphic;
  289. yAxis.crossLabel = origLabel;
  290. }
  291. });