From da576168d7b5498c054c427a0ce45391c7ea4dc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Mon, 9 Nov 2026 21:18:00 +0100 Subject: [PATCH 1/2] adopt d3.nice --- src/linear.js | 40 +++++----------------------------------- test/linear-test.js | 9 +++++++++ 2 files changed, 14 insertions(+), 35 deletions(-) diff --git a/src/linear.js b/src/linear.js index 3617d32..f257082 100644 --- a/src/linear.js +++ b/src/linear.js @@ -1,4 +1,4 @@ -import {ticks, tickIncrement} from "d3-array"; +import {nice, ticks} from "d3-array"; import continuous, {copy} from "./continuous.js"; import {initRange} from "./init.js"; import tickFormat from "./tickFormat.js"; @@ -18,40 +18,10 @@ export function linearish(scale) { scale.nice = function(count) { if (count == null) count = 10; - - var d = domain(); - var i0 = 0; - var i1 = d.length - 1; - var start = d[i0]; - var stop = d[i1]; - var prestep; - var step; - var maxIter = 10; - - if (stop < start) { - step = start, start = stop, stop = step; - step = i0, i0 = i1, i1 = step; - } - - while (maxIter-- > 0) { - step = tickIncrement(start, stop, count); - if (step === prestep) { - d[i0] = start - d[i1] = stop - return domain(d); - } else if (step > 0) { - start = Math.floor(start / step) * step; - stop = Math.ceil(stop / step) * step; - } else if (step < 0) { - start = Math.ceil(start * step) / step; - stop = Math.floor(stop * step) / step; - } else { - break; - } - prestep = step; - } - - return scale; + var d = domain(), n = d.length - 1; + if (d[0] <= d[n]) [d[0], d[n]] = nice(d[0], d[n], count); + else [d[n], d[0]] = nice(d[n], d[0], count); + return domain(d); }; return scale; diff --git a/test/linear-test.js b/test/linear-test.js index 5e84d2d..74de470 100644 --- a/test/linear-test.js +++ b/test/linear-test.js @@ -243,6 +243,15 @@ it("linear.nice(count) nices the domain, extending it to round numbers", () => { assert.deepStrictEqual(scaleLinear().domain([0, 15]).nice(5).domain(), [0, 20]); }); +it("linear.nice(count) handles small steps precisely", () => { + assert.deepStrictEqual(scaleLinear().domain([1.1, 1.1 + 0.3]).nice().domain(), [1.1, 1.45]); + assert.deepStrictEqual(scaleLinear().domain([-(1.1 + 0.3), -1.1]).nice().domain(), [-1.45, -1.1]); +}); + +it("linear.nice(count) handles small counts", () => { + assert.deepStrictEqual(scaleLinear().domain([-1, 5]).nice(1).domain(), [-10, 10]); +}); + it("linear.nice(count) has no effect on degenerate domains", () => { assert.deepStrictEqual(scaleLinear().domain([0, 0]).nice(10).domain(), [0, 0]); assert.deepStrictEqual(scaleLinear().domain([0.5, 0.5]).nice(10).domain(), [0.5, 0.5]); From be1e108562694ee4ef7cedf9fca04bbc770bd96d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Mon, 9 Nov 2026 21:32:27 +0100 Subject: [PATCH 2/2] skip forward-looking test --- test/linear-test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/linear-test.js b/test/linear-test.js index 74de470..e68ee43 100644 --- a/test/linear-test.js +++ b/test/linear-test.js @@ -248,7 +248,7 @@ it("linear.nice(count) handles small steps precisely", () => { assert.deepStrictEqual(scaleLinear().domain([-(1.1 + 0.3), -1.1]).nice().domain(), [-1.45, -1.1]); }); -it("linear.nice(count) handles small counts", () => { +it.skip("linear.nice(count) handles small counts", () => { // See d3-array #299. assert.deepStrictEqual(scaleLinear().domain([-1, 5]).nice(1).domain(), [-10, 10]); });