TrendLineIndicator.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /* *
  2. *
  3. * License: www.highcharts.com/license
  4. *
  5. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  6. *
  7. * */
  8. 'use strict';
  9. import U from '../../Core/Utilities.js';
  10. var isArray = U.isArray, seriesType = U.seriesType;
  11. /**
  12. * The Trend line series type.
  13. *
  14. * @private
  15. * @class
  16. * @name Highcharts.seriesTypes.trendline
  17. *
  18. * @augments Highcharts.Series
  19. */
  20. seriesType('trendline', 'sma',
  21. /**
  22. * Trendline (linear regression) fits a straight line to the selected data
  23. * using a method called the Sum Of Least Squares. This series requires the
  24. * `linkedTo` option to be set.
  25. *
  26. * @sample stock/indicators/trendline
  27. * Trendline indicator
  28. *
  29. * @extends plotOptions.sma
  30. * @since 7.1.3
  31. * @product highstock
  32. * @requires stock/indicators/indicators
  33. * @requires stock/indicators/trendline
  34. * @optionparent plotOptions.trendline
  35. */
  36. {
  37. /**
  38. * @excluding period
  39. */
  40. params: {
  41. /**
  42. * The point index which indicator calculations will base. For
  43. * example using OHLC data, index=2 means the indicator will be
  44. * calculated using Low values.
  45. *
  46. * @default 3
  47. */
  48. index: 3
  49. }
  50. },
  51. /**
  52. * @lends Highcharts.Series#
  53. */
  54. {
  55. nameBase: 'Trendline',
  56. nameComponents: false,
  57. getValues: function (series, params) {
  58. var xVal = series.xData, yVal = series.yData, LR = [], xData = [], yData = [], sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0, xValLength = xVal.length, index = params.index, alpha, beta, i, x, y;
  59. // Get sums:
  60. for (i = 0; i < xValLength; i++) {
  61. x = xVal[i];
  62. y = isArray(yVal[i]) ? yVal[i][index] : yVal[i];
  63. sumX += x;
  64. sumY += y;
  65. sumXY += x * y;
  66. sumX2 += x * x;
  67. }
  68. // Get slope and offset:
  69. alpha = (xValLength * sumXY - sumX * sumY) /
  70. (xValLength * sumX2 - sumX * sumX);
  71. if (isNaN(alpha)) {
  72. alpha = 0;
  73. }
  74. beta = (sumY - alpha * sumX) / xValLength;
  75. // Calculate linear regression:
  76. for (i = 0; i < xValLength; i++) {
  77. x = xVal[i];
  78. y = alpha * x + beta;
  79. // Prepare arrays required for getValues() method
  80. LR[i] = [x, y];
  81. xData[i] = x;
  82. yData[i] = y;
  83. }
  84. return {
  85. xData: xData,
  86. yData: yData,
  87. values: LR
  88. };
  89. }
  90. });
  91. /**
  92. * A `TrendLine` series. If the [type](#series.trendline.type) option is not
  93. * specified, it is inherited from [chart.type](#chart.type).
  94. *
  95. * @extends series,plotOptions.trendline
  96. * @since 7.1.3
  97. * @product highstock
  98. * @excluding dataParser, dataURL
  99. * @requires stock/indicators/indicators
  100. * @requires stock/indicators/trendline
  101. * @apioption series.trendline
  102. */
  103. ''; // to include the above in the js output