histogram-bellcurve.src.js 16 KB

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