Skip to content

Commit 0941655

Browse files
authored
detect ordinal year intervals (#1512)
* detect ordinal year intervals * comment re. equality
1 parent 2f6ae0a commit 0941655

File tree

5 files changed

+42
-33
lines changed

5 files changed

+42
-33
lines changed

src/marks/axis.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {extent, format, utcFormat} from "d3";
1+
import {extent, format, timeFormat, utcFormat} from "d3";
22
import {formatDefault} from "../format.js";
33
import {marks} from "../mark.js";
44
import {radians} from "../math.js";
@@ -7,6 +7,7 @@ import {isIterable, isNoneish, isTemporal, orderof} from "../options.js";
77
import {maybeColorChannel, maybeNumberChannel, maybeRangeInterval} from "../options.js";
88
import {isTemporalScale} from "../scales.js";
99
import {offset} from "../style.js";
10+
import {isTimeYear, isUtcYear} from "../time.js";
1011
import {initializer} from "../transforms/basic.js";
1112
import {ruleX, ruleY} from "./rule.js";
1213
import {text, textX, textY} from "./text.js";
@@ -579,7 +580,11 @@ function inferTickFormat(scale, ticks, tickFormat) {
579580
return scale.tickFormat
580581
? scale.tickFormat(isIterable(ticks) ? null : ticks, tickFormat)
581582
: tickFormat === undefined
582-
? formatDefault
583+
? isUtcYear(scale.interval)
584+
? utcFormat("%Y")
585+
: isTimeYear(scale.interval)
586+
? timeFormat("%Y")
587+
: formatDefault
583588
: typeof tickFormat === "string"
584589
? (isTemporal(scale.domain()) ? utcFormat : format)(tickFormat)
585590
: constant(tickFormat);

src/time.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,15 @@ export function maybeTimeInterval(interval) {
7070
export function maybeUtcInterval(interval) {
7171
return parseInterval(interval, utcIntervals);
7272
}
73+
74+
export function isUtcYear(i) {
75+
if (!i) return false;
76+
const date = i.floor(new Date(Date.UTC(2000, 11, 31)));
77+
return utcYear(date) >= date; // coercing equality
78+
}
79+
80+
export function isTimeYear(i) {
81+
if (!i) return false;
82+
const date = i.floor(new Date(2000, 11, 31));
83+
return timeYear(date) >= date; // coercing equality
84+
}

test/output/internFacetDate.svg

Lines changed: 6 additions & 6 deletions
Loading

test/output/yearlyRequestsDot.svg

Lines changed: 15 additions & 15 deletions
Loading

test/plots/yearly-requests-dot.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@ export async function yearlyRequestsDot() {
77
[new Date("2005-01-01"), 5]
88
];
99
return Plot.plot({
10-
label: null,
11-
x: {
12-
type: "utc",
13-
interval: "year",
14-
inset: 40,
15-
grid: true
16-
},
17-
y: {
18-
zero: true
19-
},
10+
x: {type: "point", interval: "year", grid: true},
11+
y: {zero: true},
2012
marks: [Plot.ruleY([0]), Plot.dot(requests)]
2113
});
2214
}

0 commit comments

Comments
 (0)