Skip to content

Commit fc236e2

Browse files
authored
Merge pull request #3635 from plotly/fix-legend-3627
legend: enable selection of a trace by clicking its symbol
2 parents 9663795 + 8d08500 commit fc236e2

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

src/components/legend/draw.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,13 @@ module.exports = function draw(gd) {
118118
})
119119
.each(function() {
120120
d3.select(this)
121-
.call(drawTexts, gd, maxLength)
122-
.call(setupTraceToggle, gd);
121+
.call(drawTexts, gd, maxLength);
123122
})
124-
.call(style, gd);
123+
.call(style, gd)
124+
.each(function() {
125+
d3.select(this)
126+
.call(setupTraceToggle, gd);
127+
});
125128

126129
Lib.syncOrAsync([Plots.previousPromises,
127130
function() {

test/jasmine/tests/legend_test.js

+27
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ var createGraphDiv = require('../assets/create_graph_div');
1515
var destroyGraphDiv = require('../assets/destroy_graph_div');
1616
var assertPlotSize = require('../assets/custom_assertions').assertPlotSize;
1717

18+
var mock = require('@mocks/legend_horizontal.json');
19+
1820
var Drawing = require('@src/components/drawing');
1921

2022
describe('legend defaults', function() {
@@ -1639,3 +1641,28 @@ describe('legend interaction', function() {
16391641
});
16401642
});
16411643
});
1644+
1645+
describe('legend DOM', function() {
1646+
'use strict';
1647+
1648+
afterEach(destroyGraphDiv);
1649+
1650+
it('draws `legendtoggle` last to make sure it is unobstructed', function(done) {
1651+
var gd = createGraphDiv();
1652+
Plotly.newPlot(gd, mock)
1653+
.then(function() {
1654+
// Find legend in figure
1655+
var legend = document.getElementsByClassName('legend')[0];
1656+
1657+
// For each legend item
1658+
var legendItems = legend.getElementsByClassName('traces');
1659+
Array.prototype.slice.call(legendItems).forEach(function(legendItem) {
1660+
// Check that the last element is our `legendtoggle`
1661+
var lastEl = legendItem.children[legendItem.children.length - 1];
1662+
expect(lastEl.getAttribute('class')).toBe('legendtoggle');
1663+
});
1664+
})
1665+
.catch(failTest)
1666+
.then(done);
1667+
});
1668+
});

0 commit comments

Comments
 (0)