Skip to content

Commit 4b0c162

Browse files
committed
automatic height
closes #323
1 parent 9edd66b commit 4b0c162

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/plot.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function Dimensions(
107107
},
108108
{
109109
width = 640,
110-
height = y || fy ? 396 : fx ? 90 : 60,
110+
height = autoHeight(y, fy, fx),
111111
facet: {
112112
marginTop: facetMarginTop = fxAxis === "top" ? 30 : 0,
113113
marginRight: facetMarginRight = fyAxis === "right" ? 40 : 0,
@@ -137,3 +137,17 @@ function Dimensions(
137137
function ScaleFunctions(scales) {
138138
return Object.fromEntries(Object.entries(scales).map(([name, {scale}]) => [name, scale]));
139139
}
140+
141+
function autoHeight(y, fy, fx) {
142+
const ny = y && ["ordinal", "point"].includes(y.type) && length(y.domain) || 1;
143+
const nfy = fy && ["ordinal", "point"].includes(fy.type) && length(fy.domain) || 1;
144+
return !!(y || fy) * clamp(ny * nfy * 16, 336, 1200) + !!fx * 30 + 60;
145+
}
146+
147+
function clamp(x, lo, hi) {
148+
return x < lo ? lo : x > hi ? hi : x;
149+
}
150+
151+
function length(iterator) {
152+
return Array.from(iterator).length;
153+
}

test/plots/athletes-sport-sex.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export default async function() {
66
const female = data => d3.mean(data, d => d.sex === "female");
77
return Plot.plot({
88
marginLeft: 100,
9-
height: 500,
109
x: {
1110
label: "Women (%) →",
1211
domain: [0, 100],

0 commit comments

Comments
 (0)