Skip to content

Commit 915c67a

Browse files
committed
refactor - simplify logic
1 parent 274db06 commit 915c67a

File tree

1 file changed

+12
-19
lines changed

1 file changed

+12
-19
lines changed

src/components/legend/get_legend_data.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -69,25 +69,18 @@ module.exports = function getLegendData(calcdata, opts) {
6969
if(!lgroups.length) return [];
7070

7171
// rearrange lgroupToTraces into a d3-friendly array of arrays
72-
var lgroupsLength = lgroups.length;
7372
var legendData;
7473

75-
if(hasOneNonBlankGroup && helpers.isGrouped(opts)) {
76-
legendData = [];
77-
for(i = 0; i < lgroupsLength; i++) {
78-
legendData.push(
79-
lgroupToTraces[lgroups[i]]
80-
);
81-
}
82-
} else {
83-
// collapse all groups into one if all groups are blank
84-
legendData = [[]];
85-
for(i = 0; i < lgroupsLength; i++) {
86-
legendData[0].push(
87-
lgroupToTraces[lgroups[i]][0]
88-
);
74+
var shouldCollapse = !hasOneNonBlankGroup || !helpers.isGrouped(opts);
75+
legendData = shouldCollapse ? [[]] : [];
76+
for(i = 0; i < lgroups.length; i++) {
77+
var t = lgroupToTraces[lgroups[i]];
78+
if(shouldCollapse) {
79+
// collapse all groups into one if all groups are blank
80+
legendData[0].push(t[0]);
81+
} else {
82+
legendData.push(t);
8983
}
90-
lgroupsLength = 1;
9184
}
9285

9386
// sort considering trace.legendrank and legend.traceorder
@@ -102,12 +95,12 @@ module.exports = function getLegendData(calcdata, opts) {
10295
return dir * delta;
10396
};
10497

105-
for(i = 0; i < lgroupsLength; i++) {
106-
legendData[i] = legendData[i].sort(orderFn);
98+
for(i = 0; i < legendData.length; i++) {
99+
legendData[i].sort(orderFn);
107100
}
108101

109102
// number of legend groups - needed in legend/draw.js
110-
opts._lgroupsLength = lgroupsLength;
103+
opts._lgroupsLength = legendData.length;
111104
// maximum name/label length - needed in legend/draw.js
112105
opts._maxNameLength = maxNameLength;
113106

0 commit comments

Comments
 (0)