diff --git a/src/bin.js b/src/bin.js index 276e0f6e..28d64aad 100644 --- a/src/bin.js +++ b/src/bin.js @@ -85,14 +85,14 @@ export default function bin() { if (step > 0) { for (i = 0; i < n; ++i) { if ((x = values[i]) != null && x0 <= x && x <= x1) { - bins[Math.floor((x - x0) / step)].push(data[i]); + bins[Math.min(m, Math.floor((x - x0) / step))].push(data[i]); } } } else if (step < 0) { for (i = 0; i < n; ++i) { if ((x = values[i]) != null && x0 <= x && x <= x1) { const j = Math.floor((x0 - x) * step); - bins[j + (tz[j] <= x)].push(data[i]); // handle off-by-one due to rounding + bins[Math.min(m, j + (tz[j] <= x))].push(data[i]); // handle off-by-one due to rounding } } } diff --git a/test/bin-test.js b/test/bin-test.js index 9ee6099d..389e505d 100644 --- a/test/bin-test.js +++ b/test/bin-test.js @@ -235,6 +235,12 @@ it("bin(data) assigns floating point values to the correct bins", () => { } }); +it("bin(data) assigns integer values to the correct bins", () => { + assert.deepStrictEqual(bin().domain([4, 5])([5]), [box([5], 4, 5)]); + const eights = [8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8]; + assert.deepStrictEqual(bin().domain([3, 8])(eights), [box([], 3, 4), box([], 4, 5), box([], 5, 6), box([], 6, 7), box(eights, 7, 8)]); +}); + function box(bin, x0, x1) { bin.x0 = x0; bin.x1 = x1;