Skip to content

Commit 2cba296

Browse files
committed
daspect sets the default height
1 parent 1884d5d commit 2cba296

File tree

9 files changed

+1607
-1575
lines changed

9 files changed

+1607
-1575
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ 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, sets the data aspect ratio between the *x* and *y* scale; if set to 1, the range of one of the scales will be adjusted so that a variation of 1 unit in the *x* dimension corresponds to the same number of pixels as a variation of 1 unit in the *y* dimension.
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.
205205

206206
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.
207207

src/dimensions.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ export function Dimensions(
2727
marginLeft = margin !== undefined ? margin : Math.max((yAxis === "left" ? 40 : 0) + facetMarginLeft, xAxis || fxAxis ? 20 : 0.5 - offset)
2828
} = {}
2929
) {
30-
const dims = {
30+
return {
3131
width,
32-
height,
32+
height: height !== undefined ? height : autoHeight(scales, {width, marginLeft, marginRight, marginTop, marginBottom}, daspect),
3333
marginTop,
3434
marginRight,
3535
marginBottom,
@@ -39,23 +39,18 @@ export function Dimensions(
3939
facetMarginBottom,
4040
facetMarginLeft
4141
};
42-
if (height === undefined) height = autoHeight(scales, dims, daspect);
43-
return {...dims, height};
4442
}
4543

46-
function diff([a, b]) { return Math.abs(b - a); }
47-
4844
function autoHeight({x, y, fy, fx}, {width, marginLeft, marginRight, marginTop, marginBottom}, daspect) {
4945
const nfy = fy ? fy.scale.domain().length : 1;
5046
const ny = y ? (isOrdinalScale(y) ? y.scale.domain().length : Math.max(7, 17 / nfy)) : 1;
5147

52-
// If a data aspect ratio is given, tweak the height to match;
53-
// but first disentangle the facets' dimensions…
54-
if (daspect != null) {
55-
console.warn({daspect, xd: x.domain, yd: y.domain});
56-
const ratio = Math.abs(diff(y.domain) / diff(x.domain)) / daspect;
57-
const trueWidth = width - marginLeft - marginRight;
58-
return ratio * trueWidth + marginTop + marginBottom;
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;
5954
}
6055

6156
return !!(y || fy) * Math.max(1, Math.min(60, ny * nfy)) * 20 + !!fx * 30 + 60;

src/plot.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {basic} from "./transforms/basic.js";
1111
import {consumeWarnings} from "./warnings.js";
1212

1313
export function plot(options = {}) {
14-
const {facet, style, caption, ariaLabel, ariaDescription, daspect} = options;
14+
const {facet, style, caption, ariaLabel, ariaDescription} = options;
1515

1616
// className for inline styles
1717
const className = maybeClassName(options.className);
@@ -87,7 +87,7 @@ export function plot(options = {}) {
8787
const axes = Axes(scaleDescriptors, options);
8888
const dimensions = Dimensions(scaleDescriptors, axes, options);
8989

90-
autoScaleRange(scaleDescriptors, dimensions, daspect);
90+
autoScaleRange(scaleDescriptors, dimensions);
9191
autoScaleLabels(channelsByScale, scaleDescriptors, axes, dimensions, options);
9292
autoAxisTicks(scaleDescriptors, axes);
9393

src/scales.js

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,34 +67,11 @@ export function ScaleFunctions(scales) {
6767
}
6868

6969
// Mutates scale.range!
70-
export function autoScaleRange({x, y, fx, fy}, dimensions, daspect) {
70+
export function autoScaleRange({x, y, fx, fy}, dimensions) {
7171
if (fx) autoScaleRangeX(fx, dimensions);
7272
if (fy) autoScaleRangeY(fy, dimensions);
7373
if (x) autoScaleRangeX(x, fx ? {width: fx.scale.bandwidth()} : dimensions);
7474
if (y) autoScaleRangeY(y, fy ? {height: fy.scale.bandwidth()} : dimensions);
75-
if (daspect != null) {
76-
if (+daspect <= 0) throw new Error(`invalid daspect: ${daspect}`);
77-
if (x?.type !== "linear" || x.domain.length !== 2 || y?.type !== "linear" || y.domain.length !== 2) {
78-
throw new Error("Only simple linear scales can use a data aspect ratio");
79-
}
80-
const drx = x.range[1] - x.range[0];
81-
const ddx = x.domain[1] - x.domain[0];
82-
const dry = y.range[1] - y.range[0];
83-
const ddy = y.domain[1] - y.domain[0];
84-
const d = Math.abs(+daspect * drx / dry * ddy / ddx);
85-
if (d > 1) {
86-
const delta = drx * (1 - 1 / d) / 2;
87-
x.range[0] += delta;
88-
x.range[1] -= delta;
89-
x.scale.range(x.range);
90-
}
91-
else if (d < 1) {
92-
const delta = dry * (1 - d) / 2;
93-
y.range[0] += delta;
94-
y.range[1] -= delta;
95-
y.scale.range(y.range);
96-
}
97-
}
9875
}
9976

10077
function autoScaleRangeX(scale, dimensions) {

test/output/emptyDaspect.svg

Lines changed: 96 additions & 36 deletions
Loading

0 commit comments

Comments
 (0)