@@ -351,6 +351,23 @@ function _draw(gd, legendObj) {
351
351
} ] , gd ) ;
352
352
}
353
353
354
+ function getTraceWidth ( trace , legendObj , textGap ) {
355
+ var legendItem = trace [ 0 ] ;
356
+ var legendWidth = legendItem . width ;
357
+
358
+ var traceLegendWidth = legendItem . trace . legendwidth || legendObj . entrywidth ;
359
+
360
+ if ( traceLegendWidth ) {
361
+ if ( legendObj . entrywidthmode === 'pixels' ) {
362
+ return traceLegendWidth + textGap ;
363
+ } else {
364
+ return legendObj . _maxWidth * traceLegendWidth ;
365
+ }
366
+ }
367
+
368
+ return legendWidth + textGap ;
369
+ }
370
+
354
371
function clickOrDoubleClick ( gd , legend , legendItem , numClicks , evt ) {
355
372
var trace = legendItem . data ( ) [ 0 ] [ 0 ] . trace ;
356
373
var evtData = {
@@ -636,6 +653,7 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
636
653
var isAbovePlotArea = legendObj . y > 1 || ( legendObj . y === 1 && yanchor === 'bottom' ) ;
637
654
638
655
var traceGroupGap = legendObj . tracegroupgap ;
656
+ var legendGroupWidths = { } ;
639
657
640
658
// - if below/above plot area, give it the maximum potential margin-push value
641
659
// - otherwise, extend the height of the plot area
@@ -688,7 +706,7 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
688
706
var maxItemWidth = 0 ;
689
707
var combinedItemWidth = 0 ;
690
708
traces . each ( function ( d ) {
691
- var w = d [ 0 ] . width + textGap ;
709
+ var w = getTraceWidth ( d , legendObj , textGap ) ;
692
710
maxItemWidth = Math . max ( maxItemWidth , w ) ;
693
711
combinedItemWidth += w ;
694
712
} ) ;
@@ -704,15 +722,16 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
704
722
var maxWidthInGroup = 0 ;
705
723
var offsetY = 0 ;
706
724
d3 . select ( this ) . selectAll ( 'g.traces' ) . each ( function ( d ) {
707
- var w = d [ 0 ] . width ;
725
+ var w = getTraceWidth ( d , legendObj , textGap ) ;
708
726
var h = d [ 0 ] . height ;
709
727
710
728
Drawing . setTranslate ( this ,
711
729
titleSize [ 0 ] ,
712
730
titleSize [ 1 ] + bw + itemGap + h / 2 + offsetY
713
731
) ;
714
732
offsetY += h ;
715
- maxWidthInGroup = Math . max ( maxWidthInGroup , textGap + w ) ;
733
+ maxWidthInGroup = Math . max ( maxWidthInGroup , w ) ;
734
+ legendGroupWidths [ d [ 0 ] . trace . legendgroup ] = maxWidthInGroup ;
716
735
} ) ;
717
736
718
737
var next = maxWidthInGroup + itemGap ;
@@ -750,8 +769,12 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
750
769
var rowWidth = 0 ;
751
770
traces . each ( function ( d ) {
752
771
var h = d [ 0 ] . height ;
753
- var w = textGap + d [ 0 ] . width ;
754
- var next = ( oneRowLegend ? w : maxItemWidth ) + itemGap ;
772
+ var w = getTraceWidth ( d , legendObj , textGap , isGrouped ) ;
773
+ var next = ( oneRowLegend ? w : maxItemWidth ) ;
774
+
775
+ if ( legendObj . entrywidthmode !== 'fraction' ) {
776
+ next += itemGap ;
777
+ }
755
778
756
779
if ( ( next + bw + offsetX - itemGap ) >= legendObj . _maxWidth ) {
757
780
maxRowWidth = Math . max ( maxRowWidth , rowWidth ) ;
@@ -802,8 +825,15 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
802
825
traces . each ( function ( d ) {
803
826
var traceToggle = d3 . select ( this ) . select ( '.legendtoggle' ) ;
804
827
var h = d [ 0 ] . height ;
805
- var w = isEditable ? textGap : ( toggleRectWidth || ( textGap + d [ 0 ] . width ) ) ;
806
- if ( ! isVertical ) w += itemGap / 2 ;
828
+ var legendgroup = d [ 0 ] . trace . legendgroup ;
829
+ var traceWidth = getTraceWidth ( d , legendObj , textGap ) ;
830
+ if ( isGrouped && legendgroup !== '' ) {
831
+ traceWidth = legendGroupWidths [ legendgroup ] ;
832
+ }
833
+ var w = isEditable ? textGap : ( toggleRectWidth || traceWidth ) ;
834
+ if ( ! isVertical && legendObj . entrywidthmode !== 'fraction' ) {
835
+ w += itemGap / 2 ;
836
+ }
807
837
Drawing . setRect ( traceToggle , 0 , - h / 2 , w , h ) ;
808
838
} ) ;
809
839
}
0 commit comments