trendline.src.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. * @license Highstock JS v9.0.1 (2021-02-16)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2021 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/TrendLine/TrendLineIndicator.js', [_modules['Core/Series/SeriesRegistry.js'], _modules['Core/Utilities.js']], function (SeriesRegistry, U) {
  32. /* *
  33. *
  34. * License: www.highcharts.com/license
  35. *
  36. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  37. *
  38. * */
  39. var __extends = (this && this.__extends) || (function () {
  40. var extendStatics = function (d,
  41. b) {
  42. extendStatics = Object.setPrototypeOf ||
  43. ({ __proto__: [] } instanceof Array && function (d,
  44. b) { d.__proto__ = b; }) ||
  45. function (d,
  46. b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  47. return extendStatics(d, b);
  48. };
  49. return function (d, b) {
  50. extendStatics(d, b);
  51. function __() { this.constructor = d; }
  52. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  53. };
  54. })();
  55. var SMAIndicator = SeriesRegistry.seriesTypes.sma;
  56. var extend = U.extend,
  57. merge = U.merge,
  58. isArray = U.isArray;
  59. /* *
  60. *
  61. * Class
  62. *
  63. * */
  64. /**
  65. * The Trend line series type.
  66. *
  67. * @private
  68. * @class
  69. * @name Highcharts.seriesTypes.trendline
  70. *
  71. * @augments Highcharts.Series
  72. */
  73. var TrendLineIndicator = /** @class */ (function (_super) {
  74. __extends(TrendLineIndicator, _super);
  75. function TrendLineIndicator() {
  76. var _this = _super !== null && _super.apply(this,
  77. arguments) || this;
  78. /* *
  79. *
  80. * Properties
  81. *
  82. * */
  83. _this.data = void 0;
  84. _this.options = void 0;
  85. _this.points = void 0;
  86. return _this;
  87. }
  88. /* *
  89. *
  90. * Functions
  91. *
  92. * */
  93. TrendLineIndicator.prototype.getValues = function (series, params) {
  94. var xVal = series.xData,
  95. yVal = series.yData,
  96. LR = [],
  97. xData = [],
  98. yData = [],
  99. sumX = 0,
  100. sumY = 0,
  101. sumXY = 0,
  102. sumX2 = 0,
  103. xValLength = xVal.length,
  104. index = params.index,
  105. alpha,
  106. beta,
  107. i,
  108. x,
  109. y;
  110. // Get sums:
  111. for (i = 0; i < xValLength; i++) {
  112. x = xVal[i];
  113. y = isArray(yVal[i]) ? yVal[i][index] : yVal[i];
  114. sumX += x;
  115. sumY += y;
  116. sumXY += x * y;
  117. sumX2 += x * x;
  118. }
  119. // Get slope and offset:
  120. alpha = (xValLength * sumXY - sumX * sumY) /
  121. (xValLength * sumX2 - sumX * sumX);
  122. if (isNaN(alpha)) {
  123. alpha = 0;
  124. }
  125. beta = (sumY - alpha * sumX) / xValLength;
  126. // Calculate linear regression:
  127. for (i = 0; i < xValLength; i++) {
  128. x = xVal[i];
  129. y = alpha * x + beta;
  130. // Prepare arrays required for getValues() method
  131. LR[i] = [x, y];
  132. xData[i] = x;
  133. yData[i] = y;
  134. }
  135. return {
  136. xData: xData,
  137. yData: yData,
  138. values: LR
  139. };
  140. };
  141. /**
  142. * Trendline (linear regression) fits a straight line to the selected data
  143. * using a method called the Sum Of Least Squares. This series requires the
  144. * `linkedTo` option to be set.
  145. *
  146. * @sample stock/indicators/trendline
  147. * Trendline indicator
  148. *
  149. * @extends plotOptions.sma
  150. * @since 7.1.3
  151. * @product highstock
  152. * @requires stock/indicators/indicators
  153. * @requires stock/indicators/trendline
  154. * @optionparent plotOptions.trendline
  155. */
  156. TrendLineIndicator.defaultOptions = merge(SMAIndicator.defaultOptions, {
  157. /**
  158. * @excluding period
  159. */
  160. params: {
  161. /**
  162. * The point index which indicator calculations will base. For
  163. * example using OHLC data, index=2 means the indicator will be
  164. * calculated using Low values.
  165. *
  166. * @default 3
  167. */
  168. index: 3
  169. }
  170. });
  171. return TrendLineIndicator;
  172. }(SMAIndicator));
  173. extend(TrendLineIndicator.prototype, {
  174. nameBase: 'Trendline',
  175. nameComponents: false
  176. });
  177. SeriesRegistry.registerSeriesType('trendline', TrendLineIndicator);
  178. /* *
  179. *
  180. * Default Export
  181. *
  182. * */
  183. /**
  184. * A `TrendLine` series. If the [type](#series.trendline.type) option is not
  185. * specified, it is inherited from [chart.type](#chart.type).
  186. *
  187. * @extends series,plotOptions.trendline
  188. * @since 7.1.3
  189. * @product highstock
  190. * @excluding dataParser, dataURL
  191. * @requires stock/indicators/indicators
  192. * @requires stock/indicators/trendline
  193. * @apioption series.trendline
  194. */
  195. ''; // to include the above in the js output
  196. return TrendLineIndicator;
  197. });
  198. _registerModule(_modules, 'masters/indicators/trendline.src.js', [], function () {
  199. });
  200. }));