histogram-bellcurve.src.js 22 KB

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