Skip to content

Commit

Permalink
adopt nice (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Jan 11, 2023
1 parent 9124d1e commit f3902eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/contours.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions test/contours-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
});

0 comments on commit f3902eb

Please sign in to comment.