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