regressions.src.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * @license Highstock JS v8.0.0 (2019-12-10)
  3. *
  4. * Indicator series type for Highstock
  5. *
  6. * (c) 2010-2019 Kamil Kulig
  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/regressions', ['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/regressions.src.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  32. /**
  33. *
  34. * (c) 2010-2019 Kamil Kulig
  35. *
  36. * License: www.highcharts.com/license
  37. *
  38. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  39. *
  40. * */
  41. var isArray = U.isArray;
  42. var seriesType = H.seriesType;
  43. /**
  44. * Linear regression series type.
  45. *
  46. * @private
  47. * @class
  48. * @name Highcharts.seriesTypes.linearregression
  49. *
  50. * @augments Highcharts.Series
  51. */
  52. seriesType('linearRegression', 'sma',
  53. /**
  54. * Linear regression indicator. This series requires `linkedTo` option to be
  55. * set.
  56. *
  57. * @sample {highstock} stock/indicators/linear-regression
  58. * Linear regression indicator
  59. *
  60. * @extends plotOptions.sma
  61. * @since 7.0.0
  62. * @product highstock
  63. * @requires stock/indicators/indicators
  64. * @requires stock/indicators/regressions
  65. * @optionparent plotOptions.linearregression
  66. */
  67. {
  68. params: {
  69. /**
  70. * Unit (in milliseconds) for the x axis distances used to compute
  71. * the regression line paramters (slope & intercept) for every
  72. * range. In Highstock the x axis values are always represented in
  73. * milliseconds which may cause that distances between points are
  74. * "big" integer numbers.
  75. *
  76. * Highstock's linear regression algorithm (least squares method)
  77. * will utilize these "big" integers for finding the slope and the
  78. * intercept of the regression line for each period. In consequence,
  79. * this value may be a very "small" decimal number that's hard to
  80. * interpret by a human.
  81. *
  82. * For instance: `xAxisUnit` equealed to `86400000` ms (1 day)
  83. * forces the algorithm to treat `86400000` as `1` while computing
  84. * the slope and the intercept. This may enchance the legiblitity of
  85. * the indicator's values.
  86. *
  87. * Default value is the closest distance between two data points.
  88. *
  89. * @sample {highstock} stock/plotoptions/linear-regression-xaxisunit
  90. * xAxisUnit set to 1 minute
  91. *
  92. * @example
  93. * // In Liniear Regression Slope Indicator series `xAxisUnit` is
  94. * // `86400000` (1 day) and period is `3`. There're 3 points in the
  95. * // base series:
  96. *
  97. * data: [
  98. * [Date.UTC(2019, 0, 1), 1],
  99. * [Date.UTC(2019, 0, 2), 3],
  100. * [Date.UTC(2019, 0, 3), 5]
  101. * ]
  102. *
  103. * // This will produce one point in the indicator series that has a
  104. * // `y` value of `2` (slope of the regression line). If we change
  105. * // the `xAxisUnit` to `1` (ms) the value of the indicator's point
  106. * // will be `2.3148148148148148e-8` which is harder to interpert
  107. * // for a human.
  108. *
  109. * @type {number}
  110. * @product highstock
  111. */
  112. xAxisUnit: void 0
  113. },
  114. tooltip: {
  115. valueDecimals: 4
  116. }
  117. },
  118. /**
  119. * @lends Highcharts.Series#
  120. */
  121. {
  122. nameBase: 'Linear Regression Indicator',
  123. /**
  124. * Return the slope and intercept of a straight line function.
  125. * @private
  126. * @param {Highcharts.LinearRegressionIndicator} this indicator to use
  127. * @param {Array<number>} xData - list of all x coordinates in a period
  128. * @param {Array<number>} yData - list of all y coordinates in a period
  129. * @return {Highcharts.RegressionLineParametersObject}
  130. * object that contains the slope and the intercept
  131. * of a straight line function
  132. */
  133. getRegressionLineParameters: function (xData, yData) {
  134. // least squares method
  135. var yIndex = this.options.params.index, getSingleYValue = function (yValue, yIndex) {
  136. return isArray(yValue) ? yValue[yIndex] : yValue;
  137. }, xSum = xData.reduce(function (accX, val) {
  138. return val + accX;
  139. }, 0), ySum = yData.reduce(function (accY, val) {
  140. return getSingleYValue(val, yIndex) + accY;
  141. }, 0), xMean = xSum / xData.length, yMean = ySum / yData.length, xError, yError, formulaNumerator = 0, formulaDenominator = 0, i, slope;
  142. for (i = 0; i < xData.length; i++) {
  143. xError = xData[i] - xMean;
  144. yError = getSingleYValue(yData[i], yIndex) - yMean;
  145. formulaNumerator += xError * yError;
  146. formulaDenominator += Math.pow(xError, 2);
  147. }
  148. slope = formulaDenominator ?
  149. formulaNumerator / formulaDenominator : 0; // don't divide by 0
  150. return {
  151. slope: slope,
  152. intercept: yMean - slope * xMean
  153. };
  154. },
  155. /**
  156. * Return the y value on a straight line.
  157. * @private
  158. * @param {Highcharts.RegressionLineParametersObject} lineParameters
  159. * object that contains the slope and the intercept
  160. * of a straight line function
  161. * @param {number} endPointX - x coordinate of the point
  162. * @return {number} - y value of the point that lies on the line
  163. */
  164. getEndPointY: function (lineParameters, endPointX) {
  165. return lineParameters.slope * endPointX + lineParameters.intercept;
  166. },
  167. /**
  168. * Transform the coordinate system so that x values start at 0 and
  169. * apply xAxisUnit.
  170. * @private
  171. * @param {Array<number>} xData - list of all x coordinates in a period
  172. * @param {number} xAxisUnit - option (see the API)
  173. * @return {Array<number>} - array of transformed x data
  174. */
  175. transformXData: function (xData, xAxisUnit) {
  176. var xOffset = xData[0];
  177. return xData.map(function (xValue) {
  178. return (xValue - xOffset) / xAxisUnit;
  179. });
  180. },
  181. /**
  182. * Find the closest distance between points in the base series.
  183. * @private
  184. * @param {Array<number>} xData
  185. list of all x coordinates in the base series
  186. * @return {number} - closest distance between points in the base series
  187. */
  188. findClosestDistance: function (xData) {
  189. var distance, closestDistance, i;
  190. for (i = 1; i < xData.length - 1; i++) {
  191. distance = xData[i] - xData[i - 1];
  192. if (distance > 0 &&
  193. (typeof closestDistance === 'undefined' ||
  194. distance < closestDistance)) {
  195. closestDistance = distance;
  196. }
  197. }
  198. return closestDistance;
  199. },
  200. // Required to be implemented - starting point for indicator's logic
  201. getValues: function (baseSeries, regressionSeriesParams) {
  202. var xData = baseSeries.xData, yData = baseSeries.yData, period = regressionSeriesParams.period, lineParameters, i, periodStart, periodEnd,
  203. // format required to be returned
  204. indicatorData = {
  205. xData: [],
  206. yData: [],
  207. values: []
  208. }, endPointX, endPointY, periodXData, periodYData, periodTransformedXData, xAxisUnit = this.options.params.xAxisUnit ||
  209. this.findClosestDistance(xData);
  210. // Iteration logic: x value of the last point within the period
  211. // (end point) is used to represent the y value (regression)
  212. // of the entire period.
  213. for (i = period - 1; i <= xData.length - 1; i++) {
  214. periodStart = i - period + 1; // adjusted for slice() function
  215. periodEnd = i + 1; // (as above)
  216. endPointX = xData[i];
  217. periodXData = xData.slice(periodStart, periodEnd);
  218. periodYData = yData.slice(periodStart, periodEnd);
  219. periodTransformedXData = this.transformXData(periodXData, xAxisUnit);
  220. lineParameters = this.getRegressionLineParameters(periodTransformedXData, periodYData);
  221. endPointY = this.getEndPointY(lineParameters, periodTransformedXData[periodTransformedXData.length - 1]);
  222. // @todo this is probably not used anywhere
  223. indicatorData.values.push({
  224. regressionLineParameters: lineParameters,
  225. x: endPointX,
  226. y: endPointY
  227. });
  228. indicatorData.xData.push(endPointX);
  229. indicatorData.yData.push(endPointY);
  230. }
  231. return indicatorData;
  232. }
  233. });
  234. /**
  235. * A linear regression series. If the [type](#series.linearregression.type)
  236. * option is not specified, it is inherited from [chart.type](#chart.type).
  237. *
  238. * @extends series,plotOptions.linearregression
  239. * @since 7.0.0
  240. * @product highstock
  241. * @excluding dataParser,dataURL
  242. * @requires stock/indicators/indicators
  243. * @requires stock/indicators/regressions
  244. * @apioption series.linearregression
  245. */
  246. /* ************************************************************************** */
  247. /**
  248. * The Linear Regression Slope series type.
  249. *
  250. * @private
  251. * @class
  252. * @name Highcharts.seriesTypes.linearRegressionSlope
  253. *
  254. * @augments Highcharts.Series
  255. */
  256. seriesType('linearRegressionSlope', 'linearRegression',
  257. /**
  258. * Linear regression slope indicator. This series requires `linkedTo`
  259. * option to be set.
  260. *
  261. * @sample {highstock} stock/indicators/linear-regression-slope
  262. * Linear regression slope indicator
  263. *
  264. * @extends plotOptions.linearregression
  265. * @since 7.0.0
  266. * @product highstock
  267. * @requires stock/indicators/indicators
  268. * @requires stock/indicators/regressions
  269. * @optionparent plotOptions.linearregressionslope
  270. */
  271. {},
  272. /**
  273. * @lends Highcharts.Series#
  274. */
  275. {
  276. nameBase: 'Linear Regression Slope Indicator',
  277. getEndPointY: function (lineParameters) {
  278. return lineParameters.slope;
  279. }
  280. });
  281. /**
  282. * A linear regression slope series. If the
  283. * [type](#series.linearregressionslope.type) option is not specified, it is
  284. * inherited from [chart.type](#chart.type).
  285. *
  286. * @extends series,plotOptions.linearregressionslope
  287. * @since 7.0.0
  288. * @product highstock
  289. * @excluding dataParser,dataURL
  290. * @requires stock/indicators/indicators
  291. * @requires stock/indicators/regressions
  292. * @apioption series.linearregressionslope
  293. */
  294. /* ************************************************************************** */
  295. /**
  296. * The Linear Regression Intercept series type.
  297. *
  298. * @private
  299. * @class
  300. * @name Highcharts.seriesTypes.linearRegressionIntercept
  301. *
  302. * @augments Highcharts.Series
  303. */
  304. seriesType('linearRegressionIntercept', 'linearRegression',
  305. /**
  306. * Linear regression intercept indicator. This series requires `linkedTo`
  307. * option to be set.
  308. *
  309. * @sample {highstock} stock/indicators/linear-regression-intercept
  310. * Linear intercept slope indicator
  311. *
  312. * @extends plotOptions.linearregression
  313. * @since 7.0.0
  314. * @product highstock
  315. * @requires stock/indicators/indicators
  316. * @requires stock/indicators/regressions
  317. * @optionparent plotOptions.linearregressionintercept
  318. */
  319. {},
  320. /**
  321. * @lends Highcharts.Series#
  322. */
  323. {
  324. nameBase: 'Linear Regression Intercept Indicator',
  325. getEndPointY: function (lineParameters) {
  326. return lineParameters.intercept;
  327. }
  328. });
  329. /**
  330. * A linear regression intercept series. If the
  331. * [type](#series.linearregressionintercept.type) option is not specified, it is
  332. * inherited from [chart.type](#chart.type).
  333. *
  334. * @extends series,plotOptions.linearregressionintercept
  335. * @since 7.0.0
  336. * @product highstock
  337. * @excluding dataParser,dataURL
  338. * @requires stock/indicators/indicators
  339. * @requires stock/indicators/regressions
  340. * @apioption series.linearregressionintercept
  341. */
  342. /* ************************************************************************** */
  343. /**
  344. * The Linear Regression Angle series type.
  345. *
  346. * @private
  347. * @class
  348. * @name Highcharts.seriesTypes.linearRegressionAngle
  349. *
  350. * @augments Highcharts.Series
  351. */
  352. seriesType('linearRegressionAngle', 'linearRegression',
  353. /**
  354. * Linear regression angle indicator. This series requires `linkedTo`
  355. * option to be set.
  356. *
  357. * @sample {highstock} stock/indicators/linear-regression-angle
  358. * Linear intercept angle indicator
  359. *
  360. * @extends plotOptions.linearregression
  361. * @since 7.0.0
  362. * @product highstock
  363. * @requires stock/indicators/indicators
  364. * @requires stock/indicators/regressions
  365. * @optionparent plotOptions.linearregressionangle
  366. */
  367. {
  368. tooltip: {
  369. pointFormat: '<span style="color:{point.color}">\u25CF</span>' +
  370. '{series.name}: <b>{point.y}°</b><br/>'
  371. }
  372. },
  373. /**
  374. * @lends Highcharts.Series#
  375. */
  376. {
  377. nameBase: 'Linear Regression Angle Indicator',
  378. /**
  379. * Convert a slope of a line to angle (in degrees) between
  380. * the line and x axis
  381. * @private
  382. * @param {number} slope of the straight line function
  383. * @return {number} angle in degrees
  384. */
  385. slopeToAngle: function (slope) {
  386. return Math.atan(slope) * (180 / Math.PI); // rad to deg
  387. },
  388. getEndPointY: function (lineParameters) {
  389. return this.slopeToAngle(lineParameters.slope);
  390. }
  391. });
  392. /**
  393. * A linear regression intercept series. If the
  394. * [type](#series.linearregressionangle.type) option is not specified, it is
  395. * inherited from [chart.type](#chart.type).
  396. *
  397. * @extends series,plotOptions.linearregressionangle
  398. * @since 7.0.0
  399. * @product highstock
  400. * @excluding dataParser,dataURL
  401. * @requires stock/indicators/indicators
  402. * @requires stock/indicators/regressions
  403. * @apioption series.linearregressionangle
  404. */
  405. ''; // to include the above in the js output
  406. });
  407. _registerModule(_modules, 'masters/indicators/regressions.src.js', [], function () {
  408. });
  409. }));