histogram.src.js 6.5 KB

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