Skip to content

Commit e100f11

Browse files
Filmbostock
authored andcommitted
fix NaN ticks crashing (observablehq#1335)
* Guard against formatDefault returning undefined (it always returns a string except when the value is NaN) Closes observablehq#1334 related to observablehq#493 * coalesce null to empty string * DRY --------- Co-authored-by: Mike Bostock <[email protected]>
1 parent 9605e07 commit e100f11

File tree

6 files changed

+1171
-1
lines changed

6 files changed

+1171
-1
lines changed

src/marks/text.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function applyMultilineText(selection, mark, T, TL) {
137137
if (!T) return;
138138
const {lineAnchor, lineHeight, textOverflow, splitLines, clipLine} = mark;
139139
selection.each(function (i) {
140-
const lines = splitLines(formatDefault(T[i])).map(clipLine);
140+
const lines = splitLines(formatDefault(T[i]) ?? "").map(clipLine);
141141
const n = lines.length;
142142
const y = lineAnchor === "top" ? 0.71 : lineAnchor === "bottom" ? 1 - n : (164 - n * 100) / 200;
143143
if (n > 1) {

test/output/penguinNA1.svg

Lines changed: 382 additions & 0 deletions
Loading

test/output/penguinNA2.svg

Lines changed: 382 additions & 0 deletions
Loading

test/output/penguinNA3.svg

Lines changed: 382 additions & 0 deletions
Loading

test/plots/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ export * from "./penguin-mass-sex-species.js";
188188
export * from "./penguin-mass-sex.js";
189189
export * from "./penguin-mass-species.js";
190190
export * from "./penguin-mass.js";
191+
export * from "./penguin-na.js";
191192
export * from "./penguin-quantile-unknown.js";
192193
export * from "./penguin-sex-mass-culmen-species.js";
193194
export * from "./penguin-sex.js";

test/plots/penguin-na.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as Plot from "@observablehq/plot";
2+
import * as d3 from "d3";
3+
4+
async function penguinNA(tickFormat: (x: number) => any = undefined) {
5+
const sample = await d3.csv<any>("data/penguins.csv", d3.autoType);
6+
const V = Plot.valueof(sample, "body_mass_g");
7+
const [min, max] = d3.extent(V);
8+
return Plot.tickX(sample, {x: V, stroke: (d) => (d.body_mass_g ? "black" : "red")}).plot({
9+
x: {unknown: 10, ticks: [NaN, ...d3.ticks(min, max, 7)], tickFormat}
10+
});
11+
}
12+
13+
export async function penguinNA1() {
14+
return penguinNA();
15+
}
16+
17+
export async function penguinNA2() {
18+
return penguinNA((d) => (isNaN(d) ? "N/A" : d));
19+
}
20+
21+
export async function penguinNA3() {
22+
return penguinNA((d) => d);
23+
}

0 commit comments

Comments
 (0)