histogram-bellcurve.src.js 15 KB

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