Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ticks.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ function ticker(year, month, week, day, hour, minute) {
}

function tickInterval(start, stop, count) {
if (stop < start) [start, stop] = [stop, start];
const target = Math.abs(stop - start) / count;
const i = bisector(([,, step]) => step).right(tickIntervals, target);
if (i === tickIntervals.length) return year.every(tickStep(start / durationYear, stop / durationYear, count));
Expand Down
26 changes: 26 additions & 0 deletions test/tickInterval-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import assert from "node:assert";
import {it} from "vitest";
import {timeTickInterval, utcTickInterval} from "d3-time";
import {local, utc} from "./date.js";

for (const [name, tickInterval, date] of [["timeTickInterval", timeTickInterval, local], ["utcTickInterval", utcTickInterval, utc]]) {
it(`${name} selects multi-year intervals for descending domains`, () => {
const interval = tickInterval(date(2020, 0, 1), date(2000, 0, 1), 4);
assert.notStrictEqual(interval, null);
assert.deepStrictEqual(interval.range(date(2000, 0, 1), date(2021, 0, 1)), [
date(2000, 0, 1),
date(2005, 0, 1),
date(2010, 0, 1),
date(2015, 0, 1),
date(2020, 0, 1)
]);
});

it(`${name} selects sub-second intervals for descending domains`, () => {
const interval = tickInterval(new Date(1000), new Date(0), 5);
assert.deepStrictEqual(interval.offset(new Date(0), 1), new Date(200));
assert.deepStrictEqual(interval.range(new Date(0), new Date(1001)), [
new Date(0), new Date(200), new Date(400), new Date(600), new Date(800), new Date(1000)
]);
});
}