@@ -400,14 +400,20 @@ function findCounterAxisLineWidth(ax, side, counterAx, axList) {
400
400
401
401
exports . drawMainTitle = function ( gd ) {
402
402
var title = gd . _fullLayout . title ;
403
- setDflts ( title ) ;
404
403
var fullLayout = gd . _fullLayout ;
405
404
var textAnchor = getMainTitleTextAnchor ( fullLayout ) ;
406
405
var dy = getMainTitleDy ( fullLayout ) ;
407
406
var y = getMainTitleY ( fullLayout , dy ) ;
408
407
409
408
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
+ }
411
417
}
412
418
413
419
Titles . draw ( gd , 'gtitle' , {
@@ -436,23 +442,29 @@ function isOutsideContainer(gd, title, position, y) {
436
442
}
437
443
438
444
439
- // TODO: Move to setting defaults stage, rather than drawing
440
- // TODO: Too complex that defaults are different, depending on yref?
441
445
// title.y is 1 or 0 if automargin and paper ref
442
446
// '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 ;
444
450
if ( title . automargin && title . yref === 'paper' ) {
445
- title . y = title . y === 0 ? 0 : 1 ;
451
+ titleY = title . y === 0 ? 0 : 1 ;
446
452
if ( title . yanchor === 'auto' ) {
447
- title . yanchor = title . y === 0 ? 'top' : 'bottom' ;
453
+ titleYanchor = title . y === 0 ? 'top' : 'bottom' ;
448
454
}
449
455
}
450
456
if ( title . automargin && title . yref === 'container' ) {
451
- if ( title . y === 'auto' ) title . y = 1 ;
457
+ if ( title . y === 'auto' ) titleY = 1 ;
452
458
if ( title . yanchor === 'auto' ) {
453
- title . yanchor = title . y < 0.5 ? 'bottom' : 'top' ;
459
+ titleYanchor = title . y < 0.5 ? 'bottom' : 'top' ;
454
460
}
455
461
}
462
+ return [ titleY , titleYanchor ]
463
+ }
464
+
465
+ function setDflts ( title , titleY , titleYanchor ) {
466
+ title . y = titleY ;
467
+ title . yanchor = titleYanchor ;
456
468
}
457
469
458
470
function titleDepth ( title ) {
@@ -463,30 +475,54 @@ function titleDepth(title) {
463
475
fontSize ;
464
476
}
465
477
466
- // TODO: Simplify this logic...
467
- function containerPushVal ( position , title , height , titleDepth ) {
478
+ function containerPushVal ( position , titleY , titleYanchor , height , titleDepth ) {
468
479
var push = 0 ;
469
- if ( title . yanchor === 'middle' ) {
480
+ if ( titleYanchor === 'middle' ) {
470
481
push += titleDepth / 2 ;
471
482
}
472
483
if ( position === 't' ) {
473
- if ( title . yanchor === 'top' ) {
484
+ if ( titleYanchor === 'top' ) {
474
485
push += titleDepth ;
475
486
}
476
- push += ( height - title . y * height ) ;
487
+ push += ( height - titleY * height ) ;
477
488
} else {
478
- if ( title . yanchor === 'bottom' ) {
489
+ if ( titleYanchor === 'bottom' ) {
479
490
push += titleDepth ;
480
491
}
481
- push += ( height - ( 1 - title . y ) * height ) ;
492
+ push += ( height - ( 1 - titleY ) * height ) ;
482
493
}
483
494
return push ;
484
495
}
485
496
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 ) {
487
523
var titleID = 'title.automargin' ;
488
524
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' ;
490
526
var push = {
491
527
x : title . x ,
492
528
y : title . y ,
@@ -496,17 +532,9 @@ function applyTitleAutoMargin(gd, y) {
496
532
var reservedPush = { } ;
497
533
498
534
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 ;
504
536
} 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 ;
510
538
gd . _fullLayout . _reservedMargin [ titleID ] = reservedPush ;
511
539
}
512
540
Plots . allowAutoMargin ( gd , titleID ) ;
0 commit comments