trendline.src.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @license Highstock JS v8.0.0 (2019-12-10)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Sebastian Bochan
  7. *
  8. * License: www.highcharts.com/license
  9. */
  10. 'use strict';
  11. (function (factory) {
  12. if (typeof module === 'object' && module.exports) {
  13. factory['default'] = factory;
  14. module.exports = factory;
  15. } else if (typeof define === 'function' && define.amd) {
  16. define('highcharts/indicators/trendline', ['highcharts', 'highcharts/modules/stock'], function (Highcharts) {
  17. factory(Highcharts);
  18. factory.Highcharts = Highcharts;
  19. return factory;
  20. });
  21. } else {
  22. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  23. }
  24. }(function (Highcharts) {
  25. var _modules = Highcharts ? Highcharts._modules : {};
  26. function _registerModule(obj, path, args, fn) {
  27. if (!obj.hasOwnProperty(path)) {
  28. obj[path] = fn.apply(null, args);
  29. }
  30. }
  31. _registerModule(_modules, 'indicators/trendline.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var isArray = U.isArray;
  40. var seriesType = H.seriesType;
  41. /**
  42. * The Trend line series type.
  43. *
  44. * @private
  45. * @class
  46. * @name Highcharts.seriesTypes.trendline
  47. *
  48. * @augments Highcharts.Series
  49. */
  50. seriesType('trendline', 'sma',
  51. /**
  52. * Trendline (linear regression) fits a straight line to the selected data
  53. * using a method called the Sum Of Least Squares. This series requires the
  54. * `linkedTo` option to be set.
  55. *
  56. * @sample stock/indicators/trendline
  57. * Trendline indicator
  58. *
  59. * @extends plotOptions.sma
  60. * @since 7.1.3
  61. * @product highstock
  62. * @requires stock/indicators/indicators
  63. * @requires stock/indicators/trendline
  64. * @optionparent plotOptions.trendline
  65. */
  66. {
  67. /**
  68. * @excluding period
  69. */
  70. params: {
  71. /**
  72. * The point index which indicator calculations will base. For
  73. * example using OHLC data, index=2 means the indicator will be
  74. * calculated using Low values.
  75. *
  76. * @default 3
  77. */
  78. index: 3
  79. }
  80. },
  81. /**
  82. * @lends Highcharts.Series#
  83. */
  84. {
  85. nameBase: 'Trendline',
  86. nameComponents: false,
  87. getValues: function (series, params) {
  88. 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;
  89. // Get sums:
  90. for (i = 0; i < xValLength; i++) {
  91. x = xVal[i];
  92. y = isArray(yVal[i]) ? yVal[i][index] : yVal[i];
  93. sumX += x;
  94. sumY += y;
  95. sumXY += x * y;
  96. sumX2 += x * x;
  97. }
  98. // Get slope and offset:
  99. alpha = (xValLength * sumXY - sumX * sumY) /
  100. (xValLength * sumX2 - sumX * sumX);
  101. if (isNaN(alpha)) {
  102. alpha = 0;
  103. }
  104. beta = (sumY - alpha * sumX) / xValLength;
  105. // Calculate linear regression:
  106. for (i = 0; i < xValLength; i++) {
  107. x = xVal[i];
  108. y = alpha * x + beta;
  109. // Prepare arrays required for getValues() method
  110. LR[i] = [x, y];
  111. xData[i] = x;
  112. yData[i] = y;
  113. }
  114. return {
  115. xData: xData,
  116. yData: yData,
  117. values: LR
  118. };
  119. }
  120. });
  121. /**
  122. * A `TrendLine` series. If the [type](#series.trendline.type) option is not
  123. * specified, it is inherited from [chart.type](#chart.type).
  124. *
  125. * @extends series,plotOptions.trendline
  126. * @since 7.1.3
  127. * @product highstock
  128. * @excluding dataParser, dataURL
  129. * @requires stock/indicators/indicators
  130. * @requires stock/indicators/trendline
  131. * @apioption series.trendline
  132. */
  133. ''; // to include the above in the js output
  134. });
  135. _registerModule(_modules, 'masters/indicators/trendline.src.js', [], function () {
  136. });
  137. }));