Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9ebbe7d

Browse files
committed
generalize tick labels selection stash
- so that it can still be relevant for multi-category axes.
1 parent d28ed18 commit 9ebbe7d

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/plots/cartesian/axes.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,10 @@ axes.drawOne = function(gd, ax, opts) {
16371637

16381638
if(!ax.visible) return;
16391639

1640+
// stash selections to avoid DOM queries e.g.
1641+
// - stash tickLabels selection, so that drawTitle can use it to scoot title
1642+
ax._selections = {};
1643+
16401644
var transFn = axes.makeTransFn(ax);
16411645

16421646
// We remove zero lines, grid lines, and inside ticks if they're within 1px of the end
@@ -1737,12 +1741,6 @@ axes.drawOne = function(gd, ax, opts) {
17371741
});
17381742
}
17391743

1740-
// stash tickLabels selection, so that drawTitle can use it
1741-
// to scoot title w/o having to query the axis layer again
1742-
//
1743-
// TODO? Stash this per class?
1744-
ax._tickLabels = null;
1745-
17461744
var seq = [];
17471745

17481746
// tick labels - for now just the main labels.
@@ -2216,6 +2214,7 @@ axes.drawZeroLine = function(gd, ax, opts) {
22162214
* - {string} _id
22172215
* - {boolean} showticklabels
22182216
* - {number} tickangle
2217+
* - {object (optional)} _selections
22192218
* @param {object} opts
22202219
* - {array of object} vals (calcTicks output-like)
22212220
* - {d3 selection} layer
@@ -2273,8 +2272,6 @@ axes.drawLabels = function(gd, ax, opts) {
22732272

22742273
tickLabels.exit().remove();
22752274

2276-
ax._tickLabels = tickLabels;
2277-
22782275
// TODO ??
22792276
if(isAngular(ax)) {
22802277
tickLabels.each(function(d) {
@@ -2422,32 +2419,25 @@ axes.drawLabels = function(gd, ax, opts) {
24222419
}
24232420
}
24242421

2422+
if(ax._selections) {
2423+
ax._selections[cls] = tickLabels;
2424+
}
2425+
24252426
var done = Lib.syncOrAsync([allLabelsReady, fixLabelOverlaps]);
24262427
if(done && done.then) gd._promises.push(done);
24272428
return done;
24282429
};
24292430

2431+
function drawTitle(gd, ax) {
24302432
axes.drawTitle = function(gd, ax) {
24312433
var fullLayout = gd._fullLayout;
2432-
var tickLabels = ax._tickLabels;
2433-
2434-
var avoid = {
2435-
selection: tickLabels,
2436-
side: ax.side
2437-
};
2438-
24392434
var axId = ax._id;
24402435
var axLetter = axId.charAt(0);
24412436
var offsetBase = 1.5;
24422437
var gs = fullLayout._size;
24432438
var fontSize = ax.title.font.size;
24442439

24452440
var transform, counterAxis, x, y;
2446-
2447-
if(tickLabels && tickLabels.node() && tickLabels.node().parentNode) {
2448-
var translation = Drawing.getTranslate(tickLabels.node().parentNode);
2449-
avoid.offsetLeft = translation.x;
2450-
avoid.offsetTop = translation.y;
24512441
}
24522442

24532443
var titleStandoff = 10 + fontSize * offsetBase +
@@ -2467,8 +2457,6 @@ axes.drawTitle = function(gd, ax) {
24672457
fontSize * (ax.showticklabels ? 1.5 : 0.5);
24682458
}
24692459
y += counterAxis._offset;
2470-
2471-
if(!avoid.side) avoid.side = 'bottom';
24722460
}
24732461
else {
24742462
counterAxis = (ax.anchor === 'free') ?
@@ -2485,7 +2473,23 @@ axes.drawTitle = function(gd, ax) {
24852473
x += counterAxis._offset;
24862474

24872475
transform = {rotate: '-90', offset: 0};
2488-
if(!avoid.side) avoid.side = 'left';
2476+
}
2477+
2478+
var avoid;
2479+
2480+
if(ax.type !== 'multicategory') {
2481+
var tickLabels = ax._selections[ax._id + 'tick'];
2482+
2483+
avoid = {
2484+
selection: tickLabels,
2485+
side: ax.side
2486+
};
2487+
2488+
if(tickLabels && tickLabels.node() && tickLabels.node().parentNode) {
2489+
var translation = Drawing.getTranslate(tickLabels.node().parentNode);
2490+
avoid.offsetLeft = translation.x;
2491+
avoid.offsetTop = translation.y;
2492+
}
24892493
}
24902494

24912495
Titles.draw(gd, axId + 'title', {

0 commit comments

Comments
 (0)