diff --git a/dist/index.js b/dist/index.js index 54c9499..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; @@ -55,7 +61,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..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; @@ -47,7 +51,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]++;