@@ -523,13 +523,26 @@ function computeTextDimensions(g, gd) {
523523 legendItem . height = Math . max ( height , 16 ) + 3 ;
524524 legendItem . width = width ;
525525 } else { // case of title
526- if ( opts . title . side . indexOf ( 'left' ) !== - 1 ) {
527- opts . _titleWidth = width ;
526+ opts . _titleWidth = width ;
527+ opts . _titleHeight = height ;
528+ }
529+ }
530+
531+ function getTitleSize ( opts ) {
532+ var w = 0 ;
533+ var h = 0 ;
534+
535+ var side = opts . title . side ;
536+ if ( side ) {
537+ if ( side . indexOf ( 'left' ) !== - 1 ) {
538+ w = opts . _titleWidth ;
528539 }
529- if ( opts . title . side . indexOf ( 'top' ) !== - 1 ) {
530- opts . _titleHeight = height ;
540+ if ( side . indexOf ( 'top' ) !== - 1 ) {
541+ h = opts . _titleHeight ;
531542 }
532543 }
544+
545+ return [ w , h ] ;
533546}
534547
535548/*
@@ -570,13 +583,14 @@ function computeLegendDimensions(gd, groups, traces) {
570583 var toggleRectWidth = 0 ;
571584 opts . _width = 0 ;
572585 opts . _height = 0 ;
586+ var titleSize = getTitleSize ( opts ) ;
573587
574588 if ( isVertical ) {
575589 traces . each ( function ( d ) {
576590 var h = d [ 0 ] . height ;
577591 Drawing . setTranslate ( this ,
578- bw + opts . _titleWidth ,
579- bw + opts . _titleHeight + opts . _height + h / 2 + itemGap
592+ bw + titleSize [ 0 ] ,
593+ bw + titleSize [ 1 ] + opts . _height + h / 2 + itemGap
580594 ) ;
581595 opts . _height += h ;
582596 opts . _width = Math . max ( opts . _width , d [ 0 ] . width ) ;
@@ -628,8 +642,8 @@ function computeLegendDimensions(gd, groups, traces) {
628642 d3 . select ( this ) . selectAll ( 'g.traces' ) . each ( function ( d ) {
629643 var h = d [ 0 ] . height ;
630644 Drawing . setTranslate ( this ,
631- opts . _titleWidth ,
632- bw + opts . _titleHeight + itemGap + h / 2 + offsetY
645+ titleSize [ 0 ] ,
646+ titleSize [ 1 ] + bw + itemGap + h / 2 + offsetY
633647 ) ;
634648 offsetY += h ;
635649 maxWidthInGroup = Math . max ( maxWidthInGroup , textGap + d [ 0 ] . width ) ;
@@ -674,8 +688,8 @@ function computeLegendDimensions(gd, groups, traces) {
674688 }
675689
676690 Drawing . setTranslate ( this ,
677- bw + opts . _titleWidth + offsetX ,
678- bw + opts . _titleHeight + offsetY + h / 2 + itemGap
691+ titleSize [ 0 ] + bw + offsetX ,
692+ titleSize [ 1 ] + bw + offsetY + h / 2 + itemGap
679693 ) ;
680694
681695 rowWidth = offsetX + w + itemGap ;
@@ -693,8 +707,19 @@ function computeLegendDimensions(gd, groups, traces) {
693707 }
694708 }
695709
696- opts . _width = Math . ceil ( opts . _width + opts . _titleWidth ) ;
697- opts . _height = Math . ceil ( opts . _height + opts . _titleHeight ) ;
710+ opts . _width = Math . ceil (
711+ Math . max (
712+ opts . _width + titleSize [ 0 ] ,
713+ opts . _titleWidth + 2 * ( bw + constants . titlePad )
714+ )
715+ ) ;
716+
717+ opts . _height = Math . ceil (
718+ Math . max (
719+ opts . _height + titleSize [ 1 ] ,
720+ opts . _titleHeight + 2 * ( bw + constants . itemGap )
721+ )
722+ ) ;
698723
699724 opts . _effHeight = Math . min ( opts . _height , opts . _maxHeight ) ;
700725
0 commit comments