From 1bfe5ffc7b579cc13de80a17b08cff23d14c1df1 Mon Sep 17 00:00:00 2001 From: magiccwarss Date: Sat, 7 Nov 2026 17:10:30 +0400 Subject: [PATCH] Normalize descending domains when selecting tick intervals --- src/ticks.js | 1 + test/tickInterval-test.js | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 test/tickInterval-test.js diff --git a/src/ticks.js b/src/ticks.js index c314789..756faef 100644 --- a/src/ticks.js +++ b/src/ticks.js @@ -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)); diff --git a/test/tickInterval-test.js b/test/tickInterval-test.js new file mode 100644 index 0000000..50d727d --- /dev/null +++ b/test/tickInterval-test.js @@ -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) + ]); + }); +}