From 067edbcacf712451de465f0f8eac7037e14a02ff Mon Sep 17 00:00:00 2001 From: "Friska (Dendi) Pambudi" Date: Thu, 11 Jul 2019 12:49:42 +1000 Subject: [PATCH 1/2] fixed condition when numBins is string --- dist/index.js | 2 +- lib/index.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 54c9499..11aeeb5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -55,7 +55,7 @@ function calculateHistogram(arr) { dataCopy.forEach(function (item) { var binIndex = Math.floor((item - min) / binSize); // for values that lie exactly on last bin we need to subtract one - if (binIndex === numBins) { + if (binIndex === numBins || JSON.stringify(binIndex) === numBins) { binIndex--; } bins[binIndex][1]++; diff --git a/lib/index.js b/lib/index.js index 91ca35b..67bb89e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -47,7 +47,7 @@ function calculateHistogram(arr, numBins = 0, trimTailPercentage = 0.00) { dataCopy.forEach(item => { let binIndex = Math.floor((item - min) / binSize); // for values that lie exactly on last bin we need to subtract one - if (binIndex === numBins) { + if (binIndex === numBins || JSON.stringify(binIndex) === numBins) { binIndex--; } bins[binIndex][1]++; From a95de393d65063bf36d46332168f21a0785ec0a7 Mon Sep 17 00:00:00 2001 From: "Friska (Dendi) Pambudi" Date: Mon, 15 Jul 2019 11:18:56 +1000 Subject: [PATCH 2/2] Add minimum and maximum input parameters --- dist/index.js | 6 ++++++ lib/index.js | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index 11aeeb5..a5bb02e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -17,6 +17,8 @@ var computeIQR = require('compute-iqr'); function calculateHistogram(arr) { var numBins = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; var trimTailPercentage = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0.00; + var minimum = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; + var maximum = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : null; var bins = []; @@ -34,6 +36,10 @@ function calculateHistogram(arr) { var min = dataCopy[0]; var max = dataCopy[dataCopy.length - 1]; + if (!(minimum === null || maximum === null)) { + min = minimum; + max = maximum; + } if (numBins === 0) { var sturges = Math.ceil(Math.log2(dataCopy.length)) + 1; diff --git a/lib/index.js b/lib/index.js index 67bb89e..96fb41b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -12,7 +12,7 @@ const computeIQR = require('compute-iqr'); * @returns Array Two dimensional array. First dimension is the index of the bin, and the second index * is the count. This allows for direct import into ChartJS without having to change the data shape */ -function calculateHistogram(arr, numBins = 0, trimTailPercentage = 0.00) { +function calculateHistogram(arr, numBins = 0, trimTailPercentage = 0.00, minimum, maximum) { const bins = []; let dataCopy = arr.sort((a, b) => a - b); @@ -23,8 +23,12 @@ function calculateHistogram(arr, numBins = 0, trimTailPercentage = 0.00) { dataCopy = dataCopy.filter(x => x <= rightPercentile && x >= leftPercentile); } - const min = dataCopy[0]; - const max = dataCopy[dataCopy.length - 1]; + let min = dataCopy[0]; + let max = dataCopy[dataCopy.length - 1]; + if (!(minimum === undefined || maximum === undefined || minimum === null || maximum === null)) { + min = minimum; + max = maximum; + } if(numBins === 0){ const sturges = Math.ceil(Math.log2(dataCopy.length)) + 1;