@@ -453,10 +453,11 @@ function computeTextDimensions(g, gd, legendItem) {
453
453
function computeLegendDimensions ( gd , groups , traces ) {
454
454
var fullLayout = gd . _fullLayout ,
455
455
opts = fullLayout . legend ,
456
- borderwidth = opts . borderwidth ;
456
+ borderwidth = opts . borderwidth ,
457
+ isGrouped = helpers . isGrouped ( opts ) ;
457
458
458
459
if ( helpers . isVertical ( opts ) ) {
459
- if ( helpers . isGrouped ( opts ) ) {
460
+ if ( isGrouped ) {
460
461
groups . each ( function ( d , i ) {
461
462
Lib . setTranslate ( this , 0 , i * opts . tracegroupgap ) ;
462
463
} ) ;
@@ -481,7 +482,7 @@ function computeLegendDimensions(gd, groups, traces) {
481
482
opts . width += 45 + borderwidth * 2 ;
482
483
opts . height += 10 + borderwidth * 2 ;
483
484
484
- if ( helpers . isGrouped ( opts ) ) {
485
+ if ( isGrouped ) {
485
486
opts . height += ( opts . _lgroupsLength - 1 ) * opts . tracegroupgap ;
486
487
}
487
488
@@ -492,21 +493,24 @@ function computeLegendDimensions(gd, groups, traces) {
492
493
opts . width = Math . ceil ( opts . width ) ;
493
494
opts . height = Math . ceil ( opts . height ) ;
494
495
}
495
- else if ( helpers . isGrouped ( opts ) ) {
496
+ else if ( isGrouped ) {
496
497
opts . width = 0 ;
497
498
opts . height = 0 ;
498
499
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 ) {
502
505
return legendItemArray [ 0 ] . width ;
503
506
} ) ;
504
507
505
508
var groupWidth = 40 + Math . max . apply ( null , textWidths ) ;
509
+
506
510
opts . width += opts . tracegroupgap + groupWidth ;
507
511
508
512
groupXOffsets . push ( opts . width ) ;
509
- } ) ;
513
+ }
510
514
511
515
groups . each ( function ( d , i ) {
512
516
Lib . setTranslate ( this , groupXOffsets [ i ] , 0 ) ;
0 commit comments