BellcurveSeries.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* *
  2. *
  3. * (c) 2010-2021 Highsoft AS
  4. *
  5. * Author: Sebastian Domas
  6. *
  7. * License: www.highcharts.com/license
  8. *
  9. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  10. *
  11. * */
  12. 'use strict';
  13. var __extends = (this && this.__extends) || (function () {
  14. var extendStatics = function (d, b) {
  15. extendStatics = Object.setPrototypeOf ||
  16. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  17. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  18. return extendStatics(d, b);
  19. };
  20. return function (d, b) {
  21. extendStatics(d, b);
  22. function __() { this.constructor = d; }
  23. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  24. };
  25. })();
  26. import DerivedSeriesMixin from '../../Mixins/DerivedSeries.js';
  27. import SeriesRegistry from '../../Core/Series/SeriesRegistry.js';
  28. var AreaSplineSeries = SeriesRegistry.seriesTypes.areaspline;
  29. import U from '../../Core/Utilities.js';
  30. var correctFloat = U.correctFloat, extend = U.extend, isNumber = U.isNumber, merge = U.merge;
  31. /**
  32. * Bell curve class
  33. *
  34. * @private
  35. * @class
  36. * @name Highcharts.seriesTypes.bellcurve
  37. *
  38. * @augments Highcharts.Series
  39. */
  40. var BellcurveSeries = /** @class */ (function (_super) {
  41. __extends(BellcurveSeries, _super);
  42. function BellcurveSeries() {
  43. /* *
  44. *
  45. * Static Properties
  46. *
  47. * */
  48. var _this = _super !== null && _super.apply(this, arguments) || this;
  49. /* eslint-enable valid-jsdoc */
  50. /* *
  51. *
  52. * Properties
  53. *
  54. * */
  55. _this.data = void 0;
  56. _this.options = void 0;
  57. _this.points = void 0;
  58. return _this;
  59. /* eslint-enable valid-jsdoc */
  60. }
  61. /* *
  62. *
  63. * Static Functions
  64. *
  65. * */
  66. /* eslint-disable valid-jsdoc */
  67. /**
  68. * @private
  69. */
  70. BellcurveSeries.mean = function (data) {
  71. var length = data.length, sum = data.reduce(function (sum, value) {
  72. return (sum += value);
  73. }, 0);
  74. return length > 0 && sum / length;
  75. };
  76. /**
  77. * @private
  78. */
  79. BellcurveSeries.standardDeviation = function (data, average) {
  80. var len = data.length, sum;
  81. average = isNumber(average) ? average : BellcurveSeries.mean(data);
  82. sum = data.reduce(function (sum, value) {
  83. var diff = value - average;
  84. return (sum += diff * diff);
  85. }, 0);
  86. return len > 1 && Math.sqrt(sum / (len - 1));
  87. };
  88. /**
  89. * @private
  90. */
  91. BellcurveSeries.normalDensity = function (x, mean, standardDeviation) {
  92. var translation = x - mean;
  93. return Math.exp(-(translation * translation) /
  94. (2 * standardDeviation * standardDeviation)) / (standardDeviation * Math.sqrt(2 * Math.PI));
  95. };
  96. /* *
  97. *
  98. * Functions
  99. *
  100. * */
  101. /* eslint-disable valid-jsdoc */
  102. BellcurveSeries.prototype.derivedData = function (mean, standardDeviation) {
  103. var intervals = this.options.intervals, pointsInInterval = this.options.pointsInInterval, x = mean - intervals * standardDeviation, stop = intervals * pointsInInterval * 2 + 1, increment = standardDeviation / pointsInInterval, data = [], i;
  104. for (i = 0; i < stop; i++) {
  105. data.push([x, BellcurveSeries.normalDensity(x, mean, standardDeviation)]);
  106. x += increment;
  107. }
  108. return data;
  109. };
  110. BellcurveSeries.prototype.setDerivedData = function () {
  111. if (this.baseSeries.yData.length > 1) {
  112. this.setMean();
  113. this.setStandardDeviation();
  114. this.setData(this.derivedData(this.mean, this.standardDeviation), false);
  115. }
  116. return (void 0);
  117. };
  118. BellcurveSeries.prototype.setMean = function () {
  119. this.mean = correctFloat(BellcurveSeries.mean(this.baseSeries.yData));
  120. };
  121. BellcurveSeries.prototype.setStandardDeviation = function () {
  122. this.standardDeviation = correctFloat(BellcurveSeries.standardDeviation(this.baseSeries.yData, this.mean));
  123. };
  124. /**
  125. * A bell curve is an areaspline series which represents the probability
  126. * density function of the normal distribution. It calculates mean and
  127. * standard deviation of the base series data and plots the curve according
  128. * to the calculated parameters.
  129. *
  130. * @sample {highcharts} highcharts/demo/bellcurve/
  131. * Bell curve
  132. *
  133. * @extends plotOptions.areaspline
  134. * @since 6.0.0
  135. * @product highcharts
  136. * @excluding boostThreshold, connectNulls, dragDrop, stacking,
  137. * pointInterval, pointIntervalUnit
  138. * @requires modules/bellcurve
  139. * @optionparent plotOptions.bellcurve
  140. */
  141. BellcurveSeries.defaultOptions = merge(AreaSplineSeries.defaultOptions, {
  142. /**
  143. * @see [fillColor](#plotOptions.bellcurve.fillColor)
  144. * @see [fillOpacity](#plotOptions.bellcurve.fillOpacity)
  145. *
  146. * @apioption plotOptions.bellcurve.color
  147. */
  148. /**
  149. * @see [color](#plotOptions.bellcurve.color)
  150. * @see [fillOpacity](#plotOptions.bellcurve.fillOpacity)
  151. *
  152. * @apioption plotOptions.bellcurve.fillColor
  153. */
  154. /**
  155. * @see [color](#plotOptions.bellcurve.color)
  156. * @see [fillColor](#plotOptions.bellcurve.fillColor)
  157. *
  158. * @default {highcharts} 0.75
  159. * @default {highstock} 0.75
  160. * @apioption plotOptions.bellcurve.fillOpacity
  161. */
  162. /**
  163. * This option allows to define the length of the bell curve. A unit of
  164. * the length of the bell curve is standard deviation.
  165. *
  166. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  167. * Intervals and points in interval
  168. */
  169. intervals: 3,
  170. /**
  171. * Defines how many points should be plotted within 1 interval. See
  172. * `plotOptions.bellcurve.intervals`.
  173. *
  174. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  175. * Intervals and points in interval
  176. */
  177. pointsInInterval: 3,
  178. marker: {
  179. enabled: false
  180. }
  181. });
  182. return BellcurveSeries;
  183. }(AreaSplineSeries));
  184. extend(BellcurveSeries.prototype, {
  185. addBaseSeriesEvents: DerivedSeriesMixin.addBaseSeriesEvents,
  186. addEvents: DerivedSeriesMixin.addEvents,
  187. destroy: DerivedSeriesMixin.destroy,
  188. init: DerivedSeriesMixin.init,
  189. setBaseSeries: DerivedSeriesMixin.setBaseSeries
  190. });
  191. SeriesRegistry.registerSeriesType('bellcurve', BellcurveSeries);
  192. /* *
  193. *
  194. * Default Export
  195. *
  196. * */
  197. export default BellcurveSeries;
  198. /* *
  199. *
  200. * API Options
  201. *
  202. * */
  203. /**
  204. * A `bellcurve` series. If the [type](#series.bellcurve.type) option is not
  205. * specified, it is inherited from [chart.type](#chart.type).
  206. *
  207. * For options that apply to multiple series, it is recommended to add
  208. * them to the [plotOptions.series](#plotOptions.series) options structure.
  209. * To apply to all series of this specific type, apply it to
  210. * [plotOptions.bellcurve](#plotOptions.bellcurve).
  211. *
  212. * @extends series,plotOptions.bellcurve
  213. * @since 6.0.0
  214. * @product highcharts
  215. * @excluding dataParser, dataURL, data, boostThreshold, boostBlending
  216. * @requires modules/bellcurve
  217. * @apioption series.bellcurve
  218. */
  219. /**
  220. * An integer identifying the index to use for the base series, or a string
  221. * representing the id of the series.
  222. *
  223. * @type {number|string}
  224. * @apioption series.bellcurve.baseSeries
  225. */
  226. /**
  227. * @see [fillColor](#series.bellcurve.fillColor)
  228. * @see [fillOpacity](#series.bellcurve.fillOpacity)
  229. *
  230. * @apioption series.bellcurve.color
  231. */
  232. /**
  233. * @see [color](#series.bellcurve.color)
  234. * @see [fillOpacity](#series.bellcurve.fillOpacity)
  235. *
  236. * @apioption series.bellcurve.fillColor
  237. */
  238. /**
  239. * @see [color](#series.bellcurve.color)
  240. * @see [fillColor](#series.bellcurve.fillColor)
  241. *
  242. * @default {highcharts} 0.75
  243. * @default {highstock} 0.75
  244. * @apioption series.bellcurve.fillOpacity
  245. */
  246. ''; // adds doclets above to transpiled file