Skip to content

Commit 89e46b1

Browse files
authored
Merge pull request #4406 from plotly/legend-toggleothers-doot-hide-traces-not-in-legend
Fix *toggleothers* behavior for graphs with traces not in legend
2 parents 2868b25 + 950400c commit 89e46b1

File tree

2 files changed

+53
-3
lines changed

2 files changed

+53
-3
lines changed

src/components/legend/handle_click.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,12 @@ module.exports = function handleClick(g, gd, numClicks) {
165165
} else if(mode === 'toggleothers') {
166166
// Compute the clicked index. expandedIndex does what we want for expanded traces
167167
// but also culls hidden traces. That means we have some work to do.
168-
var isClicked, isInGroup, otherState;
168+
var isClicked, isInGroup, notInLegend, otherState;
169169
var isIsolated = true;
170170
for(i = 0; i < fullData.length; i++) {
171171
isClicked = fullData[i] === fullTrace;
172-
if(isClicked) continue;
172+
notInLegend = fullData[i].showlegend !== true;
173+
if(isClicked || notInLegend) continue;
173174

174175
isInGroup = (hasLegendgroup && fullData[i].legendgroup === legendgroup);
175176

@@ -194,8 +195,10 @@ module.exports = function handleClick(g, gd, numClicks) {
194195
case true:
195196
otherState = isIsolated ? true : 'legendonly';
196197
isClicked = fullData[i] === fullTrace;
198+
// N.B. consider traces that have a set legendgroup as toggleable
199+
notInLegend = (fullData[i].showlegend !== true && !fullData[i].legendgroup);
197200
isInGroup = isClicked || (hasLegendgroup && fullData[i].legendgroup === legendgroup);
198-
setVisibility(fullData[i], isInGroup ? true : otherState);
201+
setVisibility(fullData[i], (isInGroup || notInLegend) ? true : otherState);
199202
break;
200203
}
201204
}

test/jasmine/tests/legend_test.js

+47
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,53 @@ describe('legend interaction', function() {
15191519
});
15201520
});
15211521

1522+
describe('legend visibility with *showlegend:false* traces', function() {
1523+
beforeEach(function(done) {
1524+
Plotly.plot(gd, [
1525+
{y: [1, 2, 3]},
1526+
{y: [2, 3, 1]},
1527+
{type: 'heatmap', z: [[1, 2], [3, 4]], showscale: false}
1528+
])
1529+
.then(done);
1530+
});
1531+
1532+
it('isolate trace in legend, ignore trace that is not in legend', function(done) {
1533+
Promise.resolve()
1534+
.then(click(0, 2))
1535+
.then(assertVisible([true, 'legendonly', true]))
1536+
.then(click(0, 2))
1537+
.then(assertVisible([true, true, true]))
1538+
.catch(failTest).then(done);
1539+
});
1540+
1541+
it('isolate trace in legend, ignore trace that is not in legend (2)', function(done) {
1542+
Promise.resolve()
1543+
.then(click(1, 2))
1544+
.then(assertVisible(['legendonly', true, true]))
1545+
.then(click(1, 2))
1546+
.then(assertVisible([true, true, true]))
1547+
.catch(failTest).then(done);
1548+
});
1549+
1550+
it('isolate trace in legend AND trace in associated legendgroup', function(done) {
1551+
Plotly.restyle(gd, 'legendgroup', ['group', '', 'group'])
1552+
.then(click(0, 2))
1553+
.then(assertVisible([true, 'legendonly', true]))
1554+
.then(click(0, 2))
1555+
.then(assertVisible([true, true, true]))
1556+
.catch(failTest).then(done);
1557+
});
1558+
1559+
it('isolate trace in legend, hide trace not in legend that has set legendgroup', function(done) {
1560+
Plotly.restyle(gd, 'legendgroup', ['group', '', 'group'])
1561+
.then(click(1, 2))
1562+
.then(assertVisible(['legendonly', true, 'legendonly']))
1563+
.then(click(1, 2))
1564+
.then(assertVisible([true, true, true]))
1565+
.catch(failTest).then(done);
1566+
});
1567+
});
1568+
15221569
describe('custom legend click/doubleclick handlers', function() {
15231570
var fig, to;
15241571

0 commit comments

Comments
 (0)