Skip to content

Commit e30102c

Browse files
committed
daspect: set the default height
closes #836
1 parent 8757c02 commit e30102c

File tree

9 files changed

+4719
-16
lines changed

9 files changed

+4719
-16
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ Quantitative scales can be further customized with additional options:
201201

202202
Clamping is typically used in conjunction with setting an explicit domain since if the domain is inferred, no values will be outside the domain. Clamping is useful for focusing on a subset of the data while ensuring that extreme values remain visible, but use caution: clamped values may need an annotation to avoid misinterpretation. Top-level **clamp** and **nice** options are supported as shorthand for setting *scale*.clamp and *scale*.nice on all scales.
203203

204+
The top-level **daspect** option, if specified, computes a default height such that a variation of 1 unit in the *x* dimension roughly corresponds to the same number of pixels as a variation of *daspect* unit in the *y* dimension. Note: for an exact data aspect ratio, set *fx* and *fy*’s round option to false.
205+
204206
The *scale*.**transform** option allows you to apply a function to all values before they are passed through the scale. This is convenient for transforming a scale’s data, say to convert to thousands or between temperature units.
205207

206208
```js

src/dimensions.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ export function Dimensions(
1111
},
1212
{
1313
width = 640,
14-
height = autoHeight(scales),
14+
daspect,
15+
height,
1516
facet: {
1617
margin: facetMargin,
1718
marginTop: facetMarginTop = facetMargin !== undefined ? facetMargin : fxAxis === "top" ? 30 : 0,
@@ -28,7 +29,7 @@ export function Dimensions(
2829
) {
2930
return {
3031
width,
31-
height,
32+
height: height !== undefined ? height : autoHeight(scales, {width, marginLeft, marginRight, marginTop, marginBottom}, daspect),
3233
marginTop,
3334
marginRight,
3435
marginBottom,
@@ -40,8 +41,17 @@ export function Dimensions(
4041
};
4142
}
4243

43-
function autoHeight({y, fy, fx}) {
44+
function autoHeight({x, y, fy, fx}, {width, marginLeft, marginRight, marginTop, marginBottom}, daspect) {
4445
const nfy = fy ? fy.scale.domain().length : 1;
4546
const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length : Math.max(7, 17 / nfy)) : 1;
47+
48+
// If a data aspect ratio is given, tweak the height to match
49+
if (daspect != null && daspect !== false) {
50+
if (!(isFinite(daspect) && daspect > 0)) throw new Error(`invalid data aspect ratio: ${daspect}`);
51+
const ratio = Math.abs((y.domain[1] - y.domain[0]) / (x.domain[1] - x.domain[0]) / daspect);
52+
const trueWidth = (fx ? fx.scale.bandwidth() : 1) * (width - marginLeft - marginRight);
53+
return (ratio * trueWidth) / (fy ? fy.scale.bandwidth() : 1) + marginTop + marginBottom;
54+
}
55+
4656
return !!(y || fy) * Math.max(1, Math.min(60, ny * nfy)) * 20 + !!fx * 30 + 60;
4757
}

test/output/emptyDaspect.svg

Lines changed: 150 additions & 0 deletions
Loading

test/output/markovChain.svg

Lines changed: 12 additions & 12 deletions
Loading

0 commit comments

Comments
 (0)