Skip to content

Commit c089d8e

Browse files
committed
dataAspectRatio: boolean | number, document and test
1 parent 9ae7777 commit c089d8e

File tree

11 files changed

+103
-184
lines changed

11 files changed

+103
-184
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ Quantitative scales can be further customized with additional options:
206206

207207
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**, **nice**, and **zero** options are supported as shorthand for setting the respective option on all scales.
208208

209-
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.
209+
The top-level **dataAspectRatio** option, if true, computes a default height such that a variation of one unit in the *x* dimension is represented by the same number of pixels as a variation in the *y* dimension of one unit—or *t* units if the option is specified as a positive number *t*. Note: when using facets, make sure to set *fx* and *fy*’s round option to false if you need an exact data aspect ratio.
210210

211211
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.
212212

src/dimensions.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ export function Dimensions(
1212
},
1313
{
1414
width = 640,
15-
daspect,
16-
height,
1715
facet: {
1816
margin: facetMargin,
1917
marginTop: facetMarginTop = facetMargin !== undefined ? facetMargin : fxAxis === "top" ? 30 : 0,
@@ -25,12 +23,14 @@ export function Dimensions(
2523
marginTop = margin !== undefined ? margin : Math.max((xAxis === "top" ? 30 : 0) + facetMarginTop, yAxis || fyAxis ? 20 : 0.5 - offset),
2624
marginRight = margin !== undefined ? margin : Math.max((yAxis === "right" ? 40 : 0) + facetMarginRight, xAxis || fxAxis ? 20 : 0.5 + offset),
2725
marginBottom = margin !== undefined ? margin : Math.max((xAxis === "bottom" ? 30 : 0) + facetMarginBottom, yAxis || fyAxis ? 20 : 0.5 + offset),
28-
marginLeft = margin !== undefined ? margin : Math.max((yAxis === "left" ? 40 : 0) + facetMarginLeft, xAxis || fxAxis ? 20 : 0.5 - offset)
26+
marginLeft = margin !== undefined ? margin : Math.max((yAxis === "left" ? 40 : 0) + facetMarginLeft, xAxis || fxAxis ? 20 : 0.5 - offset),
27+
dataAspectRatio,
28+
height = autoHeight(scales, {width, marginLeft, marginRight, marginTop, marginBottom}, dataAspectRatio)
2929
} = {}
3030
) {
3131
return {
3232
width,
33-
height: height !== undefined ? height : autoHeight(scales, {width, marginLeft, marginRight, marginTop, marginBottom}, daspect),
33+
height,
3434
marginTop,
3535
marginRight,
3636
marginBottom,
@@ -42,17 +42,17 @@ export function Dimensions(
4242
};
4343
}
4444

45-
function autoHeight({x, y, fy, fx}, {width, marginLeft, marginRight, marginTop, marginBottom}, daspect) {
45+
function autoHeight({x, y, fy, fx}, {width, marginLeft, marginRight, marginTop, marginBottom}, dataAspectRatio) {
4646
const nfy = fy ? fy.scale.domain().length : 1;
4747
const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length : Math.max(7, 17 / nfy)) : 1;
4848

4949
// If a data aspect ratio is given, tweak the height to match
50-
if (daspect != null && daspect !== false) {
51-
if (!(isFinite(daspect) && daspect > 0)) throw new Error(`invalid data aspect ratio: ${daspect}`);
50+
if (dataAspectRatio = +dataAspectRatio) {
51+
if (!(isFinite(dataAspectRatio) && dataAspectRatio > 0)) throw new Error(`invalid data aspect ratio: ${dataAspectRatio}`);
5252
if (!["utc", "time", "linear"].includes(x?.type) || !["utc", "time", "linear"].includes(y?.type)) {
53-
warn(`invalid x/y scale types for the daspect option: ${x?.type}/${y?.type}`);
53+
warn(`invalid x/y scale types for the dataAspectRatio option: ${x?.type}/${y?.type}`);
5454
} else {
55-
const ratio = Math.abs((y.domain[1] - y.domain[0]) / (x.domain[1] - x.domain[0]) / daspect);
55+
const ratio = Math.abs((y.domain[1] - y.domain[0]) / (x.domain[1] - x.domain[0]) / dataAspectRatio);
5656
const trueWidth = (fx ? fx.scale.bandwidth() : 1) * (width - marginLeft - marginRight) - x.insetLeft - x.insetRight;
5757
return (ratio * trueWidth + y.insetTop + y.insetBottom) / (fy ? fy.scale.bandwidth() : 1) + marginTop + marginBottom;
5858
}

test/output/emptyDaspect.svg

Lines changed: 0 additions & 150 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Loading

test/output/liborProjections.svg

Lines changed: 3 additions & 3 deletions
Loading

0 commit comments

Comments
 (0)