price-indicator.src.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /**
  2. * (c) 2009-2019 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 H from '../parts/Globals.js';
  12. import U from '../parts/Utilities.js';
  13. var isArray = U.isArray;
  14. var addEvent = H.addEvent, merge = H.merge;
  15. /**
  16. * The line marks the last price from visible range of points.
  17. *
  18. * @sample {highstock} stock/indicators/last-visible-price
  19. * Last visible price
  20. *
  21. * @declare Highcharts.SeriesLastVisiblePriceOptionsObject
  22. * @product highstock
  23. * @requires modules/price-indicator
  24. * @apioption plotOptions.series.lastVisiblePrice
  25. */
  26. /**
  27. * Enable or disable the indicator.
  28. *
  29. * @type {boolean}
  30. * @product highstock
  31. * @default true
  32. * @apioption plotOptions.series.lastVisiblePrice.enabled
  33. */
  34. /**
  35. * @declare Highcharts.SeriesLastVisiblePriceLabelOptionsObject
  36. * @apioption plotOptions.series.lastVisiblePrice.label
  37. */
  38. /**
  39. * Enable or disable the label.
  40. *
  41. * @type {boolean}
  42. * @product highstock
  43. * @default true
  44. * @apioption plotOptions.series.lastVisiblePrice.label.enabled
  45. *
  46. */
  47. /**
  48. * The line marks the last price from all points.
  49. *
  50. * @sample {highstock} stock/indicators/last-price
  51. * Last price
  52. *
  53. * @declare Highcharts.SeriesLastPriceOptionsObject
  54. * @product highstock
  55. * @requires modules/price-indicator
  56. * @apioption plotOptions.series.lastPrice
  57. */
  58. /**
  59. * Enable or disable the indicator.
  60. *
  61. * @type {boolean}
  62. * @product highstock
  63. * @default true
  64. * @apioption plotOptions.series.lastPrice.enabled
  65. */
  66. /**
  67. * The color of the line of last price.
  68. *
  69. * @type {string}
  70. * @product highstock
  71. * @default red
  72. * @apioption plotOptions.series.lastPrice.color
  73. *
  74. */
  75. /* eslint-disable no-invalid-this */
  76. addEvent(H.Series, 'afterRender', function () {
  77. var serie = this, seriesOptions = serie.options, pointRange = seriesOptions.pointRange, lastVisiblePrice = seriesOptions.lastVisiblePrice, lastPrice = seriesOptions.lastPrice;
  78. if ((lastVisiblePrice || lastPrice) &&
  79. seriesOptions.id !== 'highcharts-navigator-series') {
  80. var xAxis = serie.xAxis, yAxis = serie.yAxis, origOptions = yAxis.crosshair, origGraphic = yAxis.cross, origLabel = yAxis.crossLabel, points = serie.points, yLength = serie.yData.length, pLength = points.length, x = serie.xData[serie.xData.length - 1], y = serie.yData[yLength - 1], lastPoint, yValue, crop;
  81. if (lastPrice && lastPrice.enabled) {
  82. yAxis.crosshair = yAxis.options.crosshair = seriesOptions.lastPrice;
  83. yAxis.cross = serie.lastPrice;
  84. yValue = isArray(y) ? y[3] : y;
  85. yAxis.drawCrosshair(null, ({
  86. x: x,
  87. y: yValue,
  88. plotX: xAxis.toPixels(x, true),
  89. plotY: yAxis.toPixels(yValue, true)
  90. }));
  91. // Save price
  92. if (serie.yAxis.cross) {
  93. serie.lastPrice = serie.yAxis.cross;
  94. serie.lastPrice.y = yValue;
  95. }
  96. }
  97. if (lastVisiblePrice &&
  98. lastVisiblePrice.enabled &&
  99. pLength > 0) {
  100. crop = (points[pLength - 1].x === x) || pointRange === null ? 1 : 2;
  101. yAxis.crosshair = yAxis.options.crosshair = merge({
  102. color: 'transparent'
  103. }, seriesOptions.lastVisiblePrice);
  104. yAxis.cross = serie.lastVisiblePrice;
  105. lastPoint = points[pLength - crop];
  106. // Save price
  107. yAxis.drawCrosshair(null, lastPoint);
  108. if (yAxis.cross) {
  109. serie.lastVisiblePrice = yAxis.cross;
  110. serie.lastVisiblePrice.y = lastPoint.y;
  111. }
  112. if (serie.crossLabel) {
  113. serie.crossLabel.destroy();
  114. }
  115. serie.crossLabel = yAxis.crossLabel;
  116. }
  117. // Restore crosshair:
  118. yAxis.crosshair = origOptions;
  119. yAxis.cross = origGraphic;
  120. yAxis.crossLabel = origLabel;
  121. }
  122. });