@@ -24,7 +24,6 @@ var doAutoRange = require('../plots/cartesian/autorange').doAutoRange;
24
24
var SVG_TEXT_ANCHOR_START = 'start' ;
25
25
var SVG_TEXT_ANCHOR_MIDDLE = 'middle' ;
26
26
var SVG_TEXT_ANCHOR_END = 'end' ;
27
- var LINE_SPACING = alignmentConstants . LINE_SPACING ;
28
27
29
28
exports . layoutStyles = function ( gd ) {
30
29
return Lib . syncOrAsync ( [ Plots . doAutoMargin , lsInner ] , gd ) ;
@@ -405,40 +404,52 @@ exports.drawMainTitle = function(gd) {
405
404
var textAnchor = getMainTitleTextAnchor ( fullLayout ) ;
406
405
var dy = getMainTitleDy ( fullLayout ) ;
407
406
var y = getMainTitleY ( fullLayout , dy ) ;
407
+ var x = getMainTitleX ( fullLayout , textAnchor ) ;
408
408
409
- if ( title . text && title . automargin ) {
410
- var pushMargin = needsMarginPush ( gd , title ) ;
411
- if ( pushMargin > 0 ) {
412
- setDflts ( title , getDflts ( title ) [ 0 ] , getDflts ( title ) [ 1 ] ) ;
413
- // Recalculate these since the defaults have changed
414
- dy = getMainTitleDy ( fullLayout ) ;
415
- y = getMainTitleY ( fullLayout , dy ) ;
416
- applyTitleAutoMargin ( gd , y , pushMargin ) ;
417
- }
418
- }
419
-
409
+ // Draw title without positioning to get size
420
410
Titles . draw ( gd , 'gtitle' , {
421
411
propContainer : fullLayout ,
422
412
propName : 'title.text' ,
423
413
placeholder : fullLayout . _dfltTitle . plot ,
424
- attributes : {
425
- x : getMainTitleX ( fullLayout , textAnchor ) ,
414
+ attributes : ( {
415
+ x : x ,
426
416
y : y ,
427
417
'text-anchor' : textAnchor ,
428
418
dy : dy
429
- }
419
+ } )
430
420
} ) ;
421
+
422
+ if ( title . text && title . automargin ) {
423
+ var titleObj = d3 . selectAll ( '.gtitle' ) ;
424
+ var titleHeight = Drawing . bBox ( titleObj . node ( ) ) . height ;
425
+ var pushMargin = needsMarginPush ( gd , title , titleHeight ) ;
426
+ if ( pushMargin > 0 ) {
427
+ setDflts ( title , getDflts ( title ) [ 0 ] , getDflts ( title ) [ 1 ] ) ;
428
+ // Recalculate these since the defaults have changed
429
+ dy = getMainTitleDy ( fullLayout ) ;
430
+ y = getMainTitleY ( fullLayout , dy ) ;
431
+ applyTitleAutoMargin ( gd , y , pushMargin , titleHeight ) ;
432
+
433
+ // Position the title once we know where it needs to be
434
+ titleObj . attr ( {
435
+ x : x ,
436
+ y : y ,
437
+ 'text-anchor' : textAnchor ,
438
+ dy : dy
439
+ } ) . call ( svgTextUtils . positionText , x , y ) ;
440
+ }
441
+ }
431
442
} ;
432
443
433
444
434
- function isOutsideContainer ( gd , title , position , y ) {
445
+ function isOutsideContainer ( gd , title , position , y , titleHeight ) {
435
446
var plotHeight = title . yref === 'paper' ? gd . _fullLayout . _size . h : gd . _fullLayout . height ;
436
- var yPosTop = Lib . isTopAnchor ( title ) ? y : y - getTitleDepth ( title ) ; // Standardize to the top of the title
447
+ var yPosTop = Lib . isTopAnchor ( title ) ? y : y - titleHeight ; // Standardize to the top of the title
437
448
var yPosRel = position === 'b' ? plotHeight - yPosTop : yPosTop ; // Position relative to the top or bottom of plot
438
449
if ( ( Lib . isTopAnchor ( title ) && position === 't' ) || Lib . isBottomAnchor ( title ) && position === 'b' ) {
439
450
return false ;
440
451
} else {
441
- return yPosRel < getTitleDepth ( title ) ;
452
+ return yPosRel < titleHeight ;
442
453
}
443
454
}
444
455
@@ -471,13 +482,6 @@ function setDflts(title, titleY, titleYanchor) {
471
482
title . yanchor = titleYanchor ;
472
483
}
473
484
474
- function getTitleDepth ( title ) {
475
- var fontSize = title . font . size ;
476
- var extraLines = ( title . text . match ( svgTextUtils . BR_TAG_ALL ) || [ ] ) . length ;
477
- return extraLines ?
478
- fontSize * ( extraLines + 1 ) * LINE_SPACING :
479
- fontSize ;
480
- }
481
485
482
486
function containerPushVal ( position , titleY , titleYanchor , height , titleDepth ) {
483
487
var push = 0 ;
@@ -498,21 +502,21 @@ function containerPushVal(position, titleY, titleYanchor, height, titleDepth) {
498
502
return push ;
499
503
}
500
504
501
- function needsMarginPush ( gd , title ) {
505
+ function needsMarginPush ( gd , title , titleHeight ) {
502
506
var titleY = getDflts ( title ) [ 0 ] ;
503
507
var titleYanchor = getDflts ( title ) [ 1 ] ;
504
508
var position = titleY > 0.5 ? 't' : 'b' ;
505
509
var curMargin = gd . _fullLayout . margin [ position ] ;
506
510
var pushMargin = 0 ;
507
511
if ( title . yref === 'paper' ) {
508
512
pushMargin = (
509
- getTitleDepth ( title ) +
513
+ titleHeight +
510
514
title . pad . t +
511
515
title . pad . b
512
516
) ;
513
517
} else if ( title . yref === 'container' ) {
514
518
pushMargin = (
515
- containerPushVal ( position , titleY , titleYanchor , gd . _fullLayout . height , getTitleDepth ( title ) ) +
519
+ containerPushVal ( position , titleY , titleYanchor , gd . _fullLayout . height , titleHeight ) +
516
520
title . pad . t +
517
521
title . pad . b
518
522
) ;
@@ -523,7 +527,7 @@ function needsMarginPush(gd, title) {
523
527
return 0 ;
524
528
}
525
529
526
- function applyTitleAutoMargin ( gd , y , pushMargin ) {
530
+ function applyTitleAutoMargin ( gd , y , pushMargin , titleHeight ) {
527
531
var titleID = 'title.automargin' ;
528
532
var title = gd . _fullLayout . title ;
529
533
var position = title . y > 0.5 ? 't' : 'b' ;
@@ -535,7 +539,7 @@ function applyTitleAutoMargin(gd, y, pushMargin) {
535
539
} ;
536
540
var reservedPush = { } ;
537
541
538
- if ( title . yref === 'paper' && isOutsideContainer ( gd , title , position , y ) ) {
542
+ if ( title . yref === 'paper' && isOutsideContainer ( gd , title , position , y , titleHeight ) ) {
539
543
push [ position ] = pushMargin ;
540
544
} else if ( title . yref === 'container' ) {
541
545
reservedPush [ position ] = pushMargin ;
0 commit comments