trendline.src.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @license Highstock JS v8.1.2 (2020-06-16)
  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/Utilities.js']], function (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, seriesType = U.seriesType;
  40. /**
  41. * The Trend line series type.
  42. *
  43. * @private
  44. * @class
  45. * @name Highcharts.seriesTypes.trendline
  46. *
  47. * @augments Highcharts.Series
  48. */
  49. seriesType('trendline', 'sma',
  50. /**
  51. * Trendline (linear regression) fits a straight line to the selected data
  52. * using a method called the Sum Of Least Squares. This series requires the
  53. * `linkedTo` option to be set.
  54. *
  55. * @sample stock/indicators/trendline
  56. * Trendline indicator
  57. *
  58. * @extends plotOptions.sma
  59. * @since 7.1.3
  60. * @product highstock
  61. * @requires stock/indicators/indicators
  62. * @requires stock/indicators/trendline
  63. * @optionparent plotOptions.trendline
  64. */
  65. {
  66. /**
  67. * @excluding period
  68. */
  69. params: {
  70. /**
  71. * The point index which indicator calculations will base. For
  72. * example using OHLC data, index=2 means the indicator will be
  73. * calculated using Low values.
  74. *
  75. * @default 3
  76. */
  77. index: 3
  78. }
  79. },
  80. /**
  81. * @lends Highcharts.Series#
  82. */
  83. {
  84. nameBase: 'Trendline',
  85. nameComponents: false,
  86. getValues: function (series, params) {
  87. 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;
  88. // Get sums:
  89. for (i = 0; i < xValLength; i++) {
  90. x = xVal[i];
  91. y = isArray(yVal[i]) ? yVal[i][index] : yVal[i];
  92. sumX += x;
  93. sumY += y;
  94. sumXY += x * y;
  95. sumX2 += x * x;
  96. }
  97. // Get slope and offset:
  98. alpha = (xValLength * sumXY - sumX * sumY) /
  99. (xValLength * sumX2 - sumX * sumX);
  100. if (isNaN(alpha)) {
  101. alpha = 0;
  102. }
  103. beta = (sumY - alpha * sumX) / xValLength;
  104. // Calculate linear regression:
  105. for (i = 0; i < xValLength; i++) {
  106. x = xVal[i];
  107. y = alpha * x + beta;
  108. // Prepare arrays required for getValues() method
  109. LR[i] = [x, y];
  110. xData[i] = x;
  111. yData[i] = y;
  112. }
  113. return {
  114. xData: xData,
  115. yData: yData,
  116. values: LR
  117. };
  118. }
  119. });
  120. /**
  121. * A `TrendLine` series. If the [type](#series.trendline.type) option is not
  122. * specified, it is inherited from [chart.type](#chart.type).
  123. *
  124. * @extends series,plotOptions.trendline
  125. * @since 7.1.3
  126. * @product highstock
  127. * @excluding dataParser, dataURL
  128. * @requires stock/indicators/indicators
  129. * @requires stock/indicators/trendline
  130. * @apioption series.trendline
  131. */
  132. ''; // to include the above in the js output
  133. });
  134. _registerModule(_modules, 'masters/indicators/trendline.src.js', [], function () {
  135. });
  136. }));