trendline.src.js 2.9 KB

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