Skip to content

Commit 1b69395

Browse files
committed
move legend-data logic to own module
1 parent 09354bf commit 1b69395

File tree

3 files changed

+105
-87
lines changed

3 files changed

+105
-87
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* Copyright 2012-2016, Plotly, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*/
8+
9+
10+
'use strict';
11+
12+
var Plots = require('../../plots/plots');
13+
14+
var helpers = require('./helpers');
15+
16+
17+
module.exports = function getLegendData(calcdata, opts) {
18+
var lgroupToTraces = {},
19+
lgroups = [],
20+
hasOneNonBlankGroup = false,
21+
slicesShown = {},
22+
lgroupi = 0;
23+
24+
var i, j;
25+
26+
function addOneItem(legendGroup, legendItem) {
27+
// each '' legend group is treated as a separate group
28+
if(legendGroup === '' || !helpers.isGrouped(opts)) {
29+
var uniqueGroup = '~~i' + lgroupi; // TODO: check this against fullData legendgroups?
30+
31+
lgroups.push(uniqueGroup);
32+
lgroupToTraces[uniqueGroup] = [[legendItem]];
33+
lgroupi++;
34+
}
35+
else if(lgroups.indexOf(legendGroup) === -1) {
36+
lgroups.push(legendGroup);
37+
hasOneNonBlankGroup = true;
38+
lgroupToTraces[legendGroup] = [[legendItem]];
39+
}
40+
else lgroupToTraces[legendGroup].push([legendItem]);
41+
}
42+
43+
// build an { legendgroup: [cd0, cd0], ... } object
44+
for(i = 0; i < calcdata.length; i++) {
45+
var cd = calcdata[i],
46+
cd0 = cd[0],
47+
trace = cd0.trace,
48+
lgroup = trace.legendgroup;
49+
50+
if(!helpers.legendGetsTrace(trace) || !trace.showlegend) continue;
51+
52+
if(Plots.traceIs(trace, 'pie')) {
53+
if(!slicesShown[lgroup]) slicesShown[lgroup] = {};
54+
55+
for(j = 0; j < cd.length; j++) {
56+
var labelj = cd[j].label;
57+
58+
if(!slicesShown[lgroup][labelj]) {
59+
addOneItem(lgroup, {
60+
label: labelj,
61+
color: cd[j].color,
62+
i: cd[j].i,
63+
trace: trace
64+
});
65+
66+
slicesShown[lgroup][labelj] = true;
67+
}
68+
}
69+
}
70+
71+
else addOneItem(lgroup, cd0);
72+
}
73+
74+
// won't draw a legend in this case
75+
if(!lgroups.length) return [];
76+
77+
// rearrange lgroupToTraces into a d3-friendly array of arrays
78+
var lgroupsLength = lgroups.length,
79+
ltraces,
80+
legendData;
81+
82+
if(hasOneNonBlankGroup && helpers.isGrouped(opts)) {
83+
legendData = new Array(lgroupsLength);
84+
85+
for(i = 0; i < lgroupsLength; i++) {
86+
ltraces = lgroupToTraces[lgroups[i]];
87+
legendData[i] = helpers.isReversed(opts) ? ltraces.reverse() : ltraces;
88+
}
89+
}
90+
else {
91+
// collapse all groups into one if all groups are blank
92+
legendData = [new Array(lgroupsLength)];
93+
94+
for(i = 0; i < lgroupsLength; i++) {
95+
ltraces = lgroupToTraces[lgroups[i]][0];
96+
legendData[0][helpers.isReversed(opts) ? lgroupsLength-i-1 : i] = ltraces;
97+
}
98+
lgroupsLength = 1;
99+
}
100+
101+
// needed in repositionLegend
102+
opts._lgroupsLength = lgroupsLength;
103+
return legendData;
104+
};

src/components/legend/index.js

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -75,91 +75,6 @@ legend.texts = function(context, td, d, i, traces) {
7575
else text.call(textLayout);
7676
};
7777

78-
legend.getLegendData = function(calcdata, opts) {
79-
80-
// build an { legendgroup: [cd0, cd0], ... } object
81-
var lgroupToTraces = {},
82-
lgroups = [],
83-
hasOneNonBlankGroup = false,
84-
slicesShown = {},
85-
lgroupi = 0;
86-
87-
var cd, cd0, trace, lgroup, i, j, labelj;
88-
89-
function addOneItem(legendGroup, legendItem) {
90-
// each '' legend group is treated as a separate group
91-
if(legendGroup==='' || !isGrouped(opts)) {
92-
var uniqueGroup = '~~i' + lgroupi; // TODO: check this against fullData legendgroups?
93-
lgroups.push(uniqueGroup);
94-
lgroupToTraces[uniqueGroup] = [[legendItem]];
95-
lgroupi++;
96-
}
97-
else if(lgroups.indexOf(legendGroup) === -1) {
98-
lgroups.push(legendGroup);
99-
hasOneNonBlankGroup = true;
100-
lgroupToTraces[legendGroup] = [[legendItem]];
101-
}
102-
else lgroupToTraces[legendGroup].push([legendItem]);
103-
}
104-
105-
for(i = 0; i < calcdata.length; i++) {
106-
cd = calcdata[i];
107-
cd0 = cd[0];
108-
trace = cd0.trace;
109-
lgroup = trace.legendgroup;
110-
111-
if(!legendGetsTrace(trace) || !trace.showlegend) continue;
112-
113-
if(Plots.traceIs(trace, 'pie')) {
114-
if(!slicesShown[lgroup]) slicesShown[lgroup] = {};
115-
for(j = 0; j < cd.length; j++) {
116-
labelj = cd[j].label;
117-
if(!slicesShown[lgroup][labelj]) {
118-
addOneItem(lgroup, {
119-
label: labelj,
120-
color: cd[j].color,
121-
i: cd[j].i,
122-
trace: trace
123-
});
124-
125-
slicesShown[lgroup][labelj] = true;
126-
}
127-
}
128-
}
129-
130-
else addOneItem(lgroup, cd0);
131-
}
132-
133-
// won't draw a legend in this case
134-
if(!lgroups.length) return [];
135-
136-
// rearrange lgroupToTraces into a d3-friendly array of arrays
137-
var lgroupsLength = lgroups.length,
138-
ltraces,
139-
legendData;
140-
141-
if(hasOneNonBlankGroup && isGrouped(opts)) {
142-
legendData = new Array(lgroupsLength);
143-
for(i = 0; i < lgroupsLength; i++) {
144-
ltraces = lgroupToTraces[lgroups[i]];
145-
legendData[i] = isReversed(opts) ? ltraces.reverse() : ltraces;
146-
}
147-
}
148-
else {
149-
// collapse all groups into one if all groups are blank
150-
legendData = [new Array(lgroupsLength)];
151-
for(i = 0; i < lgroupsLength; i++) {
152-
ltraces = lgroupToTraces[lgroups[i]][0];
153-
legendData[0][isReversed(opts) ? lgroupsLength-i-1 : i] = ltraces;
154-
}
155-
lgroupsLength = 1;
156-
}
157-
158-
// needed in repositionLegend
159-
opts._lgroupsLength = lgroupsLength;
160-
return legendData;
161-
};
162-
16378
legend.draw = function(td) {
16479
var fullLayout = td._fullLayout;
16580

test/jasmine/tests/legend_test.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var Legend = require('@src/components/legend');
22
var Plots = require('@src/plots/plots');
33

4+
var getLegendData = require('@src/components/legend/get_legend_data');
45
var helpers = require('@src/components/legend/helpers');
56
var anchorUtils = require('@src/components/legend/anchor_utils');
67
describe('Test legend:', function() {
@@ -65,8 +66,6 @@ describe('Test legend:', function() {
6566
});
6667

6768
describe('getLegendData', function() {
68-
var getLegendData = Legend.getLegendData;
69-
7069
var calcdata, opts, legendData, expected;
7170

7271
it('should group legendgroup traces', function() {

0 commit comments

Comments
 (0)