@@ -45,8 +45,39 @@ function groupDefaults(legendId, layoutIn, layoutOut, fullData) {
4545
4646 var shapesWithLegend = ( layoutOut . shapes || [ ] ) . filter ( function ( d ) { return d . showlegend ; } ) ;
4747
48+ function isPieWithLegendArray ( trace ) {
49+ return Registry . traceIs ( trace , 'pie-like' )
50+ && trace . _length != null
51+ && ( Array . isArray ( trace . legend ) || Array . isArray ( trace . showlegend ) ) ;
52+ } ;
53+ fullData
54+ . filter ( isPieWithLegendArray )
55+ . forEach ( function ( trace ) {
56+ if ( trace . visible ) {
57+ legendTraceCount ++ ;
58+ }
59+ for ( var index = 0 ; index < trace . _length ; index ++ ) {
60+ var legend = ( Array . isArray ( trace . legend ) ? trace . legend [ index ] : trace . legend ) || 'legend' ;
61+ if ( legend === legendId ) {
62+ // showlegend can be boolean or a boolean array.
63+ // will fall back to default if array index is out-of-range
64+ const showInLegend = Array . isArray ( trace . showlegend ) ? trace . showlegend [ index ] : trace . showlegend ;
65+ if ( showInLegend || trace . _dfltShowLegend ) {
66+ legendReallyHasATrace = true ;
67+ legendTraceCount ++ ;
68+ }
69+ }
70+ }
71+ if ( legendId === 'legend' && trace . _length > trace . legend . length ) {
72+ for ( var idx = trace . legend . length ; idx < trace . _length ; idx ++ ) {
73+ legendReallyHasATrace = true ;
74+ legendTraceCount ++ ;
75+ }
76+ }
77+ } ) ;
78+
4879 var allLegendItems = fullData . concat ( shapesWithLegend ) . filter ( function ( d ) {
49- return legendId === ( d . legend || 'legend' ) ;
80+ return ! isPieWithLegendArray ( trace ) && legendId === ( d . legend || 'legend' ) ;
5081 } ) ;
5182
5283 for ( var i = 0 ; i < allLegendItems . length ; i ++ ) {
@@ -82,7 +113,6 @@ function groupDefaults(legendId, layoutIn, layoutOut, fullData) {
82113
83114 Lib . coerceFont ( traceCoerce , 'legendgrouptitle.font' , grouptitlefont ) ;
84115 }
85-
86116 if ( ( ! isShape && Registry . traceIs ( trace , 'bar' ) && layoutOut . barmode === 'stack' ) ||
87117 [ 'tonextx' , 'tonexty' ] . indexOf ( trace . fill ) !== - 1 ) {
88118 defaultOrder = helpers . isGrouped ( { traceorder : defaultOrder } ) ?
@@ -95,9 +125,15 @@ function groupDefaults(legendId, layoutIn, layoutOut, fullData) {
95125 }
96126 }
97127
98- var showLegend = Lib . coerce ( layoutIn , layoutOut ,
99- basePlotLayoutAttributes , 'showlegend' ,
100- legendReallyHasATrace && ( legendTraceCount > ( legendId === 'legend' ? 1 : 0 ) ) ) ;
128+ var showLegend = Lib . coerce (
129+ layoutIn ,
130+ layoutOut ,
131+ basePlotLayoutAttributes ,
132+ 'showlegend' ,
133+ layoutOut . showlegend ||
134+ ( legendReallyHasATrace &&
135+ legendTraceCount > ( legendId === 'legend' ? 1 : 0 ) )
136+ ) ;
101137
102138 // delete legend
103139 if ( showLegend === false ) layoutOut [ legendId ] = undefined ;
@@ -230,7 +266,11 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
230266
231267 var legends = [ 'legend' ] ;
232268 for ( i = 0 ; i < allLegendsData . length ; i ++ ) {
233- Lib . pushUnique ( legends , allLegendsData [ i ] . legend ) ;
269+ if ( Array . isArray ( allLegendsData [ i ] . legend ) ) {
270+ legends = legends . concat ( allLegendsData [ i ] . legend ) ;
271+ } else {
272+ Lib . pushUnique ( legends , allLegendsData [ i ] . legend ) ;
273+ }
234274 }
235275
236276 layoutOut . _legends = [ ] ;
0 commit comments