Skip to content

Commit a5a5de9

Browse files
authored
Merge pull request #5610 from plotly/fix5601
Take into account inside labels of the counter axis during autorange relayout
2 parents 32d4631 + b676bc6 commit a5a5de9

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/plots/cartesian/autorange.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use strict';
22

3+
var d3 = require('@plotly/d3');
34
var isNumeric = require('fast-isnumeric');
45

56
var Lib = require('../../lib');
67
var FP_SAFE = require('../../constants/numerical').FP_SAFE;
78
var Registry = require('../../registry');
9+
var Drawing = require('../../components/drawing');
810

911
var axIds = require('./axis_ids');
1012
var getFromId = axIds.getFromId;
@@ -266,12 +268,27 @@ function padInsideLabelsOnAnchorAxis(fullLayout, ax, max) {
266268
var cosA = Math.abs(Math.cos(rad));
267269
var sinA = Math.abs(Math.sin(rad));
268270

271+
// no stashed bounding boxes - stash bounding boxes
272+
if(!anchorAxis._vals[0].bb) {
273+
var cls = anchorAxis._id + 'tick';
274+
var tickLabels = anchorAxis._selections[cls];
275+
tickLabels.each(function(d) {
276+
var thisLabel = d3.select(this);
277+
var mathjaxGroup = thisLabel.select('.text-math-group');
278+
if(mathjaxGroup.empty()) {
279+
d.bb = Drawing.bBox(thisLabel.node());
280+
}
281+
});
282+
}
283+
269284
// use bounding boxes
270285
for(var i = 0; i < anchorAxis._vals.length; i++) {
271286
var t = anchorAxis._vals[i];
272-
if(t.bb) {
273-
var w = 2 * TEXTPAD + t.bb.width;
274-
var h = 2 * TEXTPAD + t.bb.height;
287+
var bb = t.bb;
288+
289+
if(bb) {
290+
var w = 2 * TEXTPAD + bb.width;
291+
var h = 2 * TEXTPAD + bb.height;
275292

276293
pad = Math.max(pad, isX ?
277294
Math.max(w * cosA, h * sinA) :

src/plots/cartesian/axes.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3329,7 +3329,10 @@ axes.drawLabels = function(gd, ax, opts) {
33293329
function computeFinalTickLabelBoundingBoxes() {
33303330
tickLabels.each(function(d, i) {
33313331
var thisLabel = selectTickLabel(this);
3332-
ax._vals[i].bb = Drawing.bBox(thisLabel.node());
3332+
var mathjaxGroup = thisLabel.select('.text-math-group');
3333+
if(mathjaxGroup.empty()) {
3334+
ax._vals[i].bb = Drawing.bBox(thisLabel.node());
3335+
}
33333336
});
33343337
}
33353338
);

test/jasmine/tests/axes_test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,30 @@ describe('Test axes', function() {
14811481
})
14821482
.then(done, done.fail);
14831483
});
1484+
1485+
it('should make room for the inside labels of the counter axes', function(done) {
1486+
Plotly.newPlot(gd, {
1487+
data: [{
1488+
x: [1, 2, 3],
1489+
y: [0, 100, 0]
1490+
}],
1491+
layout: {
1492+
width: 300,
1493+
height: 300,
1494+
showlegend: false,
1495+
plot_bgcolor: 'lightblue',
1496+
yaxis: { ticklabelposition: 'inside' },
1497+
xaxis: { range: [1.5, 2.5] }
1498+
}
1499+
}).then(function() {
1500+
expect(gd._fullLayout.xaxis.range).toEqual([1.5, 2.5]);
1501+
1502+
return Plotly.relayout(gd, 'xaxis.autorange', true);
1503+
}).then(function() {
1504+
expect(gd._fullLayout.xaxis.range).toBeCloseToArray([0.37, 3.22], 1);
1505+
})
1506+
.then(done, done.fail);
1507+
});
14841508
});
14851509

14861510
describe('constraints relayout', function() {

0 commit comments

Comments
 (0)