trendline.src.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. * @license Highstock JS v8.2.0 (2020-08-20)
  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, 'Stock/Indicators/TrendLineIndicator.js', [_modules['Core/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,
  40. seriesType = U.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,
  89. yVal = series.yData,
  90. LR = [],
  91. xData = [],
  92. yData = [],
  93. sumX = 0,
  94. sumY = 0,
  95. sumXY = 0,
  96. sumX2 = 0,
  97. xValLength = xVal.length,
  98. index = params.index,
  99. alpha,
  100. beta,
  101. i,
  102. x,
  103. y;
  104. // Get sums:
  105. for (i = 0; i < xValLength; i++) {
  106. x = xVal[i];
  107. y = isArray(yVal[i]) ? yVal[i][index] : yVal[i];
  108. sumX += x;
  109. sumY += y;
  110. sumXY += x * y;
  111. sumX2 += x * x;
  112. }
  113. // Get slope and offset:
  114. alpha = (xValLength * sumXY - sumX * sumY) /
  115. (xValLength * sumX2 - sumX * sumX);
  116. if (isNaN(alpha)) {
  117. alpha = 0;
  118. }
  119. beta = (sumY - alpha * sumX) / xValLength;
  120. // Calculate linear regression:
  121. for (i = 0; i < xValLength; i++) {
  122. x = xVal[i];
  123. y = alpha * x + beta;
  124. // Prepare arrays required for getValues() method
  125. LR[i] = [x, y];
  126. xData[i] = x;
  127. yData[i] = y;
  128. }
  129. return {
  130. xData: xData,
  131. yData: yData,
  132. values: LR
  133. };
  134. }
  135. });
  136. /**
  137. * A `TrendLine` series. If the [type](#series.trendline.type) option is not
  138. * specified, it is inherited from [chart.type](#chart.type).
  139. *
  140. * @extends series,plotOptions.trendline
  141. * @since 7.1.3
  142. * @product highstock
  143. * @excluding dataParser, dataURL
  144. * @requires stock/indicators/indicators
  145. * @requires stock/indicators/trendline
  146. * @apioption series.trendline
  147. */
  148. ''; // to include the above in the js output
  149. });
  150. _registerModule(_modules, 'masters/indicators/trendline.src.js', [], function () {
  151. });
  152. }));