regressions.src.js 13 KB

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