Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/scales.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ function autoScaleRangeX(scale, dimensions) {
if (scale.range === undefined) {
const {insetLeft, insetRight} = scale;
const {width, marginLeft = 0, marginRight = 0} = dimensions;
scale.range = [marginLeft + insetLeft, width - marginRight - insetRight];
const left = marginLeft + insetLeft;
const right = width - marginRight - insetRight;
scale.range = [left, Math.max(left, right)];
if (!isOrdinalScale(scale)) scale.range = piecewiseRange(scale);
scale.scale.range(scale.range);
}
Expand All @@ -85,7 +87,9 @@ function autoScaleRangeY(scale, dimensions) {
if (scale.range === undefined) {
const {insetTop, insetBottom} = scale;
const {height, marginTop = 0, marginBottom = 0} = dimensions;
scale.range = [height - marginBottom - insetBottom, marginTop + insetTop];
const top = marginTop + insetTop;
const bottom = height - marginBottom - insetBottom;
scale.range = [Math.max(top, bottom), top];
if (!isOrdinalScale(scale)) scale.range = piecewiseRange(scale);
else scale.range.reverse();
scale.scale.range(scale.range);
Expand Down
10 changes: 10 additions & 0 deletions test/scales/scales-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,16 @@ it("plot(…).scale('opacity') respects the percent option, affecting domain and
});
});

it("plot({inset, …}).scale('x') does not allow insets or margins to invert the range", () => {
assert.deepStrictEqual(Plot.plot({x: {inset: 60}, width: 100}).scale("x").range, [80, 80]);
assert.deepStrictEqual(Plot.plot({x: {inset: 30}, margin: 30, width: 100}).scale("x").range, [60, 60]);
});

it("plot({inset, …}).scale('y') does not allow insets or margins to invert the range", () => {
assert.deepStrictEqual(Plot.plot({y: {inset: 60}, height: 100}).scale("y").range, [80, 80]);
assert.deepStrictEqual(Plot.plot({y: {inset: 30}, margin: 30, height: 100}).scale("y").range, [60, 60]);
});

it("plot({inset, …}).scale('x').range respects the given top-level inset", () => {
assert.deepStrictEqual(Plot.dot([1, 2, 3], {x: d => d}).plot({inset: 0}).scale("x").range, [20, 620]);
assert.deepStrictEqual(Plot.dot([1, 2, 3], {x: d => d}).plot({inset: 7}).scale("x").range, [27, 613]);
Expand Down