Skip to content

Commit e3af586

Browse files
authored
Merge pull request #3510 from plotly/fix-automargin-push-offset
Fix axis automargin push offset
2 parents 49620c8 + 4a0b1a5 commit e3af586

8 files changed

+73
-12
lines changed

src/plots/cartesian/axes.js

+36-10
Original file line numberDiff line numberDiff line change
@@ -1940,27 +1940,53 @@ axes.drawOne = function(gd, ax, opts) {
19401940
var hasRangeSlider = Registry.getComponentMethod('rangeslider', 'isVisible')(ax);
19411941

19421942
function doAutoMargins() {
1943-
var push, rangeSliderPush;
1943+
var s = ax.side.charAt(0);
1944+
var push;
1945+
var rangeSliderPush;
19441946

19451947
if(hasRangeSlider) {
19461948
rangeSliderPush = Registry.getComponentMethod('rangeslider', 'autoMarginOpts')(gd, ax);
19471949
}
19481950
Plots.autoMargin(gd, rangeSliderAutoMarginID(ax), rangeSliderPush);
19491951

1950-
var s = ax.side.charAt(0);
19511952
if(ax.automargin && (!hasRangeSlider || s !== 'b')) {
19521953
push = {x: 0, y: 0, r: 0, l: 0, t: 0, b: 0};
19531954

1954-
if(axLetter === 'x') {
1955-
push.y = (ax.anchor === 'free' ? ax.position :
1956-
ax._anchorAxis.domain[s === 't' ? 1 : 0]);
1957-
push[s] += ax._boundingBox.height;
1958-
} else {
1959-
push.x = (ax.anchor === 'free' ? ax.position :
1960-
ax._anchorAxis.domain[s === 'r' ? 1 : 0]);
1961-
push[s] += ax._boundingBox.width;
1955+
var bbox = ax._boundingBox;
1956+
var counterAx = mainPlotinfo[counterLetter + 'axis'];
1957+
var anchorAxDomainIndex;
1958+
var offset;
1959+
1960+
switch(axLetter + s) {
1961+
case 'xb':
1962+
anchorAxDomainIndex = 0;
1963+
offset = bbox.top - counterAx._length - counterAx._offset;
1964+
push[s] = bbox.height;
1965+
break;
1966+
case 'xt':
1967+
anchorAxDomainIndex = 1;
1968+
offset = counterAx._offset - bbox.bottom;
1969+
push[s] = bbox.height;
1970+
break;
1971+
case 'yl':
1972+
anchorAxDomainIndex = 0;
1973+
offset = counterAx._offset - bbox.right;
1974+
push[s] = bbox.width;
1975+
break;
1976+
case 'yr':
1977+
anchorAxDomainIndex = 1;
1978+
offset = bbox.left - counterAx._length - counterAx._offset;
1979+
push[s] = bbox.width;
1980+
break;
19621981
}
19631982

1983+
push[counterLetter] = ax.anchor === 'free' ?
1984+
ax.position :
1985+
ax._anchorAxis.domain[anchorAxDomainIndex];
1986+
1987+
if(push[s] > 0) {
1988+
push[s] += offset;
1989+
}
19641990
if(ax.title.text !== fullLayout._dfltTitle[axLetter]) {
19651991
push[s] += ax.title.font.size;
19661992
}
Loading
-142 Bytes
Loading
1.59 KB
Loading
-327 Bytes
Loading
50 Bytes
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"data": [
3+
{
4+
"x": [ "12 AM", "1 AM", "2 AM", "3 AM", "4 AM", "5 AM", "6 AM", "7 AM", "8 AM", "9 AM", "10 AM", "11 AM" ],
5+
"y": [ 59.44, 68.75, 87.5, 100.5, 95.56, 92.8, 85.25, 77.4, 76.4, 73.94, 74.56, 81.06 ]
6+
},
7+
{
8+
"x": [ "12 AM", "1 AM", "2 AM", "3 AM", "4 AM", "5 AM", "6 AM", "7 AM", "8 AM", "9 AM", "10 AM", "11 AM" ],
9+
"y": [ 59.44, 68.75, 87.5, 100.5, 95.56, 92.8, 85.25, 77.4, 76.4, 73.94, 74.56, 81.06 ],
10+
"xaxis": "x2",
11+
"yaxis": "y2"
12+
}
13+
],
14+
"layout": {
15+
"margin": {"l": 0, "r": 0, "t": 0, "b": 0},
16+
"grid": {"rows": 1, "columns": 2, "pattern": "independent"},
17+
"xaxis": {
18+
"automargin": true
19+
},
20+
"yaxis": {
21+
"automargin": true
22+
},
23+
"xaxis2": {
24+
"automargin": true,
25+
"side": "top"
26+
},
27+
"yaxis2": {
28+
"automargin": true,
29+
"side": "right"
30+
},
31+
"showlegend": false,
32+
"width": 700,
33+
"height": 450
34+
}
35+
}

test/jasmine/tests/axes_test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3150,7 +3150,7 @@ describe('Test axes', function() {
31503150
})
31513151
.then(function() {
31523152
var size = gd._fullLayout._size;
3153-
expect(size.l).toBe(previousSize.l);
3153+
expect(size.l).toBeWithin(previousSize.l, 1.1);
31543154
expect(size.r).toBe(previousSize.r);
31553155
expect(size.b).toBe(previousSize.b);
31563156
expect(size.t).toBe(previousSize.t);
@@ -3187,7 +3187,7 @@ describe('Test axes', function() {
31873187
expect(size.l).toBe(initialSize.r);
31883188
expect(size.r).toBe(previousSize.l);
31893189
expect(size.b).toBe(initialSize.b);
3190-
expect(size.t).toBe(previousSize.b);
3190+
expect(size.t).toBeWithin(previousSize.b, 1.1);
31913191

31923192
return Plotly.relayout(gd, {
31933193
'xaxis.automargin': false,

0 commit comments

Comments
 (0)