@@ -487,6 +487,7 @@ function computeTextDimensions(g, gd) {
487
487
function computeLegendDimensions ( gd , groups , traces ) {
488
488
var fullLayout = gd . _fullLayout ;
489
489
var opts = fullLayout . legend ;
490
+ var isVertical = helpers . isVertical ( opts ) ;
490
491
var isGrouped = helpers . isGrouped ( opts ) ;
491
492
492
493
var bw = opts . borderwidth ;
@@ -497,32 +498,28 @@ function computeLegendDimensions(gd, groups, traces) {
497
498
498
499
opts . _maxWidth = fullLayout . _size . w ;
499
500
500
- var extraWidth = 0 ;
501
+ var toggleRectWidth = 0 ;
501
502
opts . _width = 0 ;
502
503
opts . _height = 0 ;
503
504
504
- if ( helpers . isVertical ( opts ) ) {
505
- if ( isGrouped ) {
506
- groups . each ( function ( d , i ) {
507
- Drawing . setTranslate ( this , 0 , i * opts . tracegroupgap ) ;
508
- } ) ;
509
- }
510
-
505
+ if ( isVertical ) {
511
506
traces . each ( function ( d ) {
512
507
var h = d [ 0 ] . height ;
513
508
Drawing . setTranslate ( this , bw , itemGap + bw + opts . _height + h / 2 ) ;
514
509
opts . _height += h ;
515
510
opts . _width = Math . max ( opts . _width , d [ 0 ] . width ) ;
516
511
} ) ;
517
512
513
+ toggleRectWidth = textGap + opts . _width ;
518
514
opts . _width += itemGap + textGap + bw2 ;
519
515
opts . _height += endPad ;
520
516
521
517
if ( isGrouped ) {
518
+ groups . each ( function ( d , i ) {
519
+ Drawing . setTranslate ( this , 0 , i * opts . tracegroupgap ) ;
520
+ } ) ;
522
521
opts . _height += ( opts . _lgroupsLength - 1 ) * opts . tracegroupgap ;
523
522
}
524
-
525
- extraWidth = textGap ;
526
523
} else {
527
524
var maxItemWidth = 0 ;
528
525
var combinedItemWidth = 0 ;
@@ -576,6 +573,7 @@ function computeLegendDimensions(gd, groups, traces) {
576
573
577
574
opts . _height = groupYOffsets [ groupYOffsets . length - 1 ] + maxGroupHeight + endPad ;
578
575
opts . _width = Math . max . apply ( null , groupXOffsets ) + maxItemWidth + textGap + bw2 ;
576
+ toggleRectWidth = maxItemWidth ;
579
577
} else {
580
578
var oneRowLegend = ( combinedItemWidth + bw2 + ( traces . size ( ) - 1 ) * itemGap ) < opts . _maxWidth ;
581
579
@@ -604,29 +602,26 @@ function computeLegendDimensions(gd, groups, traces) {
604
602
if ( oneRowLegend ) {
605
603
opts . _width = offsetX + bw2 ;
606
604
opts . _height = maxItemHeightInRow + endPad ;
605
+ toggleRectWidth = null ;
607
606
} else {
608
607
opts . _width = Math . max ( maxRowWidth , offsetX ) + bw ;
609
608
opts . _height += maxItemHeightInRow + endPad ;
609
+ toggleRectWidth = maxItemWidth ;
610
610
}
611
611
}
612
612
}
613
613
614
614
opts . _width = Math . ceil ( opts . _width ) ;
615
615
opts . _height = Math . ceil ( opts . _height ) ;
616
616
617
- var isEditable = (
618
- gd . _context . edits . legendText ||
619
- gd . _context . edits . legendPosition
620
- ) ;
621
-
617
+ var edits = gd . _context . edits ;
618
+ var isEditable = edits . legendText || edits . legendPosition ;
622
619
traces . each ( function ( d ) {
620
+ var traceToggle = d3 . select ( this ) . select ( '.legendtoggle' ) ;
623
621
var h = d [ 0 ] . height ;
624
- Drawing . setRect ( d3 . select ( this ) . select ( '.legendtoggle' ) ,
625
- 0 ,
626
- - h / 2 ,
627
- ( isEditable ? 0 : opts . _width ) + extraWidth ,
628
- h
629
- ) ;
622
+ var w = isEditable ? textGap : ( toggleRectWidth || ( textGap + d [ 0 ] . width ) ) ;
623
+ if ( ! isVertical ) w += itemGap / 2 ;
624
+ Drawing . setRect ( traceToggle , 0 , - h / 2 , w , h ) ;
630
625
} ) ;
631
626
}
632
627
0 commit comments