@@ -351,6 +351,23 @@ function _draw(gd, legendObj) {
351351 } ] , gd ) ;
352352}
353353
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+
354371function clickOrDoubleClick ( gd , legend , legendItem , numClicks , evt ) {
355372 var trace = legendItem . data ( ) [ 0 ] [ 0 ] . trace ;
356373 var evtData = {
@@ -636,6 +653,7 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
636653 var isAbovePlotArea = legendObj . y > 1 || ( legendObj . y === 1 && yanchor === 'bottom' ) ;
637654
638655 var traceGroupGap = legendObj . tracegroupgap ;
656+ var legendGroupWidths = { } ;
639657
640658 // - if below/above plot area, give it the maximum potential margin-push value
641659 // - otherwise, extend the height of the plot area
@@ -688,7 +706,7 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
688706 var maxItemWidth = 0 ;
689707 var combinedItemWidth = 0 ;
690708 traces . each ( function ( d ) {
691- var w = d [ 0 ] . width + textGap ;
709+ var w = getTraceWidth ( d , legendObj , textGap ) ;
692710 maxItemWidth = Math . max ( maxItemWidth , w ) ;
693711 combinedItemWidth += w ;
694712 } ) ;
@@ -704,15 +722,16 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
704722 var maxWidthInGroup = 0 ;
705723 var offsetY = 0 ;
706724 d3 . select ( this ) . selectAll ( 'g.traces' ) . each ( function ( d ) {
707- var w = d [ 0 ] . width ;
725+ var w = getTraceWidth ( d , legendObj , textGap ) ;
708726 var h = d [ 0 ] . height ;
709727
710728 Drawing . setTranslate ( this ,
711729 titleSize [ 0 ] ,
712730 titleSize [ 1 ] + bw + itemGap + h / 2 + offsetY
713731 ) ;
714732 offsetY += h ;
715- maxWidthInGroup = Math . max ( maxWidthInGroup , textGap + w ) ;
733+ maxWidthInGroup = Math . max ( maxWidthInGroup , w ) ;
734+ legendGroupWidths [ d [ 0 ] . trace . legendgroup ] = maxWidthInGroup ;
716735 } ) ;
717736
718737 var next = maxWidthInGroup + itemGap ;
@@ -750,8 +769,12 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
750769 var rowWidth = 0 ;
751770 traces . each ( function ( d ) {
752771 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+ }
755778
756779 if ( ( next + bw + offsetX - itemGap ) >= legendObj . _maxWidth ) {
757780 maxRowWidth = Math . max ( maxRowWidth , rowWidth ) ;
@@ -802,8 +825,15 @@ function computeLegendDimensions(gd, groups, traces, legendObj) {
802825 traces . each ( function ( d ) {
803826 var traceToggle = d3 . select ( this ) . select ( '.legendtoggle' ) ;
804827 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+ }
807837 Drawing . setRect ( traceToggle , 0 , - h / 2 , w , h ) ;
808838 } ) ;
809839}
0 commit comments