HistogramSeries.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* *
  2. *
  3. * Copyright (c) 2010-2017 Highsoft AS
  4. * Author: Sebastian Domas
  5. *
  6. * License: www.highcharts.com/license
  7. *
  8. * !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
  9. *
  10. * */
  11. 'use strict';
  12. import U from '../Core/Utilities.js';
  13. var arrayMax = U.arrayMax, arrayMin = U.arrayMin, correctFloat = U.correctFloat, isNumber = U.isNumber, merge = U.merge, objectEach = U.objectEach, seriesType = U.seriesType;
  14. import derivedSeriesMixin from '../Mixins/DerivedSeries.js';
  15. /* ************************************************************************** *
  16. * HISTOGRAM
  17. * ************************************************************************** */
  18. /**
  19. * A dictionary with formulas for calculating number of bins based on the
  20. * base series
  21. **/
  22. var binsNumberFormulas = {
  23. 'square-root': function (baseSeries) {
  24. return Math.ceil(Math.sqrt(baseSeries.options.data.length));
  25. },
  26. 'sturges': function (baseSeries) {
  27. return Math.ceil(Math.log(baseSeries.options.data.length) * Math.LOG2E);
  28. },
  29. 'rice': function (baseSeries) {
  30. return Math.ceil(2 * Math.pow(baseSeries.options.data.length, 1 / 3));
  31. }
  32. };
  33. /**
  34. * Returns a function for mapping number to the closed (right opened) bins
  35. * @private
  36. * @param {Array<number>} bins - Width of the bins
  37. * @return {Function}
  38. **/
  39. function fitToBinLeftClosed(bins) {
  40. return function (y) {
  41. var i = 1;
  42. while (bins[i] <= y) {
  43. i++;
  44. }
  45. return bins[--i];
  46. };
  47. }
  48. /**
  49. * Histogram class
  50. * @private
  51. * @class
  52. * @name Highcharts.seriesTypes.histogram
  53. * @augments Highcharts.Series
  54. */
  55. seriesType('histogram', 'column',
  56. /**
  57. * A histogram is a column series which represents the distribution of the
  58. * data set in the base series. Histogram splits data into bins and shows
  59. * their frequencies.
  60. *
  61. * @sample {highcharts} highcharts/demo/histogram/
  62. * Histogram
  63. *
  64. * @extends plotOptions.column
  65. * @excluding boostThreshold, dragDrop, pointInterval, pointIntervalUnit,
  66. * stacking, boostBlending
  67. * @product highcharts
  68. * @since 6.0.0
  69. * @requires modules/histogram
  70. * @optionparent plotOptions.histogram
  71. */
  72. {
  73. /**
  74. * A preferable number of bins. It is a suggestion, so a histogram may
  75. * have a different number of bins. By default it is set to the square
  76. * root of the base series' data length. Available options are:
  77. * `square-root`, `sturges`, `rice`. You can also define a function
  78. * which takes a `baseSeries` as a parameter and should return a
  79. * positive integer.
  80. *
  81. * @type {"square-root"|"sturges"|"rice"|number|function}
  82. */
  83. binsNumber: 'square-root',
  84. /**
  85. * Width of each bin. By default the bin's width is calculated as
  86. * `(max - min) / number of bins`. This option takes precedence over
  87. * [binsNumber](#plotOptions.histogram.binsNumber).
  88. *
  89. * @type {number}
  90. */
  91. binWidth: void 0,
  92. pointPadding: 0,
  93. groupPadding: 0,
  94. grouping: false,
  95. pointPlacement: 'between',
  96. tooltip: {
  97. headerFormat: '',
  98. pointFormat: ('<span style="font-size: 10px">{point.x} - {point.x2}' +
  99. '</span><br/>' +
  100. '<span style="color:{point.color}">\u25CF</span>' +
  101. ' {series.name} <b>{point.y}</b><br/>')
  102. }
  103. }, merge(derivedSeriesMixin, {
  104. setDerivedData: function () {
  105. var yData = this.baseSeries.yData;
  106. if (!yData.length) {
  107. return;
  108. }
  109. var data = this.derivedData(yData, this.binsNumber(), this.options.binWidth);
  110. this.setData(data, false);
  111. },
  112. derivedData: function (baseData, binsNumber, binWidth) {
  113. var series = this, max = arrayMax(baseData),
  114. // Float correction needed, because first frequency value is not
  115. // corrected when generating frequencies (within for loop).
  116. min = correctFloat(arrayMin(baseData)), frequencies = [], bins = {}, data = [], x, fitToBin;
  117. binWidth = series.binWidth = (correctFloat(isNumber(binWidth) ?
  118. (binWidth || 1) :
  119. (max - min) / binsNumber));
  120. // #12077 negative pointRange causes wrong calculations,
  121. // browser hanging.
  122. series.options.pointRange = Math.max(binWidth, 0);
  123. // If binWidth is 0 then max and min are equaled,
  124. // increment the x with some positive value to quit the loop
  125. for (x = min;
  126. // This condition is needed because of the margin of error while
  127. // operating on decimal numbers. Without that, additional bin
  128. // was sometimes noticeable on the graph, because of too small
  129. // precision of float correction.
  130. x < max &&
  131. (series.userOptions.binWidth ||
  132. correctFloat(max - x) >= binWidth ||
  133. // #13069 - Every add and subtract operation should
  134. // be corrected, due to general problems with
  135. // operations on float numbers in JS.
  136. correctFloat(correctFloat(min + (frequencies.length * binWidth)) -
  137. x) <= 0); x = correctFloat(x + binWidth)) {
  138. frequencies.push(x);
  139. bins[x] = 0;
  140. }
  141. if (bins[min] !== 0) {
  142. frequencies.push(correctFloat(min));
  143. bins[correctFloat(min)] = 0;
  144. }
  145. fitToBin = fitToBinLeftClosed(frequencies.map(function (elem) {
  146. return parseFloat(elem);
  147. }));
  148. baseData.forEach(function (y) {
  149. var x = correctFloat(fitToBin(y));
  150. bins[x]++;
  151. });
  152. objectEach(bins, function (frequency, x) {
  153. data.push({
  154. x: Number(x),
  155. y: frequency,
  156. x2: correctFloat(Number(x) + binWidth)
  157. });
  158. });
  159. data.sort(function (a, b) {
  160. return a.x - b.x;
  161. });
  162. return data;
  163. },
  164. binsNumber: function () {
  165. var binsNumberOption = this.options.binsNumber;
  166. var binsNumber = binsNumberFormulas[binsNumberOption] ||
  167. // #7457
  168. (typeof binsNumberOption === 'function' && binsNumberOption);
  169. return Math.ceil((binsNumber && binsNumber(this.baseSeries)) ||
  170. (isNumber(binsNumberOption) ?
  171. binsNumberOption :
  172. binsNumberFormulas['square-root'](this.baseSeries)));
  173. }
  174. }));
  175. /**
  176. * A `histogram` series. If the [type](#series.histogram.type) option is not
  177. * specified, it is inherited from [chart.type](#chart.type).
  178. *
  179. * @extends series,plotOptions.histogram
  180. * @excluding data, dataParser, dataURL, boostThreshold, boostBlending
  181. * @product highcharts
  182. * @since 6.0.0
  183. * @requires modules/histogram
  184. * @apioption series.histogram
  185. */
  186. /**
  187. * An integer identifying the index to use for the base series, or a string
  188. * representing the id of the series.
  189. *
  190. * @type {number|string}
  191. * @apioption series.histogram.baseSeries
  192. */
  193. ''; // adds doclets above to transpiled file