Skip to content

Commit d6f0dd8

Browse files
authored
precise fractional intervals (#1479)
1 parent 9151f27 commit d6f0dd8

File tree

5 files changed

+1324
-10
lines changed

5 files changed

+1324
-10
lines changed

src/options.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -275,12 +275,19 @@ export function mid(x1, x2) {
275275
export function maybeInterval(interval, type) {
276276
if (interval == null) return;
277277
if (typeof interval === "number") {
278-
const n = interval;
279-
return {
280-
floor: (d) => n * Math.floor(d / n),
281-
offset: (d) => d + n, // note: no optional step for simplicity
282-
range: (lo, hi) => rangei(Math.ceil(lo / n), hi / n).map((x) => n * x)
283-
};
278+
if (0 < interval && interval < 1 && Number.isInteger(1 / interval)) interval = -1 / interval;
279+
const n = Math.abs(interval);
280+
return interval < 0
281+
? {
282+
floor: (d) => Math.floor(d * n) / n,
283+
offset: (d) => (d * n + 1) / n, // note: no optional step for simplicity
284+
range: (lo, hi) => rangei(Math.ceil(lo * n), hi * n).map((x) => x / n)
285+
}
286+
: {
287+
floor: (d) => Math.floor(d / n) * n,
288+
offset: (d) => d + n, // note: no optional step for simplicity
289+
range: (lo, hi) => rangei(Math.ceil(lo / n), hi / n).map((x) => x * n)
290+
};
284291
}
285292
if (typeof interval === "string") return (type === "time" ? maybeTimeInterval : maybeUtcInterval)(interval);
286293
if (typeof interval.floor !== "function") throw new Error("invalid interval; missing floor method");

0 commit comments

Comments
 (0)