Skip to content

Commit 5f2b448

Browse files
committed
fix autoHeight and outerRange for empty scale domains
closes #1856
1 parent f55155b commit 5f2b448

File tree

5 files changed

+34
-3
lines changed

5 files changed

+34
-3
lines changed

src/dimensions.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function autoHeight(
9292
{projection, aspectRatio},
9393
{width, marginTopDefault, marginRightDefault, marginBottomDefault, marginLeftDefault}
9494
) {
95-
const nfy = fy ? fy.scale.domain().length : 1;
95+
const nfy = fy ? fy.scale.domain().length || 1 : 1;
9696

9797
// If a projection is specified, use its natural aspect ratio (if known).
9898
const ar = projectionAspectRatio(projection);
@@ -102,8 +102,7 @@ function autoHeight(
102102
const lar = Math.max(0.1, Math.min(10, far)); // clamp the aspect ratio to a “reasonable” value
103103
return Math.round((width - marginLeftDefault - marginRightDefault) * lar + marginTopDefault + marginBottomDefault);
104104
}
105-
106-
const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length : Math.max(7, 17 / nfy)) : 1;
105+
const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length || 1 : Math.max(7, 17 / nfy)) : 1;
107106

108107
// If a desired aspect ratio is given, compute a default height to match.
109108
if (aspectRatio != null) {

src/plot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@ function actualDimensions({fx, fy}, dimensions) {
737737

738738
function outerRange(scale) {
739739
const domain = scale.domain();
740+
if (domain.length === 0) return [0, scale.bandwidth()];
740741
let x1 = scale(domain[0]);
741742
let x2 = scale(domain[domain.length - 1]);
742743
if (x2 < x1) [x1, x2] = [x2, x1];

test/output/autoHeightEmpty.svg

Lines changed: 25 additions & 0 deletions
Loading

test/plots/autoheight.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 autoHeightEmpty() {
4+
return Plot.rectY([], {x: "date", y: "visitors", fy: "path"}).plot();
5+
}

test/plots/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export * from "./athletes-sport-sex.js";
2727
export * from "./athletes-sport-weight.js";
2828
export * from "./athletes-weight-cumulative.js";
2929
export * from "./athletes-weight.js";
30+
export * from "./autoheight.js";
3031
export * from "./autoplot.js";
3132
export * from "./availability.js";
3233
export * from "./axis-filter.js";

0 commit comments

Comments
 (0)