Skip to content

Commit ef19698

Browse files
authored
fix autoHeight and outerRange for empty scale domains (#1864)
closes #1856
1 parent 3d6aa1e commit ef19698

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, compute an aspect ratio based on the domain,
9898
// defaulting to the projection’s natural aspect ratio (if known).
@@ -103,8 +103,7 @@ function autoHeight(
103103
const lar = Math.max(0.1, Math.min(10, far)); // clamp the aspect ratio to a “reasonable” value
104104
return Math.round((width - marginLeftDefault - marginRightDefault) * lar + marginTopDefault + marginBottomDefault);
105105
}
106-
107-
const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length : Math.max(7, 17 / nfy)) : 1;
106+
const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length || 1 : Math.max(7, 17 / nfy)) : 1;
108107

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

src/plot.js

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

733733
function outerRange(scale) {
734734
const domain = scale.domain();
735+
if (domain.length === 0) return [0, scale.bandwidth()];
735736
let x1 = scale(domain[0]);
736737
let x2 = scale(domain[domain.length - 1]);
737738
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)