Skip to content

Commit 17c3244

Browse files
committed
Check if automargin is necessary before applying
1 parent 16c0bcf commit 17c3244

File tree

1 file changed

+56
-28
lines changed

1 file changed

+56
-28
lines changed

src/plot_api/subroutines.js

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -400,14 +400,20 @@ function findCounterAxisLineWidth(ax, side, counterAx, axList) {
400400

401401
exports.drawMainTitle = function(gd) {
402402
var title = gd._fullLayout.title;
403-
setDflts(title);
404403
var fullLayout = gd._fullLayout;
405404
var textAnchor = getMainTitleTextAnchor(fullLayout);
406405
var dy = getMainTitleDy(fullLayout);
407406
var y = getMainTitleY(fullLayout, dy);
408407

409408
if(title.text && title.automargin) {
410-
applyTitleAutoMargin(gd, y);
409+
var pushMargin = needsMarginPush(gd, title)
410+
if(pushMargin > 0) {
411+
setDflts(title, getDflts(title)[0], getDflts(title)[1]);
412+
// Recalculate these since the defaults have changed
413+
dy = getMainTitleDy(fullLayout);
414+
y = getMainTitleY(fullLayout, dy);
415+
applyTitleAutoMargin(gd, y, pushMargin);
416+
}
411417
}
412418

413419
Titles.draw(gd, 'gtitle', {
@@ -436,23 +442,29 @@ function isOutsideContainer(gd, title, position, y) {
436442
}
437443

438444

439-
// TODO: Move to setting defaults stage, rather than drawing
440-
// TODO: Too complex that defaults are different, depending on yref?
441445
// title.y is 1 or 0 if automargin and paper ref
442446
// 'auto' is not supported for either title.y or title.yanchor when automargin=true
443-
function setDflts(title) {
447+
function getDflts(title) {
448+
var titleY = title.y;
449+
var titleYanchor = title.yanchor;
444450
if(title.automargin && title.yref === 'paper') {
445-
title.y = title.y === 0 ? 0 : 1;
451+
titleY = title.y === 0 ? 0 : 1;
446452
if(title.yanchor === 'auto') {
447-
title.yanchor = title.y === 0 ? 'top' : 'bottom';
453+
titleYanchor = title.y === 0 ? 'top' : 'bottom';
448454
}
449455
}
450456
if(title.automargin && title.yref === 'container') {
451-
if(title.y === 'auto') title.y = 1;
457+
if(title.y === 'auto') titleY = 1;
452458
if(title.yanchor === 'auto') {
453-
title.yanchor = title.y < 0.5 ? 'bottom' : 'top';
459+
titleYanchor = title.y < 0.5 ? 'bottom' : 'top';
454460
}
455461
}
462+
return [titleY, titleYanchor]
463+
}
464+
465+
function setDflts(title, titleY, titleYanchor) {
466+
title.y = titleY;
467+
title.yanchor = titleYanchor;
456468
}
457469

458470
function titleDepth(title) {
@@ -463,30 +475,54 @@ function titleDepth(title) {
463475
fontSize;
464476
}
465477

466-
// TODO: Simplify this logic...
467-
function containerPushVal(position, title, height, titleDepth) {
478+
function containerPushVal(position, titleY, titleYanchor, height, titleDepth) {
468479
var push = 0;
469-
if(title.yanchor === 'middle') {
480+
if(titleYanchor === 'middle') {
470481
push += titleDepth / 2;
471482
}
472483
if(position === 't') {
473-
if(title.yanchor === 'top') {
484+
if(titleYanchor === 'top') {
474485
push += titleDepth;
475486
}
476-
push += (height - title.y * height);
487+
push += (height - titleY * height);
477488
} else {
478-
if(title.yanchor === 'bottom') {
489+
if(titleYanchor === 'bottom') {
479490
push += titleDepth;
480491
}
481-
push += (height - (1 - title.y) * height);
492+
push += (height - (1 - titleY) * height);
482493
}
483494
return push;
484495
}
485496

486-
function applyTitleAutoMargin(gd, y) {
497+
function needsMarginPush(gd, title) {
498+
var titleY = getDflts(title)[0];
499+
var titleYanchor = getDflts(title)[1]
500+
var position = titleY > 0.5 ? 't' : 'b';
501+
var curMargin = gd._fullLayout.margin[position]
502+
var pushMargin = 0;
503+
if(title.yref === 'paper') {
504+
pushMargin = (
505+
titleDepth(title) +
506+
title.pad.t +
507+
title.pad.b
508+
);
509+
} else if(title.yref === 'container') {
510+
pushMargin = (
511+
containerPushVal(position, titleY, titleYanchor, gd._fullLayout.height, titleDepth(title)) +
512+
title.pad.t +
513+
title.pad.b
514+
);
515+
}
516+
if(pushMargin > curMargin) {
517+
return pushMargin
518+
}
519+
return 0
520+
}
521+
522+
function applyTitleAutoMargin(gd, y, pushMargin) {
487523
var titleID = 'title.automargin';
488524
var title = gd._fullLayout.title;
489-
var position = gd._fullLayout.title.y > 0.5 ? 't' : 'b';
525+
var position = title.y > 0.5 ? 't' : 'b';
490526
var push = {
491527
x: title.x,
492528
y: title.y,
@@ -496,17 +532,9 @@ function applyTitleAutoMargin(gd, y) {
496532
var reservedPush = {};
497533

498534
if(title.yref === 'paper' && isOutsideContainer(gd, title, position, y)) {
499-
push[position] = (
500-
titleDepth(title) +
501-
title.pad.t +
502-
title.pad.b
503-
);
535+
push[position] = pushMargin;
504536
} else if(title.yref === 'container') {
505-
reservedPush[position] = (
506-
containerPushVal(position, title, gd._fullLayout.height, titleDepth(title)) +
507-
title.pad.t +
508-
title.pad.b
509-
);
537+
reservedPush[position] = pushMargin;
510538
gd._fullLayout._reservedMargin[titleID] = reservedPush;
511539
}
512540
Plots.allowAutoMargin(gd, titleID);

0 commit comments

Comments
 (0)