diff --git a/src/bin.js b/src/bin.js index 4557e10..a8792f5 100644 --- a/src/bin.js +++ b/src/bin.js @@ -54,9 +54,9 @@ export default function bin() { const step = tickIncrement(x0, x1, tn); if (isFinite(step)) { if (step > 0) { - x1 = (Math.floor(x1 / step) + 1) * step; + x1 = (Math.round(x1 / step) + 1) * step; } else if (step < 0) { - x1 = (Math.ceil(x1 * -step) + 1) / -step; + x1 = (Math.round(x1 * -step) + 1) / -step; } } } else { diff --git a/test/bin-test.js b/test/bin-test.js index d015161..cfb8ef2 100644 --- a/test/bin-test.js +++ b/test/bin-test.js @@ -258,6 +258,12 @@ it("bin(data) does not mutate user-supplied thresholds as a function", () => { assert.deepStrictEqual(b.thresholds()(), [3, 4, 5, 6]); }); +it("bin(data) extends the niced domain by exactly one step when the maximum is a threshold", () => { + assert.deepStrictEqual(bin().thresholds(7)([0, 0.07]).at(-1), box([0.07], 0.07, 0.08)); + assert.deepStrictEqual(bin().thresholds(10)([0, 0.07]).at(-1), box([0.07], 0.07, 0.075)); + assert.deepStrictEqual(bin().thresholds(50)([0, 1.1]).at(-1), box([1.1], 1.1, 1.12)); +}); + function box(bin, x0, x1) { bin.x0 = x0; bin.x1 = x1;