@@ -69,25 +69,18 @@ module.exports = function getLegendData(calcdata, opts) {
69
69
if ( ! lgroups . length ) return [ ] ;
70
70
71
71
// rearrange lgroupToTraces into a d3-friendly array of arrays
72
- var lgroupsLength = lgroups . length ;
73
72
var legendData ;
74
73
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 ) ;
89
83
}
90
- lgroupsLength = 1 ;
91
84
}
92
85
93
86
// sort considering trace.legendrank and legend.traceorder
@@ -102,12 +95,12 @@ module.exports = function getLegendData(calcdata, opts) {
102
95
return dir * delta ;
103
96
} ;
104
97
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 ) ;
107
100
}
108
101
109
102
// number of legend groups - needed in legend/draw.js
110
- opts . _lgroupsLength = lgroupsLength ;
103
+ opts . _lgroupsLength = legendData . length ;
111
104
// maximum name/label length - needed in legend/draw.js
112
105
opts . _maxNameLength = maxNameLength ;
113
106
0 commit comments