Skip to content

Commit dd73d5a

Browse files
committed
Change pie influence on default legend visibility only for non-empty pies with showlegend and/or legend as arrays
1 parent 09bcc61 commit dd73d5a

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

src/components/legend/defaults.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -45,47 +45,44 @@ 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+
};
4853
fullData
49-
.filter(function(trace) { return Registry.traceIs(trace, 'pie-like'); })
54+
.filter(isPieWithLegendArray)
5055
.map(function (trace, idx) {
5156
if (trace.visible) {
5257
legendTraceCount++;
5358
}
54-
if(Array.isArray(trace.legend)) {
55-
for(var index = 0; index < trace._length; index++) {
56-
var legend = trace.legend[index] || 'legend';
57-
if(legend === legendId) {
58-
// showlegend can be undefined, boolean or a boolean array.
59-
// will fall back to default if undefined or if array index is out-of-range
60-
if(
61-
!Array.isArray(trace.showlegend)
62-
? trace.showlegend || trace._dfltShowLegend
63-
: trace.showlegend[index] == null
64-
? trace._dfltShowLegend
65-
: trace.showlegend[index]
66-
) {
67-
legendReallyHasATrace = true;
68-
legendTraceCount++;
69-
}
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 undefined, boolean or a boolean array.
63+
// will fall back to default if undefined or if array index is out-of-range
64+
if(
65+
!Array.isArray(trace.showlegend)
66+
? trace.showlegend || trace._dfltShowLegend
67+
: trace.showlegend[index] == null
68+
? trace._dfltShowLegend
69+
: trace.showlegend[index]
70+
) {
71+
legendReallyHasATrace = true;
72+
legendTraceCount++;
7073
}
7174
}
72-
if(
73-
legendId === 'legend' &&
74-
(trace._length == null || trace._length > trace.legend.length)
75-
) {
76-
for(var idx = trace.legend.length; idx < trace._length; idx++) {
75+
}
76+
if(legendId === 'legend' && trace._length > trace.legend.length) {
77+
for(var idx = trace.legend.length; idx < trace._length; idx++) {
7778
legendReallyHasATrace = true;
7879
legendTraceCount++;
79-
}
8080
}
81-
} else if(legendId === (trace.legend || 'legend')) {
82-
legendReallyHasATrace = true;
83-
legendTraceCount += trace._length || 1;
8481
}
8582
});
8683

8784
var allLegendItems = fullData.concat(shapesWithLegend).filter(function(d) {
88-
return !Registry.traceIs(trace, 'pie-like') && legendId === (d.legend || 'legend');
85+
return !isPieWithLegendArray(trace) && legendId === (d.legend || 'legend');
8986
});
9087

9188
for(var i = 0; i < allLegendItems.length; i++) {
@@ -110,9 +107,11 @@ function groupDefaults(legendId, layoutIn, layoutOut, fullData) {
110107
legendTraceCount++;
111108
if(trace.showlegend) {
112109
legendReallyHasATrace = true;
113-
// Always show the legend by default if there's only one trace
114-
// but it's explicitly shown
115-
if(trace._input.showlegend === true) {
110+
// Always show the legend by default if there's a pie,
111+
// or if there's only one trace but it's explicitly shown
112+
if(!isShape && Registry.traceIs(trace, 'pie-like') ||
113+
trace._input.showlegend === true
114+
) {
116115
legendTraceCount++;
117116
}
118117
}
@@ -130,7 +129,7 @@ function groupDefaults(legendId, layoutIn, layoutOut, fullData) {
130129
'reversed+grouped' : 'grouped';
131130
}
132131
}
133-
132+
debugger
134133
var showLegend = Lib.coerce(
135134
layoutIn,
136135
layoutOut,

0 commit comments

Comments
 (0)