Skip to content

Commit 390aabe

Browse files
authored
don’t allow margins or insets to invert range (#640)
1 parent 72b2adb commit 390aabe

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/scales.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ function autoScaleRangeX(scale, dimensions) {
7474
if (scale.range === undefined) {
7575
const {insetLeft, insetRight} = scale;
7676
const {width, marginLeft = 0, marginRight = 0} = dimensions;
77-
scale.range = [marginLeft + insetLeft, width - marginRight - insetRight];
77+
const left = marginLeft + insetLeft;
78+
const right = width - marginRight - insetRight;
79+
scale.range = [left, Math.max(left, right)];
7880
if (!isOrdinalScale(scale)) scale.range = piecewiseRange(scale);
7981
scale.scale.range(scale.range);
8082
}
@@ -85,7 +87,9 @@ function autoScaleRangeY(scale, dimensions) {
8587
if (scale.range === undefined) {
8688
const {insetTop, insetBottom} = scale;
8789
const {height, marginTop = 0, marginBottom = 0} = dimensions;
88-
scale.range = [height - marginBottom - insetBottom, marginTop + insetTop];
90+
const top = marginTop + insetTop;
91+
const bottom = height - marginBottom - insetBottom;
92+
scale.range = [Math.max(top, bottom), top];
8993
if (!isOrdinalScale(scale)) scale.range = piecewiseRange(scale);
9094
else scale.range.reverse();
9195
scale.scale.range(scale.range);

test/scales/scales-test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,16 @@ it("plot(…).scale('opacity') respects the percent option, affecting domain and
10911091
});
10921092
});
10931093

1094+
it("plot({inset, …}).scale('x') does not allow insets or margins to invert the range", () => {
1095+
assert.deepStrictEqual(Plot.plot({x: {inset: 60}, width: 100}).scale("x").range, [80, 80]);
1096+
assert.deepStrictEqual(Plot.plot({x: {inset: 30}, margin: 30, width: 100}).scale("x").range, [60, 60]);
1097+
});
1098+
1099+
it("plot({inset, …}).scale('y') does not allow insets or margins to invert the range", () => {
1100+
assert.deepStrictEqual(Plot.plot({y: {inset: 60}, height: 100}).scale("y").range, [80, 80]);
1101+
assert.deepStrictEqual(Plot.plot({y: {inset: 30}, margin: 30, height: 100}).scale("y").range, [60, 60]);
1102+
});
1103+
10941104
it("plot({inset, …}).scale('x').range respects the given top-level inset", () => {
10951105
assert.deepStrictEqual(Plot.dot([1, 2, 3], {x: d => d}).plot({inset: 0}).scale("x").range, [20, 620]);
10961106
assert.deepStrictEqual(Plot.dot([1, 2, 3], {x: d => d}).plot({inset: 7}).scale("x").range, [27, 613]);

0 commit comments

Comments
 (0)