Skip to content

Commit b0ac05f

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 b0ac05f

File tree

4 files changed

+43
-12
lines changed

4 files changed

+43
-12
lines changed

src/components/legend/attributes.js

+14-5
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ module.exports = {
8484
editType: 'legend',
8585
description: 'Sets the width (in px) of the legend item symbols (the part other than the title.text).',
8686
},
87-
8887
itemclick: {
8988
valType: 'enumerated',
9089
values: ['toggle', 'toggleothers', false],
@@ -94,7 +93,7 @@ module.exports = {
9493
'Determines the behavior on legend item click.',
9594
'*toggle* toggles the visibility of the item clicked on the graph.',
9695
'*toggleothers* makes the clicked item the sole visible item on the graph.',
97-
'*false* disable legend item click interactions.'
96+
'*false* disables legend item click interactions.'
9897
].join(' ')
9998
},
10099
itemdoubleclick: {
@@ -106,10 +105,21 @@ module.exports = {
106105
'Determines the behavior on legend item double-click.',
107106
'*toggle* toggles the visibility of the item clicked on the graph.',
108107
'*toggleothers* makes the clicked item the sole visible item on the graph.',
109-
'*false* disable legend item double-click interactions.'
108+
'*false* disables legend item double-click interactions.'
109+
].join(' ')
110+
},
111+
groupclick: {
112+
valType: 'enumerated',
113+
values: ['toggleitem', 'togglegroup', false],
114+
dflt: 'togglegroup',
115+
editType: 'legend',
116+
description: [
117+
'Determines the behavior on legend group item click.',
118+
'*toggleitem* toggles the visibility of the individual item clicked on the graph.',
119+
'*togglegroup* toggles the visibility of all items in the same legendgroup as the item clicked on the graph.',
120+
'*false* disables legend group click interactions.'
110121
].join(' ')
111122
},
112-
113123
x: {
114124
valType: 'number',
115125
min: -2,
@@ -208,6 +218,5 @@ module.exports = {
208218
},
209219
editType: 'legend',
210220
},
211-
212221
editType: 'legend'
213222
};

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -2683,8 +2683,19 @@
26832683
"valType": "number"
26842684
}
26852685
},
2686+
"groupclick": {
2687+
"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* disables legend group click interactions.",
2688+
"dflt": "togglegroup",
2689+
"editType": "legend",
2690+
"valType": "enumerated",
2691+
"values": [
2692+
"toggleitem",
2693+
"togglegroup",
2694+
false
2695+
]
2696+
},
26862697
"itemclick": {
2687-
"description": "Determines the behavior on legend item click. *toggle* toggles the visibility of the item clicked on the graph. *toggleothers* makes the clicked item the sole visible item on the graph. *false* disable legend item click interactions.",
2698+
"description": "Determines the behavior on legend item click. *toggle* toggles the visibility of the item clicked on the graph. *toggleothers* makes the clicked item the sole visible item on the graph. *false* disables legend item click interactions.",
26882699
"dflt": "toggle",
26892700
"editType": "legend",
26902701
"valType": "enumerated",
@@ -2695,7 +2706,7 @@
26952706
]
26962707
},
26972708
"itemdoubleclick": {
2698-
"description": "Determines the behavior on legend item double-click. *toggle* toggles the visibility of the item clicked on the graph. *toggleothers* makes the clicked item the sole visible item on the graph. *false* disable legend item double-click interactions.",
2709+
"description": "Determines the behavior on legend item double-click. *toggle* toggles the visibility of the item clicked on the graph. *toggleothers* makes the clicked item the sole visible item on the graph. *false* disables legend item double-click interactions.",
26992710
"dflt": "toggleothers",
27002711
"editType": "legend",
27012712
"valType": "enumerated",

0 commit comments

Comments
 (0)