Skip to content

Commit bd859a5

Browse files
committed
fix tickFormat type inference for empty domain
1 parent 700c6ee commit bd859a5

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

src/marks/axis.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -672,10 +672,10 @@ export function inferTickFormat(scale, data, ticks, tickFormat, anchor) {
672672
? inferTimeFormat(scale.type, data, anchor) ?? formatDefault
673673
: scale.tickFormat
674674
? scale.tickFormat(typeof ticks === "number" ? ticks : null, tickFormat)
675+
: typeof tickFormat === "string" && scale.domain().length > 0
676+
? (isTemporal(scale.domain()) ? utcFormat : format)(tickFormat)
675677
: tickFormat === undefined
676678
? formatDefault
677-
: typeof tickFormat === "string"
678-
? (isTemporal(scale.domain()) ? utcFormat : format)(tickFormat)
679679
: constant(tickFormat);
680680
}
681681

src/scales.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,10 @@ function inferScaleType(key, channels, {type, domain, range, scheme, pivot, proj
425425
if (kind === opacity || kind === length) return "linear";
426426
if (kind === symbol) return "ordinal";
427427

428-
// If the domain or range has more than two values, assume it’s ordinal. You
429-
// can still use a “piecewise” (or “polylinear”) scale, but you must set the
430-
// type explicitly.
431-
if ((domain || range || []).length > 2) return asOrdinalType(kind);
428+
// If the domain or range doesn’t have exactly two values, assume it’s
429+
// ordinal. You can still use a “piecewise” (or “polylinear”) scale, but you
430+
// must set the type explicitly.
431+
if ((domain || range || []).length !== 2) return asOrdinalType(kind);
432432

433433
// Otherwise, infer the scale type from the data! Prefer the domain, if
434434
// present, over channels. (The domain and channels should be consistently

test/plots/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ export * from "./style-overrides.js";
308308
export * from "./symbol-set.js";
309309
export * from "./text-overflow.js";
310310
export * from "./this-is-just-to-say.js";
311+
export * from "./tick-format.js";
311312
export * from "./time-axis.js";
312313
export * from "./tip-format.js";
313314
export * from "./tip.js";

test/plots/tick-format.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import * as Plot from "@observablehq/plot";
2+
3+
export async function tickFormatEmptyDomain() {
4+
return Plot.plot({y: {tickFormat: "%W"}, marks: [Plot.dotX([0]), Plot.barY([]), Plot.frame()]});
5+
}

0 commit comments

Comments
 (0)