@@ -453,10 +453,11 @@ function computeTextDimensions(g, gd, legendItem) {
453453function computeLegendDimensions ( gd , groups , traces ) {
454454 var fullLayout = gd . _fullLayout ,
455455 opts = fullLayout . legend ,
456- borderwidth = opts . borderwidth ;
456+ borderwidth = opts . borderwidth ,
457+ isGrouped = helpers . isGrouped ( opts ) ;
457458
458459 if ( helpers . isVertical ( opts ) ) {
459- if ( helpers . isGrouped ( opts ) ) {
460+ if ( isGrouped ) {
460461 groups . each ( function ( d , i ) {
461462 Lib . setTranslate ( this , 0 , i * opts . tracegroupgap ) ;
462463 } ) ;
@@ -481,7 +482,7 @@ function computeLegendDimensions(gd, groups, traces) {
481482 opts . width += 45 + borderwidth * 2 ;
482483 opts . height += 10 + borderwidth * 2 ;
483484
484- if ( helpers . isGrouped ( opts ) ) {
485+ if ( isGrouped ) {
485486 opts . height += ( opts . _lgroupsLength - 1 ) * opts . tracegroupgap ;
486487 }
487488
@@ -492,21 +493,24 @@ function computeLegendDimensions(gd, groups, traces) {
492493 opts . width = Math . ceil ( opts . width ) ;
493494 opts . height = Math . ceil ( opts . height ) ;
494495 }
495- else if ( helpers . isGrouped ( opts ) ) {
496+ else if ( isGrouped ) {
496497 opts . width = 0 ;
497498 opts . height = 0 ;
498499
499- var groupXOffsets = [ opts . width ] ;
500- groups . each ( function ( d ) {
501- var textWidths = d . map ( function ( legendItemArray ) {
500+ var groupXOffsets = [ opts . width ] ,
501+ groupData = groups . data ( ) ;
502+
503+ for ( var i = 0 , n = groupData . length ; i < n ; i ++ ) {
504+ var textWidths = groupData [ i ] . map ( function ( legendItemArray ) {
502505 return legendItemArray [ 0 ] . width ;
503506 } ) ;
504507
505508 var groupWidth = 40 + Math . max . apply ( null , textWidths ) ;
509+
506510 opts . width += opts . tracegroupgap + groupWidth ;
507511
508512 groupXOffsets . push ( opts . width ) ;
509- } ) ;
513+ }
510514
511515 groups . each ( function ( d , i ) {
512516 Lib . setTranslate ( this , groupXOffsets [ i ] , 0 ) ;
0 commit comments