histogram-bellcurve.src.js 20 KB

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