Skip to content

Commit 82b453a

Browse files
author
Paul Brussee
committed
add legend.groupclick option
toggle individual items in a legendgroup or the legendgroup as a whole
1 parent 51eded4 commit 82b453a

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

src/components/legend/attributes.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ module.exports = {
9494
'Determines the behavior on legend item click.',
9595
'*toggle* toggles the visibility of the item clicked on the graph.',
9696
'*toggleothers* makes the clicked item the sole visible item on the graph.',
97-
'*false* disable legend item click interactions.'
97+
'*false* disables legend item click interactions.'
9898
].join(' ')
9999
},
100100
itemdoubleclick: {
@@ -106,7 +106,19 @@ module.exports = {
106106
'Determines the behavior on legend item double-click.',
107107
'*toggle* toggles the visibility of the item clicked on the graph.',
108108
'*toggleothers* makes the clicked item the sole visible item on the graph.',
109-
'*false* disable legend item double-click interactions.'
109+
'*false* disables legend item double-click interactions.'
110+
].join(' ')
111+
},
112+
groupclick: {
113+
valType: 'enumerated',
114+
values: ['toggleitem', 'togglegroup', false],
115+
dflt: 'togglegroup',
116+
editType: 'legend',
117+
description: [
118+
'Determines the behavior on legend group item click.',
119+
'*toggleitem* toggles the visibility of the individual item clicked on the graph.',
120+
'*togglegroup* toggles the visibility of all items in the same legendgroup as the item clicked on the graph.',
121+
'*false* disables legend group click interactions.'
110122
].join(' ')
111123
},
112124

src/components/legend/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ module.exports = function legendDefaults(layoutIn, layoutOut, fullData) {
110110

111111
coerce('itemclick');
112112
coerce('itemdoubleclick');
113+
coerce('groupclick');
113114

114115
coerce('x', defaultX);
115116
coerce('xanchor');

src/components/legend/handle_click.js

+15-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module.exports = function handleClick(g, gd, numClicks) {
1212

1313
var itemClick = fullLayout.legend.itemclick;
1414
var itemDoubleClick = fullLayout.legend.itemdoubleclick;
15+
var groupClick = fullLayout.legend.groupclick;
1516

1617
if(numClicks === 1 && itemClick === 'toggle' && itemDoubleClick === 'toggleothers' &&
1718
SHOWISOLATETIP && gd.data && gd._context.showTips
@@ -27,6 +28,8 @@ module.exports = function handleClick(g, gd, numClicks) {
2728
else if(numClicks === 2) mode = itemDoubleClick;
2829
if(!mode) return;
2930

31+
var toggleGroup = groupClick === undefined || groupClick === "togglegroup";
32+
3033
var hiddenSlices = fullLayout.hiddenlabels ?
3134
fullLayout.hiddenlabels.slice() :
3235
[];
@@ -148,10 +151,16 @@ module.exports = function handleClick(g, gd, numClicks) {
148151
}
149152

150153
if(hasLegendgroup) {
151-
for(i = 0; i < fullData.length; i++) {
152-
if(fullData[i].visible !== false && fullData[i].legendgroup === legendgroup) {
153-
setVisibility(fullData[i], nextVisibility);
154+
if(groupClick === false) return;
155+
156+
if(toggleGroup) {
157+
for(i = 0; i < fullData.length; i++) {
158+
if(fullData[i].visible !== false && fullData[i].legendgroup === legendgroup) {
159+
setVisibility(fullData[i], nextVisibility);
160+
}
154161
}
162+
} else {
163+
setVisibility(fullTrace, nextVisibility);
155164
}
156165
} else {
157166
setVisibility(fullTrace, nextVisibility);
@@ -192,7 +201,8 @@ module.exports = function handleClick(g, gd, numClicks) {
192201
// N.B. consider traces that have a set legendgroup as toggleable
193202
notInLegend = (fullData[i].showlegend !== true && !fullData[i].legendgroup);
194203
isInGroup = isClicked || (hasLegendgroup && fullData[i].legendgroup === legendgroup);
195-
setVisibility(fullData[i], (isInGroup || notInLegend) ? true : otherState);
204+
if(isInGroup && groupClick === false) continue;
205+
setVisibility(fullData[i], ((isInGroup && toggleGroup) || notInLegend) ? true : otherState);
196206
break;
197207
}
198208
}
@@ -219,7 +229,7 @@ module.exports = function handleClick(g, gd, numClicks) {
219229
for(i = 0; i < keys.length; i++) {
220230
key = keys[i];
221231
for(j = 0; j < attrIndices.length; j++) {
222-
// Use hasOwnPropety to protect against falsey values:
232+
// Use hasOwnProperty to protect against falsy values:
223233
if(!attrUpdate[key].hasOwnProperty(j)) {
224234
attrUpdate[key][j] = undefined;
225235
}

0 commit comments

Comments
 (0)