From 5f2b44838a6c8c0184c193c5bac1e1adb6aff07b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Rivi=C3=A8re?= Date: Wed, 20 Sep 2023 16:36:49 +0200 Subject: [PATCH 1/2] fix autoHeight and outerRange for empty scale domains closes #1856 --- src/dimensions.js | 5 ++--- src/plot.js | 1 + test/output/autoHeightEmpty.svg | 25 +++++++++++++++++++++++++ test/plots/autoheight.ts | 5 +++++ test/plots/index.ts | 1 + 5 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 test/output/autoHeightEmpty.svg create mode 100644 test/plots/autoheight.ts diff --git a/src/dimensions.js b/src/dimensions.js index 43b24b8b3a..a8b7576fcd 100644 --- a/src/dimensions.js +++ b/src/dimensions.js @@ -92,7 +92,7 @@ function autoHeight( {projection, aspectRatio}, {width, marginTopDefault, marginRightDefault, marginBottomDefault, marginLeftDefault} ) { - const nfy = fy ? fy.scale.domain().length : 1; + const nfy = fy ? fy.scale.domain().length || 1 : 1; // If a projection is specified, use its natural aspect ratio (if known). const ar = projectionAspectRatio(projection); @@ -102,8 +102,7 @@ function autoHeight( const lar = Math.max(0.1, Math.min(10, far)); // clamp the aspect ratio to a “reasonable” value return Math.round((width - marginLeftDefault - marginRightDefault) * lar + marginTopDefault + marginBottomDefault); } - - const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length : Math.max(7, 17 / nfy)) : 1; + const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length || 1 : Math.max(7, 17 / nfy)) : 1; // If a desired aspect ratio is given, compute a default height to match. if (aspectRatio != null) { diff --git a/src/plot.js b/src/plot.js index 039a269ada..cf37776376 100644 --- a/src/plot.js +++ b/src/plot.js @@ -737,6 +737,7 @@ function actualDimensions({fx, fy}, dimensions) { function outerRange(scale) { const domain = scale.domain(); + if (domain.length === 0) return [0, scale.bandwidth()]; let x1 = scale(domain[0]); let x2 = scale(domain[domain.length - 1]); if (x2 < x1) [x1, x2] = [x2, x1]; diff --git a/test/output/autoHeightEmpty.svg b/test/output/autoHeightEmpty.svg new file mode 100644 index 0000000000..a77ed1e02d --- /dev/null +++ b/test/output/autoHeightEmpty.svg @@ -0,0 +1,25 @@ + + + + path + + + visitors + + + date + + \ No newline at end of file diff --git a/test/plots/autoheight.ts b/test/plots/autoheight.ts new file mode 100644 index 0000000000..4fb89e79ce --- /dev/null +++ b/test/plots/autoheight.ts @@ -0,0 +1,5 @@ +import * as Plot from "@observablehq/plot"; + +export async function autoHeightEmpty() { + return Plot.rectY([], {x: "date", y: "visitors", fy: "path"}).plot(); +} diff --git a/test/plots/index.ts b/test/plots/index.ts index 0a412b4b67..cae5c0e538 100644 --- a/test/plots/index.ts +++ b/test/plots/index.ts @@ -27,6 +27,7 @@ export * from "./athletes-sport-sex.js"; export * from "./athletes-sport-weight.js"; export * from "./athletes-weight-cumulative.js"; export * from "./athletes-weight.js"; +export * from "./autoheight.js"; export * from "./autoplot.js"; export * from "./availability.js"; export * from "./axis-filter.js"; From 3b85f0dcbbc79759d112e96900d26aacdb98b3ce Mon Sep 17 00:00:00 2001 From: Mike Bostock Date: Sun, 28 Jul 2024 19:20:00 -0400 Subject: [PATCH 2/2] update test snapshot --- test/output/autoHeightEmpty.svg | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/test/output/autoHeightEmpty.svg b/test/output/autoHeightEmpty.svg index a77ed1e02d..6277e5e83a 100644 --- a/test/output/autoHeightEmpty.svg +++ b/test/output/autoHeightEmpty.svg @@ -1,15 +1,15 @@ @@ -19,7 +19,7 @@ visitors - - date + + date \ No newline at end of file