From 25ed0fdf6e48872432d580679990433448b987db Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Tue, 10 Jan 2023 21:23:10 -0800 Subject: [PATCH] adopt nice --- src/contours.js | 8 +++++--- test/contours-test.js | 10 ++++++++++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/contours.js b/src/contours.js index ea3a0a4..90faf82 100644 --- a/src/contours.js +++ b/src/contours.js @@ -1,4 +1,4 @@ -import {extent, thresholdSturges, ticks, tickStep} from "d3-array"; +import {extent, nice, thresholdSturges, ticks} from "d3-array"; import {slice} from "./array.js"; import ascending from "./ascending.js"; import area from "./area.js"; @@ -36,8 +36,10 @@ export default function() { // Convert number of thresholds into uniform thresholds. if (!Array.isArray(tz)) { - const e = extent(values, finite), ts = tickStep(e[0], e[1], tz); - tz = ticks(Math.floor(e[0] / ts) * ts, Math.floor(e[1] / ts - 1) * ts, tz); + const e = extent(values, finite); + tz = ticks(...nice(e[0], e[1], tz), tz); + while (tz[tz.length - 1] >= e[1]) tz.pop(); + while (tz[1] < e[0]) tz.shift(); } else { tz = tz.slice().sort(ascending); } diff --git a/test/contours-test.js b/test/contours-test.js index 94baa33..6cfaa31 100644 --- a/test/contours-test.js +++ b/test/contours-test.js @@ -288,3 +288,13 @@ it("contour(values, invalid value) throws an error", () => { assert.throws(() => contours().size([3, 3]).contour([1, 2, 3, 4, 5, 6, 7, 8, 9], value), /invalid value/); } }); + +it("contours(values) uses the expected nice thresholds", () => { + assert.deepStrictEqual(contours().size([2, 1]).thresholds(14)([-149.76192742819748, 321.19300631539585]).map((c) => c.value), [-150, -100, -50, 0, 50, 100, 150, 200, 250, 300]); + assert.deepStrictEqual(contours().size([2, 1]).thresholds(5)([-149.76192742819748, 321.19300631539585]).map((c) => c.value), [-200, -100, 0, 100, 200, 300]); + assert.deepStrictEqual(contours().size([2, 1]).thresholds(14)([149.76192742819748, -321.19300631539585]).map((c) => c.value), [-350, -300, -250, -200, -150, -100, -50, 0, 50, 100]); + assert.deepStrictEqual(contours().size([2, 1]).thresholds(5)([149.76192742819748, -321.19300631539585]).map((c) => c.value), [-400, -300, -200, -100, 0, 100]); + assert.deepStrictEqual(contours().size([2, 1]).thresholds(12)([-29, 50]).map((c) => c.value), [-30, -25, -20, -15, -10, -5, 0, 5, 10, 15, 20, 25, 30, 35, 40, 45]); + assert.deepStrictEqual(contours().size([2, 1]).thresholds(10)([-41, 245]).map((c) => c.value), [-50, 0, 50, 100, 150, 200]); + assert.deepStrictEqual(contours().size([2, 1]).thresholds(9)([-22, 242]).map((c) => c.value), [-50, 0, 50, 100, 150, 200]); +});