Skip to content

Commit

Permalink
Merge pull request #5610 from plotly/fix5601
Browse files Browse the repository at this point in the history
Take into account inside labels of the counter axis during autorange relayout
  • Loading branch information
archmoj authored Apr 27, 2021
2 parents 32d4631 + b676bc6 commit a5a5de9
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
23 changes: 20 additions & 3 deletions src/plots/cartesian/autorange.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

var d3 = require('@plotly/d3');
var isNumeric = require('fast-isnumeric');

var Lib = require('../../lib');
var FP_SAFE = require('../../constants/numerical').FP_SAFE;
var Registry = require('../../registry');
var Drawing = require('../../components/drawing');

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

// no stashed bounding boxes - stash bounding boxes
if(!anchorAxis._vals[0].bb) {
var cls = anchorAxis._id + 'tick';
var tickLabels = anchorAxis._selections[cls];
tickLabels.each(function(d) {
var thisLabel = d3.select(this);
var mathjaxGroup = thisLabel.select('.text-math-group');
if(mathjaxGroup.empty()) {
d.bb = Drawing.bBox(thisLabel.node());
}
});
}

// use bounding boxes
for(var i = 0; i < anchorAxis._vals.length; i++) {
var t = anchorAxis._vals[i];
if(t.bb) {
var w = 2 * TEXTPAD + t.bb.width;
var h = 2 * TEXTPAD + t.bb.height;
var bb = t.bb;

if(bb) {
var w = 2 * TEXTPAD + bb.width;
var h = 2 * TEXTPAD + bb.height;

pad = Math.max(pad, isX ?
Math.max(w * cosA, h * sinA) :
Expand Down
5 changes: 4 additions & 1 deletion src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3329,7 +3329,10 @@ axes.drawLabels = function(gd, ax, opts) {
function computeFinalTickLabelBoundingBoxes() {
tickLabels.each(function(d, i) {
var thisLabel = selectTickLabel(this);
ax._vals[i].bb = Drawing.bBox(thisLabel.node());
var mathjaxGroup = thisLabel.select('.text-math-group');
if(mathjaxGroup.empty()) {
ax._vals[i].bb = Drawing.bBox(thisLabel.node());
}
});
}
);
Expand Down
24 changes: 24 additions & 0 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,30 @@ describe('Test axes', function() {
})
.then(done, done.fail);
});

it('should make room for the inside labels of the counter axes', function(done) {
Plotly.newPlot(gd, {
data: [{
x: [1, 2, 3],
y: [0, 100, 0]
}],
layout: {
width: 300,
height: 300,
showlegend: false,
plot_bgcolor: 'lightblue',
yaxis: { ticklabelposition: 'inside' },
xaxis: { range: [1.5, 2.5] }
}
}).then(function() {
expect(gd._fullLayout.xaxis.range).toEqual([1.5, 2.5]);

return Plotly.relayout(gd, 'xaxis.autorange', true);
}).then(function() {
expect(gd._fullLayout.xaxis.range).toBeCloseToArray([0.37, 3.22], 1);
})
.then(done, done.fail);
});
});

describe('constraints relayout', function() {
Expand Down

0 comments on commit a5a5de9

Please sign in to comment.