price-indicator.src.js 3.5 KB

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