LINK
const histogram = (data: any, size: number = 1) => {
const length = data.length;
let min = data[0];
let max = data[1];
data.forEach((item: number) => {
if (item < min) min = item;
else if (item > max) max = item;
});
const bins = Math.ceil((max - min + 1) / size);
const histogram = new Array(bins);
for (let i = 0; i < bins; i++) histogram[i] = 0;
for (let i = 0; i < length; i++)
histogram[Math.floor((data[i] - min) / size)]++;
return histogram;
};
const data = [ 10.15, 15.48, 19.26, 22.49, 0.2, 11.43, 18.31, 5.58, 6.23, 11.04,
12.5, 13.44, 18.34, 8.13, 12.09, 12.1, 15.05, 18.05, 10.32, 14.10,
19.46, 20.06, 11.45, 13.08, 15.11, 19.35, 20.38, 15.14, 15.14, 15.53,
15.53, 19.35, 10.46, 14.12, 18.57, 10.21, 13.07, 13.41, 18.24, 18.25,
19.59, 19.59, 10.32, 15.37, 15.37, 18.55, 12.45, 17.56 ];
const hst = tools.histogram(data, 2);
console.log(JSON.stringify(hst));
// [1,0,1,2,1,9,8,9,2,13,1,1]