histogram-bellcurve.src.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. /**
  2. * @license Highcharts JS v8.2.0 (2020-08-20)
  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/Utilities.js'], _modules['Mixins/DerivedSeries.js']], function (U, derivedSeriesMixin) {
  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. seriesType = U.seriesType;
  172. /* ************************************************************************** *
  173. * HISTOGRAM
  174. * ************************************************************************** */
  175. /**
  176. * A dictionary with formulas for calculating number of bins based on the
  177. * base series
  178. **/
  179. var binsNumberFormulas = {
  180. 'square-root': function (baseSeries) {
  181. return Math.ceil(Math.sqrt(baseSeries.options.data.length));
  182. },
  183. 'sturges': function (baseSeries) {
  184. return Math.ceil(Math.log(baseSeries.options.data.length) * Math.LOG2E);
  185. },
  186. 'rice': function (baseSeries) {
  187. return Math.ceil(2 * Math.pow(baseSeries.options.data.length, 1 / 3));
  188. }
  189. };
  190. /**
  191. * Returns a function for mapping number to the closed (right opened) bins
  192. * @private
  193. * @param {Array<number>} bins - Width of the bins
  194. * @return {Function}
  195. **/
  196. function fitToBinLeftClosed(bins) {
  197. return function (y) {
  198. var i = 1;
  199. while (bins[i] <= y) {
  200. i++;
  201. }
  202. return bins[--i];
  203. };
  204. }
  205. /**
  206. * Histogram class
  207. * @private
  208. * @class
  209. * @name Highcharts.seriesTypes.histogram
  210. * @augments Highcharts.Series
  211. */
  212. seriesType('histogram', 'column',
  213. /**
  214. * A histogram is a column series which represents the distribution of the
  215. * data set in the base series. Histogram splits data into bins and shows
  216. * their frequencies.
  217. *
  218. * @sample {highcharts} highcharts/demo/histogram/
  219. * Histogram
  220. *
  221. * @extends plotOptions.column
  222. * @excluding boostThreshold, dragDrop, pointInterval, pointIntervalUnit,
  223. * stacking, boostBlending
  224. * @product highcharts
  225. * @since 6.0.0
  226. * @requires modules/histogram
  227. * @optionparent plotOptions.histogram
  228. */
  229. {
  230. /**
  231. * A preferable number of bins. It is a suggestion, so a histogram may
  232. * have a different number of bins. By default it is set to the square
  233. * root of the base series' data length. Available options are:
  234. * `square-root`, `sturges`, `rice`. You can also define a function
  235. * which takes a `baseSeries` as a parameter and should return a
  236. * positive integer.
  237. *
  238. * @type {"square-root"|"sturges"|"rice"|number|function}
  239. */
  240. binsNumber: 'square-root',
  241. /**
  242. * Width of each bin. By default the bin's width is calculated as
  243. * `(max - min) / number of bins`. This option takes precedence over
  244. * [binsNumber](#plotOptions.histogram.binsNumber).
  245. *
  246. * @type {number}
  247. */
  248. binWidth: void 0,
  249. pointPadding: 0,
  250. groupPadding: 0,
  251. grouping: false,
  252. pointPlacement: 'between',
  253. tooltip: {
  254. headerFormat: '',
  255. pointFormat: ('<span style="font-size: 10px">{point.x} - {point.x2}' +
  256. '</span><br/>' +
  257. '<span style="color:{point.color}">\u25CF</span>' +
  258. ' {series.name} <b>{point.y}</b><br/>')
  259. }
  260. }, merge(derivedSeriesMixin, {
  261. setDerivedData: function () {
  262. var yData = this.baseSeries.yData;
  263. if (!yData.length) {
  264. return;
  265. }
  266. var data = this.derivedData(yData,
  267. this.binsNumber(),
  268. this.options.binWidth);
  269. this.setData(data, false);
  270. },
  271. derivedData: function (baseData, binsNumber, binWidth) {
  272. var series = this,
  273. max = arrayMax(baseData),
  274. // Float correction needed, because first frequency value is not
  275. // corrected when generating frequencies (within for loop).
  276. min = correctFloat(arrayMin(baseData)),
  277. frequencies = [],
  278. bins = {},
  279. data = [],
  280. x,
  281. fitToBin;
  282. binWidth = series.binWidth = (correctFloat(isNumber(binWidth) ?
  283. (binWidth || 1) :
  284. (max - min) / binsNumber));
  285. // #12077 negative pointRange causes wrong calculations,
  286. // browser hanging.
  287. series.options.pointRange = Math.max(binWidth, 0);
  288. // If binWidth is 0 then max and min are equaled,
  289. // increment the x with some positive value to quit the loop
  290. for (x = min;
  291. // This condition is needed because of the margin of error while
  292. // operating on decimal numbers. Without that, additional bin
  293. // was sometimes noticeable on the graph, because of too small
  294. // precision of float correction.
  295. x < max &&
  296. (series.userOptions.binWidth ||
  297. correctFloat(max - x) >= binWidth ||
  298. // #13069 - Every add and subtract operation should
  299. // be corrected, due to general problems with
  300. // operations on float numbers in JS.
  301. correctFloat(correctFloat(min + (frequencies.length * binWidth)) -
  302. x) <= 0); x = correctFloat(x + binWidth)) {
  303. frequencies.push(x);
  304. bins[x] = 0;
  305. }
  306. if (bins[min] !== 0) {
  307. frequencies.push(correctFloat(min));
  308. bins[correctFloat(min)] = 0;
  309. }
  310. fitToBin = fitToBinLeftClosed(frequencies.map(function (elem) {
  311. return parseFloat(elem);
  312. }));
  313. baseData.forEach(function (y) {
  314. var x = correctFloat(fitToBin(y));
  315. bins[x]++;
  316. });
  317. objectEach(bins, function (frequency, x) {
  318. data.push({
  319. x: Number(x),
  320. y: frequency,
  321. x2: correctFloat(Number(x) + binWidth)
  322. });
  323. });
  324. data.sort(function (a, b) {
  325. return a.x - b.x;
  326. });
  327. return data;
  328. },
  329. binsNumber: function () {
  330. var binsNumberOption = this.options.binsNumber;
  331. var binsNumber = binsNumberFormulas[binsNumberOption] ||
  332. // #7457
  333. (typeof binsNumberOption === 'function' && binsNumberOption);
  334. return Math.ceil((binsNumber && binsNumber(this.baseSeries)) ||
  335. (isNumber(binsNumberOption) ?
  336. binsNumberOption :
  337. binsNumberFormulas['square-root'](this.baseSeries)));
  338. }
  339. }));
  340. /**
  341. * A `histogram` series. If the [type](#series.histogram.type) option is not
  342. * specified, it is inherited from [chart.type](#chart.type).
  343. *
  344. * @extends series,plotOptions.histogram
  345. * @excluding data, dataParser, dataURL, boostThreshold, boostBlending
  346. * @product highcharts
  347. * @since 6.0.0
  348. * @requires modules/histogram
  349. * @apioption series.histogram
  350. */
  351. /**
  352. * An integer identifying the index to use for the base series, or a string
  353. * representing the id of the series.
  354. *
  355. * @type {number|string}
  356. * @apioption series.histogram.baseSeries
  357. */
  358. ''; // adds doclets above to transpiled file
  359. });
  360. _registerModule(_modules, 'Series/BellcurveSeries.js', [_modules['Core/Utilities.js'], _modules['Mixins/DerivedSeries.js']], function (U, derivedSeriesMixin) {
  361. /* *
  362. *
  363. * (c) 2010-2020 Highsoft AS
  364. *
  365. * Author: Sebastian Domas
  366. *
  367. * License: www.highcharts.com/license
  368. *
  369. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  370. *
  371. * */
  372. var correctFloat = U.correctFloat,
  373. isNumber = U.isNumber,
  374. merge = U.merge,
  375. seriesType = U.seriesType;
  376. /* ************************************************************************** *
  377. * BELL CURVE *
  378. * ************************************************************************** */
  379. /* eslint-disable valid-jsdoc */
  380. /**
  381. * @private
  382. */
  383. function mean(data) {
  384. var length = data.length,
  385. sum = data.reduce(function (sum,
  386. value) {
  387. return (sum += value);
  388. }, 0);
  389. return length > 0 && sum / length;
  390. }
  391. /**
  392. * @private
  393. */
  394. function standardDeviation(data, average) {
  395. var len = data.length,
  396. sum;
  397. average = isNumber(average) ? average : mean(data);
  398. sum = data.reduce(function (sum, value) {
  399. var diff = value - average;
  400. return (sum += diff * diff);
  401. }, 0);
  402. return len > 1 && Math.sqrt(sum / (len - 1));
  403. }
  404. /**
  405. * @private
  406. */
  407. function normalDensity(x, mean, standardDeviation) {
  408. var translation = x - mean;
  409. return Math.exp(-(translation * translation) /
  410. (2 * standardDeviation * standardDeviation)) / (standardDeviation * Math.sqrt(2 * Math.PI));
  411. }
  412. /* eslint-enable valid-jsdoc */
  413. /**
  414. * Bell curve class
  415. *
  416. * @private
  417. * @class
  418. * @name Highcharts.seriesTypes.bellcurve
  419. *
  420. * @augments Highcharts.Series
  421. */
  422. seriesType('bellcurve', 'areaspline'
  423. /**
  424. * A bell curve is an areaspline series which represents the probability
  425. * density function of the normal distribution. It calculates mean and
  426. * standard deviation of the base series data and plots the curve according
  427. * to the calculated parameters.
  428. *
  429. * @sample {highcharts} highcharts/demo/bellcurve/
  430. * Bell curve
  431. *
  432. * @extends plotOptions.areaspline
  433. * @since 6.0.0
  434. * @product highcharts
  435. * @excluding boostThreshold, connectNulls, dragDrop, stacking,
  436. * pointInterval, pointIntervalUnit
  437. * @requires modules/bellcurve
  438. * @optionparent plotOptions.bellcurve
  439. */
  440. , {
  441. /**
  442. * This option allows to define the length of the bell curve. A unit of
  443. * the length of the bell curve is standard deviation.
  444. *
  445. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  446. * Intervals and points in interval
  447. */
  448. intervals: 3,
  449. /**
  450. * Defines how many points should be plotted within 1 interval. See
  451. * `plotOptions.bellcurve.intervals`.
  452. *
  453. * @sample highcharts/plotoptions/bellcurve-intervals-pointsininterval
  454. * Intervals and points in interval
  455. */
  456. pointsInInterval: 3,
  457. marker: {
  458. enabled: false
  459. }
  460. }, merge(derivedSeriesMixin, {
  461. setMean: function () {
  462. this.mean = correctFloat(mean(this.baseSeries.yData));
  463. },
  464. setStandardDeviation: function () {
  465. this.standardDeviation = correctFloat(standardDeviation(this.baseSeries.yData, this.mean));
  466. },
  467. setDerivedData: function () {
  468. if (this.baseSeries.yData.length > 1) {
  469. this.setMean();
  470. this.setStandardDeviation();
  471. this.setData(this.derivedData(this.mean, this.standardDeviation), false);
  472. }
  473. return (void 0);
  474. },
  475. derivedData: function (mean, standardDeviation) {
  476. var intervals = this.options.intervals,
  477. pointsInInterval = this.options.pointsInInterval,
  478. x = mean - intervals * standardDeviation,
  479. stop = intervals * pointsInInterval * 2 + 1,
  480. increment = standardDeviation / pointsInInterval,
  481. data = [],
  482. i;
  483. for (i = 0; i < stop; i++) {
  484. data.push([x, normalDensity(x, mean, standardDeviation)]);
  485. x += increment;
  486. }
  487. return data;
  488. }
  489. }));
  490. /**
  491. * A `bellcurve` series. If the [type](#series.bellcurve.type) option is not
  492. * specified, it is inherited from [chart.type](#chart.type).
  493. *
  494. * For options that apply to multiple series, it is recommended to add
  495. * them to the [plotOptions.series](#plotOptions.series) options structure.
  496. * To apply to all series of this specific type, apply it to
  497. * [plotOptions.bellcurve](#plotOptions.bellcurve).
  498. *
  499. * @extends series,plotOptions.bellcurve
  500. * @since 6.0.0
  501. * @product highcharts
  502. * @excluding dataParser, dataURL, data, boostThreshold, boostBlending
  503. * @requires modules/bellcurve
  504. * @apioption series.bellcurve
  505. */
  506. /**
  507. * An integer identifying the index to use for the base series, or a string
  508. * representing the id of the series.
  509. *
  510. * @type {number|string}
  511. * @apioption series.bellcurve.baseSeries
  512. */
  513. ''; // adds doclets above to transpiled file
  514. });
  515. _registerModule(_modules, 'masters/modules/histogram-bellcurve.src.js', [], function () {
  516. });
  517. }));