Skip to content

Commit 3f739a3

Browse files
author
Paul Brussee
committed
add legend.groupclick option
1 parent 51eded4 commit 3f739a3

File tree

4 files changed

+39
-5
lines changed

4 files changed

+39
-5
lines changed

src/components/legend/attributes.js

+12
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,18 @@ module.exports = {
109109
'*false* disable legend item double-click interactions.'
110110
].join(' ')
111111
},
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* disable legend group click interactions.'
122+
].join(' ')
123+
},
112124

113125
x: {
114126
valType: 'number',

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
}

test/plot-schema.json

+11
Original file line numberDiff line numberDiff line change
@@ -2705,6 +2705,17 @@
27052705
false
27062706
]
27072707
},
2708+
"groupclick": {
2709+
"description": "Determines the behavior on legend group item click. *toggleitem* toggles the visibility of the individual item clicked on the graph. *togglegroup* toggles the visibility of all items in the same legendgroup as the item clicked on the graph. *false* disable legend group click interactions.",
2710+
"dflt": "togglegroup",
2711+
"editType": "legend",
2712+
"valType": "enumerated",
2713+
"values": [
2714+
"toggleitem",
2715+
"togglegroup",
2716+
false
2717+
]
2718+
},
27082719
"itemsizing": {
27092720
"description": "Determines if the legend items symbols scale with their corresponding *trace* attributes or remain *constant* independent of the symbol size on the graph.",
27102721
"dflt": "trace",

0 commit comments

Comments
 (0)