From c262d4bff6df5caab93fcce286b53113c422ede1 Mon Sep 17 00:00:00 2001 From: archmoj Date: Wed, 18 Nov 2020 17:45:51 -0500 Subject: [PATCH] store and use ticklabel bounding boxes to increase pad for inside labels --- src/plots/cartesian/autorange.js | 18 ++++++++++++++---- src/plots/cartesian/axes.js | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/plots/cartesian/autorange.js b/src/plots/cartesian/autorange.js index c43e7deec38..280ffa0e788 100644 --- a/src/plots/cartesian/autorange.js +++ b/src/plots/cartesian/autorange.js @@ -225,14 +225,24 @@ function makePadFn(ax, max) { anchorAxis.side === 'right' ) )) { - var fontSize = anchorAxis.tickfont ? anchorAxis.tickfont.size : 12; - var newPad = fontSize * (ax._id.charAt(0) === 'x' ? 4 : 1); + var isX = ax._id.charAt(0) === 'x'; + + var morePad = 0; + if(anchorAxis._vals) { + // use bounding boxes + morePad = 0; + anchorAxis._vals.forEach(function(t) { + if(t.bb) { + morePad = Math.max(morePad, isX ? t.bb.width : t.bb.height); + } + }); + } if(anchorAxis.ticks === 'inside' && anchorAxis.ticklabelposition === 'inside') { - newPad += anchorAxis.ticklen || 0; + morePad += anchorAxis.ticklen || 0; } - extrappad = Math.max(extrappad, newPad); + extrappad += morePad; } } diff --git a/src/plots/cartesian/axes.js b/src/plots/cartesian/axes.js index ee510be3f00..02568528467 100644 --- a/src/plots/cartesian/axes.js +++ b/src/plots/cartesian/axes.js @@ -3140,6 +3140,20 @@ 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()); + }); + } + + if( + (ax._anchorAxis || {}).autorange && + (ax.ticklabelposition || '').indexOf('inside') !== -1 + ) { + seq.push(computeFinalTickLabelBoundingBoxes); + } + var done = Lib.syncOrAsync(seq); if(done && done.then) gd._promises.push(done); return done;