Skip to content

Commit 72c1354

Browse files
committed
add legend to traces - allow legendgroup to group inside each legend
1 parent 8630ad7 commit 72c1354

File tree

8 files changed

+359
-15
lines changed

8 files changed

+359
-15
lines changed

src/components/legend/defaults.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var basePlotLayoutAttributes = require('../../plots/layout_attributes');
1010
var helpers = require('./helpers');
1111

1212
function getLegendName(id) {
13-
return 'legend' + id;
13+
return 'legend' + (id || '');
1414
}
1515

1616
function groupDefaults(id, layoutIn, layoutOut, fullData) {
@@ -157,15 +157,15 @@ function groupDefaults(id, layoutIn, layoutOut, fullData) {
157157

158158
module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
159159
var i;
160-
var groups = [''];
160+
var legends = [0];
161161

162162
for(i = 0; i < fullData.length; i++) {
163-
Lib.pushUnique(groups, fullData[i].legendgroup);
163+
Lib.pushUnique(legends, fullData[i].legend);
164164
}
165165

166166
layoutOut._legends = [];
167-
for(i = 0; i < groups.length; i++) {
168-
var id = groups[i];
167+
for(i = 0; i < legends.length; i++) {
168+
var id = legends[i];
169169
var name = getLegendName(id);
170170
if(id && !layoutIn[name]) continue;
171171

src/components/legend/draw.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ module.exports = function draw(gd, opts) {
2828
if(opts) {
2929
drawOne(gd, opts);
3030
} else {
31-
var legendGroups = gd._fullLayout._legends;
32-
for(var i = 0; i < legendGroups.length; i++) {
33-
drawOne(gd, gd._fullLayout['legend' + legendGroups[i]]);
31+
var legends = gd._fullLayout._legends;
32+
for(var i = 0; i < legends.length; i++) {
33+
var legendObj = gd._fullLayout['legend' + (legends[i] || '')];
34+
drawOne(gd, legendObj);
3435
}
3536
}
3637
};

src/components/legend/get_legend_data.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ module.exports = function getLegendData(calcdata, opts, hasMultipleLegends) {
1616
var maxNameLength = 0;
1717
var i, j;
1818

19-
function addOneItem(legendGroup, legendItem) {
20-
if(hasMultipleLegends && legendGroup !== opts._id) return;
19+
function addOneItem(legendId, legendGroup, legendItem) {
20+
if(hasMultipleLegends && legendId !== opts._id) return;
2121

2222
// each '' legend group is treated as a separate group
2323
if(legendGroup === '' || !helpers.isGrouped(opts)) {
@@ -40,6 +40,7 @@ module.exports = function getLegendData(calcdata, opts, hasMultipleLegends) {
4040
var cd = calcdata[i];
4141
var cd0 = cd[0];
4242
var trace = cd0.trace;
43+
var lid = trace.legend;
4344
var lgroup = trace.legendgroup;
4445

4546
if(!inHover && (!trace.visible || !trace.showlegend)) continue;
@@ -51,7 +52,7 @@ module.exports = function getLegendData(calcdata, opts, hasMultipleLegends) {
5152
var labelj = cd[j].label;
5253

5354
if(!slicesShown[lgroup][labelj]) {
54-
addOneItem(lgroup, {
55+
addOneItem(lid, lgroup, {
5556
label: labelj,
5657
color: cd[j].color,
5758
i: cd[j].i,
@@ -64,7 +65,7 @@ module.exports = function getLegendData(calcdata, opts, hasMultipleLegends) {
6465
}
6566
}
6667
} else {
67-
addOneItem(lgroup, cd0);
68+
addOneItem(lid, lgroup, cd0);
6869
maxNameLength = Math.max(maxNameLength, (trace.name || '').length);
6970
}
7071
}

src/plots/attributes.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ module.exports = {
3232
'trace is shown in the legend.'
3333
].join(' ')
3434
},
35+
legend: {
36+
valType: 'number',
37+
dflt: 0,
38+
min: 2,
39+
editType: 'style',
40+
description: [
41+
'Sets the legend id to show this trace in.',
42+
'If *0* (the default value), it refers to `layout.legend`.',
43+
'If *2*, it refers to `layout.legend2`, and so on.'
44+
].join(' ')
45+
},
3546
legendgroup: {
3647
valType: 'string',
3748
dflt: '',

src/plots/plots.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,7 @@ plots.supplyTraceDefaults = function(traceIn, traceOut, colorIndex, layout, trac
13211321
'showlegend'
13221322
);
13231323

1324+
coerce('legend');
13241325
coerce('legendwidth');
13251326
coerce('legendgroup');
13261327
coerce('legendgrouptitle.text');

src/traces/parcats/attributes.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ module.exports = {
197197
customdata: undefined,
198198
hoverlabel: undefined,
199199
ids: undefined,
200+
legend: undefined,
200201
legendgroup: undefined,
201202
legendrank: undefined,
202203
opacity: undefined,

test/image/mocks/zz-multi-legends.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
},
1212
{
1313
"y": [3],
14-
"legendgroup": "2"
14+
"legend": 2
1515
},
1616
{
1717
"y": [4],
18-
"legendgroup": "3"
18+
"legend": 3
1919
},
2020
{
2121
"y": [5],
22-
"legendgroup": "3"
22+
"legend": 3
2323
}
2424
],
2525
"layout": {

0 commit comments

Comments
 (0)