HistogramSeries.js 6.9 KB

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