From 6e785cc2ce4edf6dfdb313daf99d227f25878303 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Mon, 9 Nov 2026 18:33:29 +0100 Subject: [PATCH] regression fix: the last bin is sometimes 2x too wide --- src/bin.js | 4 ++-- test/bin-test.js | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) 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;