histogram-bellcurve.src.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /**
  2. * @license Highcharts JS v8.1.2 (2020-06-16)
  3. *
  4. * (c) 2010-2019 Highsoft AS
  5. * Author: Sebastian Domas
  6. *
  7. * License: www.highcharts.com/license
  8. */
  9. 'use strict';
  10. (function (factory) {
  11. if (typeof module === 'object' && module.exports) {
  12. factory['default'] = factory;
  13. module.exports = factory;
  14. } else if (typeof define === 'function' && define.amd) {
  15. define('highcharts/modules/histogram-bellcurve', ['highcharts'], function (Highcharts) {
  16. factory(Highcharts);
  17. factory.Highcharts = Highcharts;
  18. return factory;
  19. });
  20. } else {
  21. factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
  22. }
  23. }(function (Highcharts) {
  24. var _modules = Highcharts ? Highcharts._modules : {};
  25. function _registerModule(obj, path, args, fn) {
  26. if (!obj.hasOwnProperty(path)) {
  27. obj[path] = fn.apply(null, args);
  28. }
  29. }
  30. _registerModule(_modules, 'mixins/derived-series.js', [_modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (H, U) {
  31. /* *
  32. *
  33. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  34. *
  35. * */
  36. var addEvent = U.addEvent, defined = U.defined;
  37. var Series = H.Series, noop = H.noop;
  38. /* ************************************************************************** *
  39. *
  40. * DERIVED SERIES MIXIN
  41. *
  42. * ************************************************************************** */
  43. /**
  44. * Provides methods for auto setting/updating series data based on the based
  45. * series data.
  46. *
  47. * @private
  48. * @mixin derivedSeriesMixin
  49. */
  50. var derivedSeriesMixin = {
  51. hasDerivedData: true,
  52. /* eslint-disable valid-jsdoc */
  53. /**
  54. * Initialise series
  55. *
  56. * @private
  57. * @function derivedSeriesMixin.init
  58. * @return {void}
  59. */
  60. init: function () {
  61. Series.prototype.init.apply(this, arguments);
  62. this.initialised = false;
  63. this.baseSeries = null;
  64. this.eventRemovers = [];
  65. this.addEvents();
  66. },
  67. /**
  68. * Method to be implemented - inside the method the series has already
  69. * access to the base series via m `this.baseSeries` and the bases data is
  70. * initialised. It should return data in the format accepted by
  71. * `Series.setData()` method
  72. *
  73. * @private
  74. * @function derivedSeriesMixin.setDerivedData
  75. * @return {Array<Highcharts.PointOptionsType>}
  76. * An array of data
  77. */
  78. setDerivedData: noop,
  79. /**
  80. * Sets base series for the series
  81. *
  82. * @private
  83. * @function derivedSeriesMixin.setBaseSeries
  84. * @return {void}
  85. */
  86. setBaseSeries: function () {
  87. var chart = this.chart, baseSeriesOptions = this.options.baseSeries, baseSeries = (defined(baseSeriesOptions) &&
  88. (chart.series[baseSeriesOptions] ||
  89. chart.get(baseSeriesOptions)));
  90. this.baseSeries = baseSeries || null;
  91. },
  92. /**
  93. * Adds events for the series
  94. *
  95. * @private
  96. * @function derivedSeriesMixin.addEvents
  97. * @return {void}
  98. */
  99. addEvents: function () {
  100. var derivedSeries = this, chartSeriesLinked;
  101. chartSeriesLinked = addEvent(this.chart, 'afterLinkSeries', function () {
  102. derivedSeries.setBaseSeries();
  103. if (derivedSeries.baseSeries && !derivedSeries.initialised) {
  104. derivedSeries.setDerivedData();
  105. derivedSeries.addBaseSeriesEvents();
  106. derivedSeries.initialised = true;
  107. }
  108. });
  109. this.eventRemovers.push(chartSeriesLinked);
  110. },
  111. /**
  112. * Adds events to the base series - it required for recalculating the data
  113. * in the series if the base series is updated / removed / etc.
  114. *
  115. * @private
  116. * @function derivedSeriesMixin.addBaseSeriesEvents
  117. * @return {void}
  118. */
  119. addBaseSeriesEvents: function () {
  120. var derivedSeries = this, updatedDataRemover, destroyRemover;
  121. updatedDataRemover = addEvent(derivedSeries.baseSeries, 'updatedData', function () {
  122. derivedSeries.setDerivedData();
  123. });
  124. destroyRemover = addEvent(derivedSeries.baseSeries, 'destroy', function () {
  125. derivedSeries.baseSeries = null;
  126. derivedSeries.initialised = false;
  127. });
  128. derivedSeries.eventRemovers.push(updatedDataRemover, destroyRemover);
  129. },
  130. /**
  131. * Destroys the series
  132. *
  133. * @private
  134. * @function derivedSeriesMixin.destroy
  135. */
  136. destroy: function () {
  137. this.eventRemovers.forEach(function (remover) {
  138. remover();
  139. });
  140. Series.prototype.destroy.apply(this, arguments);
  141. }
  142. /* eslint-disable valid-jsdoc */
  143. };
  144. return derivedSeriesMixin;
  145. });
  146. _registerModule(_modules, 'modules/histogram.src.js', [_modules['parts/Utilities.js'], _modules['mixins/derived-series.js']], function (U, derivedSeriesMixin) {
  147. /* *
  148. *
  149. * Copyright (c) 2010-2017 Highsoft AS
  150. * Author: Sebastian Domas
  151. *
  152. * License: www.highcharts.com/license
  153. *
  154. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  155. *
  156. * */
  157. var arrayMax = U.arrayMax, arrayMin = U.arrayMin, correctFloat = U.correctFloat, isNumber = U.isNumber, merge = U.merge, objectEach = U.objectEach, seriesType = U.seriesType;
  158. /* ************************************************************************** *
  159. * HISTOGRAM
  160. * ************************************************************************** */
  161. /**
  162. * A dictionary with formulas for calculating number of bins based on the
  163. * base series
  164. **/
  165. var binsNumberFormulas = {
  166. 'square-root': function (baseSeries) {
  167. return Math.ceil(Math.sqrt(baseSeries.options.data.length));
  168. },
  169. 'sturges': function (baseSeries) {
  170. return Math.ceil(Math.log(baseSeries.options.data.length) * Math.LOG2E);
  171. },
  172. 'rice': function (baseSeries) {
  173. return Math.ceil(2 * Math.pow(baseSeries.options.data.length, 1 / 3));
  174. }
  175. };
  176. /**
  177. * Returns a function for mapping number to the closed (right opened) bins
  178. * @private
  179. * @param {Array<number>} bins - Width of the bins
  180. * @return {Function}
  181. **/
  182. function fitToBinLeftClosed(bins) {
  183. return function (y) {
  184. var i = 1;
  185. while (bins[i] <= y) {
  186. i++;
  187. }
  188. return bins[--i];
  189. };
  190. }
  191. /**
  192. * Histogram class
  193. * @private
  194. * @class
  195. * @name Highcharts.seriesTypes.histogram
  196. * @augments Highcharts.Series
  197. */
  198. seriesType('histogram', 'column',
  199. /**
  200. * A histogram is a column series which represents the distribution of the
  201. * data set in the base series. Histogram splits data into bins and shows
  202. * their frequencies.
  203. *
  204. * @sample {highcharts} highcharts/demo/histogram/
  205. * Histogram
  206. *
  207. * @extends plotOptions.column
  208. * @excluding boostThreshold, dragDrop, pointInterval, pointIntervalUnit,
  209. * stacking
  210. * @product highcharts
  211. * @since 6.0.0
  212. * @requires modules/histogram
  213. * @optionparent plotOptions.histogram
  214. */
  215. {
  216. /**
  217. * A preferable number of bins. It is a suggestion, so a histogram may
  218. * have a different number of bins. By default it is set to the square
  219. * root of the base series' data length. Available options are:
  220. * `square-root`, `sturges`, `rice`. You can also define a function
  221. * which takes a `baseSeries` as a parameter and should return a
  222. * positive integer.
  223. *
  224. * @type {"square-root"|"sturges"|"rice"|number|function}
  225. */
  226. binsNumber: 'square-root',
  227. /**
  228. * Width of each bin. By default the bin's width is calculated as
  229. * `(max - min) / number of bins`. This option takes precedence over
  230. * [binsNumber](#plotOptions.histogram.binsNumber).
  231. *
  232. * @type {number}
  233. */
  234. binWidth: void 0,
  235. pointPadding: 0,
  236. groupPadding: 0,
  237. grouping: false,
  238. pointPlacement: 'between',
  239. tooltip: {
  240. headerFormat: '',
  241. pointFormat: ('<span style="font-size: 10px">{point.x} - {point.x2}' +
  242. '</span><br/>' +
  243. '<span style="color:{point.color}">\u25CF</span>' +
  244. ' {series.name} <b>{point.y}</b><br/>')
  245. }
  246. }, merge(derivedSeriesMixin, {
  247. setDerivedData: function () {
  248. var yData = this.baseSeries.yData;
  249. if (!yData.length) {
  250. return;
  251. }
  252. var data = this.derivedData(yData, this.binsNumber(), this.options.binWidth);
  253. this.setData(data, false);
  254. },
  255. derivedData: function (baseData, binsNumber, binWidth) {
  256. var series = this, max = arrayMax(baseData),
  257. // Float correction needed, because first frequency value is not
  258. // corrected when generating frequencies (within for loop).
  259. min = correctFloat(arrayMin(baseData)), frequencies = [], bins = {}, data = [], x, fitToBin;
  260. binWidth = series.binWidth = (correctFloat(isNumber(binWidth) ?
  261. (binWidth || 1) :
  262. (max - min) / binsNumber));
  263. // #12077 negative pointRange causes wrong calculations,
  264. // browser hanging.
  265. series.options.pointRange = Math.max(binWidth, 0);
  266. // If binWidth is 0 then max and min are equaled,
  267. // increment the x with some positive value to quit the loop
  268. for (x = min;
  269. // This condition is needed because of the margin of error while
  270. // operating on decimal numbers. Without that, additional bin
  271. // was sometimes noticeable on the graph, because of too small
  272. // precision of float correction.
  273. x < max &&
  274. (series.userOptions.binWidth ||
  275. correctFloat(max - x) >= binWidth ||
  276. // #13069 - Every add and subtract operation should
  277. // be corrected, due to general problems with
  278. // operations on float numbers in JS.
  279. correctFloat(correctFloat(min + (frequencies.length * binWidth)) -
  280. x) <= 0); x = correctFloat(x + binWidth)) {
  281. frequencies.push(x);
  282. bins[x] = 0;
  283. }
  284. if (bins[min] !== 0) {
  285. frequencies.push(correctFloat(min));
  286. bins[correctFloat(min)] = 0;
  287. }
  288. fitToBin = fitToBinLeftClosed(frequencies.map(function (elem) {
  289. return parseFloat(elem);
  290. }));
  291. baseData.forEach(function (y) {
  292. var x = correctFloat(fitToBin(y));
  293. bins[x]++;
  294. });
  295. objectEach(bins, function (frequency, x) {
  296. data.push({
  297. x: Number(x),
  298. y: frequency,
  299. x2: correctFloat(Number(x) + binWidth)
  300. });
  301. });
  302. data.sort(function (a, b) {
  303. return a.x - b.x;
  304. });
  305. return data;
  306. },
  307. binsNumber: function () {
  308. var binsNumberOption = this.options.binsNumber;
  309. var binsNumber = binsNumberFormulas[binsNumberOption] ||
  310. // #7457
  311. (typeof binsNumberOption === 'function' && binsNumberOption);
  312. return Math.ceil((binsNumber && binsNumber(this.baseSeries)) ||
  313. (isNumber(binsNumberOption) ?
  314. binsNumberOption :
  315. binsNumberFormulas['square-root'](this.baseSeries)));
  316. }
  317. }));
  318. /**
  319. * A `histogram` series. If the [type](#series.histogram.type) option is not
  320. * specified, it is inherited from [chart.type](#chart.type).
  321. *
  322. * @extends series,plotOptions.histogram
  323. * @excluding data, dataParser, dataURL
  324. * @product highcharts
  325. * @since 6.0.0
  326. * @requires modules/histogram
  327. * @apioption series.histogram
  328. */
  329. /**
  330. * An integer identifying the index to use for the base series, or a string
  331. * representing the id of the series.
  332. *
  333. * @type {number|string}
  334. * @apioption series.histogram.baseSeries
  335. */
  336. ''; // adds doclets above to transpiled file
  337. });
  338. _registerModule(_modules, 'modules/bellcurve.src.js', [_modules['parts/Utilities.js'], _modules['mixins/derived-series.js']], function (U, derivedSeriesMixin) {
  339. /* *
  340. *
  341. * (c) 2010-2020 Highsoft AS
  342. *
  343. * Author: Sebastian Domas
  344. *
  345. * License: www.highcharts.com/license
  346. *
  347. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  348. *
  349. * */
  350. var correctFloat = U.correctFloat, isNumber = U.isNumber, merge = U.merge, seriesType = U.seriesType;
  351. /* ************************************************************************** *
  352. * BELL CURVE *
  353. * ************************************************************************** */
  354. /* eslint-disable valid-jsdoc */
  355. /**
  356. * @private
  357. */
  358. function mean(data) {
  359. var length = data.length, sum = data.reduce(function (sum, value) {
  360. return (sum += value);
  361. }, 0);
  362. return length > 0 && sum / length;
  363. }
  364. /**
  365. * @private
  366. */
  367. function standardDeviation(data, average) {
  368. var len = data.length, sum;
  369. average = isNumber(average) ? average : mean(data);
  370. sum = data.reduce(function (sum, value) {
  371. var diff = value - average;
  372. return (sum += diff * diff);
  373. }, 0);
  374. return len > 1 && Math.sqrt(sum / (len - 1));
  375. }
  376. /**
  377. * @private
  378. */
  379. function normalDensity(x, mean, standardDeviation) {
  380. var translation = x - mean;
  381. return Math.exp(-(translation * translation) /
  382. (2 * standardDeviation * standardDeviation)) / (standardDeviation * Math.sqrt(2 * Math.PI));
  383. }
  384. /* eslint-enable valid-jsdoc */
  385. /**
  386. * Bell curve class
  387. *
  388. * @private
  389. * @class
  390. * @name Highcharts.seriesTypes.bellcurve
  391. *
  392. * @augments Highcharts.Series
  393. */
  394. seriesType('bellcurve', 'areaspline'
  395. /**
  396. * A bell curve is an areaspline series which represents the probability
  397. * density function of the normal distribution. It calculates mean and
  398. * standard deviation of the base series data and plots the curve according
  399. * to the calculated parameters.
  400. *
  401. * @sample {highcharts} highcharts/demo/bellcurve/
  402. * Bell curve
  403. *
  404. * @extends plotOptions.areaspline
  405. * @since 6.0.0
  406. * @product highcharts
  407. * @excluding boostThreshold, connectNulls, dragDrop, stacking,
  408. * pointInterval, pointIntervalUnit
  409. * @requires modules/bellcurve
  410. * @optionparent plotOptions.bellcurve
  411. */
  412. , {
  413. /**
  414. * This option allows to define the length of the bell curve. A unit of
  415. * the length of the bell curve is standard deviation.
  416. *
  417. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  418. * Intervals and points in interval
  419. */
  420. intervals: 3,
  421. /**
  422. * Defines how many points should be plotted within 1 interval. See
  423. * `plotOptions.bellcurve.intervals`.
  424. *
  425. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  426. * Intervals and points in interval
  427. */
  428. pointsInInterval: 3,
  429. marker: {
  430. enabled: false
  431. }
  432. }, merge(derivedSeriesMixin, {
  433. setMean: function () {
  434. this.mean = correctFloat(mean(this.baseSeries.yData));
  435. },
  436. setStandardDeviation: function () {
  437. this.standardDeviation = correctFloat(standardDeviation(this.baseSeries.yData, this.mean));
  438. },
  439. setDerivedData: function () {
  440. if (this.baseSeries.yData.length > 1) {
  441. this.setMean();
  442. this.setStandardDeviation();
  443. this.setData(this.derivedData(this.mean, this.standardDeviation), false);
  444. }
  445. return (void 0);
  446. },
  447. derivedData: function (mean, standardDeviation) {
  448. var intervals = this.options.intervals, pointsInInterval = this.options.pointsInInterval, x = mean - intervals * standardDeviation, stop = intervals * pointsInInterval * 2 + 1, increment = standardDeviation / pointsInInterval, data = [], i;
  449. for (i = 0; i < stop; i++) {
  450. data.push([x, normalDensity(x, mean, standardDeviation)]);
  451. x += increment;
  452. }
  453. return data;
  454. }
  455. }));
  456. /**
  457. * A `bellcurve` series. If the [type](#series.bellcurve.type) option is not
  458. * specified, it is inherited from [chart.type](#chart.type).
  459. *
  460. * For options that apply to multiple series, it is recommended to add
  461. * them to the [plotOptions.series](#plotOptions.series) options structure.
  462. * To apply to all series of this specific type, apply it to
  463. * [plotOptions.bellcurve](#plotOptions.bellcurve).
  464. *
  465. * @extends series,plotOptions.bellcurve
  466. * @since 6.0.0
  467. * @product highcharts
  468. * @excluding dataParser, dataURL, data
  469. * @requires modules/bellcurve
  470. * @apioption series.bellcurve
  471. */
  472. /**
  473. * An integer identifying the index to use for the base series, or a string
  474. * representing the id of the series.
  475. *
  476. * @type {number|string}
  477. * @apioption series.bellcurve.baseSeries
  478. */
  479. ''; // adds doclets above to transpiled file
  480. });
  481. _registerModule(_modules, 'masters/modules/histogram-bellcurve.src.js', [], function () {
  482. });
  483. }));