Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix autoHeight and outerRange for empty scale domains #1864

Merged
merged 4 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/dimensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, compute an aspect ratio based on the domain,
// defaulting to the projection’s natural aspect ratio (if known).
Expand All @@ -103,8 +103,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) {
Expand Down
1 change: 1 addition & 0 deletions src/plot.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,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];
Expand Down
25 changes: 25 additions & 0 deletions test/output/autoHeightEmpty.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions test/plots/autoheight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as Plot from "@observablehq/plot";

export async function autoHeightEmpty() {
return Plot.rectY([], {x: "date", y: "visitors", fy: "path"}).plot();
}
1 change: 1 addition & 0 deletions test/plots/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down