Skip to content

Commit 6111aba

Browse files
committed
fix double click shape legends
1 parent b35add8 commit 6111aba

File tree

3 files changed

+54
-24
lines changed

3 files changed

+54
-24
lines changed

src/components/legend/draw.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ function drawOne(gd, opts) {
8585
if(!shape.showlegend) continue;
8686

8787
var shapeLegend = {
88+
_isShape: true,
8889
_fullInput: shape,
8990
index: shape._index,
9091
name: shape.name || shape.label.text || ('shape ' + shape._index),

src/components/legend/handle_click.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,28 @@ module.exports = function handleClick(g, gd, numClicks) {
3838
var legendItem = g.data()[0][0];
3939
if(legendItem.groupTitle && legendItem.noClick) return;
4040

41+
var i, j;
4142
var fullData = gd._fullData;
43+
var allLegendItems = fullData.slice();
44+
if(fullLayout.shapes) {
45+
for(i = 0; i < fullLayout.shapes.length; i++) {
46+
var shapeLegend = fullLayout.shapes[i]; // TODO: make a copy instead!
47+
if(shapeLegend.visible) {
48+
shapeLegend.index = i;
49+
shapeLegend._fullInput = shapeLegend;
50+
allLegendItems.push(shapeLegend);
51+
}
52+
}
53+
}
54+
4255
var fullTrace = legendItem.trace;
56+
if(fullTrace._isShape) {
57+
fullTrace = fullTrace._fullInput;
58+
}
59+
4360
var legendgroup = fullTrace.legendgroup;
4461

45-
var i, j, kcont, key, keys, val;
62+
var kcont, key, keys, val;
4663
var dataUpdate = {};
4764
var dataIndices = [];
4865
var carrs = [];
@@ -81,7 +98,6 @@ module.exports = function handleClick(g, gd, numClicks) {
8198

8299
var fullInput = fullTrace._fullInput;
83100
var isShape = fullInput._isShape;
84-
if(isShape) fullInput = fullTrace;
85101
var index = fullInput.index;
86102

87103
if(Registry.hasTransform(fullInput, 'groupby')) {
@@ -171,8 +187,8 @@ module.exports = function handleClick(g, gd, numClicks) {
171187
var traceIndicesInGroup = [];
172188
var tracei;
173189
if(hasLegendgroup) {
174-
for(i = 0; i < fullData.length; i++) {
175-
tracei = fullData[i];
190+
for(i = 0; i < allLegendItems.length; i++) {
191+
tracei = allLegendItems[i];
176192
if(!tracei.visible) continue;
177193
if(tracei.legendgroup === legendgroup) {
178194
traceIndicesInGroup.push(i);
@@ -197,15 +213,9 @@ module.exports = function handleClick(g, gd, numClicks) {
197213

198214
if(hasLegendgroup) {
199215
if(toggleGroup) {
200-
var allLegendItems = fullData.concat(fullLayout.shapes || []);
201216
for(i = 0; i < allLegendItems.length; i++) {
202217
var item = allLegendItems[i];
203218
if(item.visible !== false && item.legendgroup === legendgroup) {
204-
if(i > fullData.length) { // case of shapes
205-
item.index = i - fullData.length;
206-
item._isShape = true;
207-
item._fullInput = item;
208-
}
209219
setVisibility(item, nextVisibility);
210220
}
211221
}
@@ -218,40 +228,43 @@ module.exports = function handleClick(g, gd, numClicks) {
218228
} else if(mode === 'toggleothers') {
219229
// Compute the clicked index. expandedIndex does what we want for expanded traces
220230
// but also culls hidden traces. That means we have some work to do.
221-
var isClicked, isInGroup, notInLegend, otherState;
231+
var isClicked, isInGroup, notInLegend, otherState, _item;
222232
var isIsolated = true;
223-
for(i = 0; i < fullData.length; i++) {
224-
isClicked = fullData[i] === fullTrace;
225-
notInLegend = fullData[i].showlegend !== true;
233+
for(i = 0; i < allLegendItems.length; i++) {
234+
_item = allLegendItems[i];
235+
isClicked = _item === fullTrace;
236+
notInLegend = _item.showlegend !== true;
226237
if(isClicked || notInLegend) continue;
227238

228-
isInGroup = (hasLegendgroup && fullData[i].legendgroup === legendgroup);
239+
isInGroup = (hasLegendgroup && _item.legendgroup === legendgroup);
229240

230-
if(!isInGroup && fullData[i].visible === true && !Registry.traceIs(fullData[i], 'notLegendIsolatable')) {
241+
if(!isInGroup && _item.visible === true && !Registry.traceIs(_item, 'notLegendIsolatable')) {
231242
isIsolated = false;
232243
break;
233244
}
234245
}
235246

236-
for(i = 0; i < fullData.length; i++) {
247+
for(i = 0; i < allLegendItems.length; i++) {
248+
_item = allLegendItems[i];
249+
237250
// False is sticky; we don't change it. Also ensure we don't change states of itmes in other legend
238-
if(fullData[i].visible === false || fullData[i].legend !== thisLegend) continue;
251+
if(_item.visible === false || _item.legend !== thisLegend) continue;
239252

240-
if(Registry.traceIs(fullData[i], 'notLegendIsolatable')) {
253+
if(Registry.traceIs(_item, 'notLegendIsolatable')) {
241254
continue;
242255
}
243256

244257
switch(fullTrace.visible) {
245258
case 'legendonly':
246-
setVisibility(fullData[i], true);
259+
setVisibility(_item, true);
247260
break;
248261
case true:
249262
otherState = isIsolated ? true : 'legendonly';
250-
isClicked = fullData[i] === fullTrace;
263+
isClicked = _item === fullTrace;
251264
// N.B. consider traces that have a set legendgroup as toggleable
252-
notInLegend = (fullData[i].showlegend !== true && !fullData[i].legendgroup);
253-
isInGroup = isClicked || (hasLegendgroup && fullData[i].legendgroup === legendgroup);
254-
setVisibility(fullData[i], (isInGroup || notInLegend) ? true : otherState);
265+
notInLegend = (_item.showlegend !== true && !_item.legendgroup);
266+
isInGroup = isClicked || (hasLegendgroup && _item.legendgroup === legendgroup);
267+
setVisibility(_item, (isInGroup || notInLegend) ? true : otherState);
255268
break;
256269
}
257270
}

test/jasmine/tests/legend_test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1952,6 +1952,22 @@ describe('legend interaction', function() {
19521952
.then(assertVisibleShapes([false, 'legendonly', 'legendonly']))
19531953
.then(done, done.fail);
19541954
});
1955+
1956+
it('double-clicking isolates a visible trace ', function(done) {
1957+
Promise.resolve()
1958+
.then(click(0))
1959+
.then(assertVisibleShapes([false, true, true]))
1960+
.then(click(0, 2))
1961+
.then(assertVisibleShapes([false, true, 'legendonly']))
1962+
.then(done, done.fail);
1963+
});
1964+
1965+
it('double-clicking an isolated trace shows all non-hidden traces', function(done) {
1966+
Promise.resolve()
1967+
.then(click(0, 2))
1968+
.then(assertVisibleShapes([false, true, true]))
1969+
.then(done, done.fail);
1970+
});
19551971
});
19561972

19571973
describe('legendgroup visibility', function() {

0 commit comments

Comments
 (0)