Skip to content

Commit

Permalink
fix tickFormat type inference for empty domain
Browse files Browse the repository at this point in the history
  • Loading branch information
mbostock committed Nov 14, 2024
1 parent 700c6ee commit bd859a5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/marks/axis.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,10 @@ export function inferTickFormat(scale, data, ticks, tickFormat, anchor) {
? inferTimeFormat(scale.type, data, anchor) ?? formatDefault
: scale.tickFormat
? scale.tickFormat(typeof ticks === "number" ? ticks : null, tickFormat)
: typeof tickFormat === "string" && scale.domain().length > 0
? (isTemporal(scale.domain()) ? utcFormat : format)(tickFormat)
: tickFormat === undefined
? formatDefault
: typeof tickFormat === "string"
? (isTemporal(scale.domain()) ? utcFormat : format)(tickFormat)
: constant(tickFormat);
}

Expand Down
8 changes: 4 additions & 4 deletions src/scales.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,10 +425,10 @@ function inferScaleType(key, channels, {type, domain, range, scheme, pivot, proj
if (kind === opacity || kind === length) return "linear";
if (kind === symbol) return "ordinal";

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

// Otherwise, infer the scale type from the data! Prefer the domain, if
// present, over channels. (The domain and channels should be consistently
Expand Down
1 change: 1 addition & 0 deletions test/plots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ export * from "./style-overrides.js";
export * from "./symbol-set.js";
export * from "./text-overflow.js";
export * from "./this-is-just-to-say.js";
export * from "./tick-format.js";
export * from "./time-axis.js";
export * from "./tip-format.js";
export * from "./tip.js";
Expand Down
5 changes: 5 additions & 0 deletions test/plots/tick-format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as Plot from "@observablehq/plot";

export async function tickFormatEmptyDomain() {
return Plot.plot({y: {tickFormat: "%W"}, marks: [Plot.dotX([0]), Plot.barY([]), Plot.frame()]});
}

0 comments on commit bd859a5

Please sign in to comment.