Skip to content

Commit ff16df1

Browse files
committed
Move setup of legend trace toggle into a function
1 parent 96c0157 commit ff16df1

File tree

1 file changed

+51
-45
lines changed

1 file changed

+51
-45
lines changed

src/components/legend/draw.js

Lines changed: 51 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -124,51 +124,7 @@ module.exports = function draw(gd) {
124124
})
125125
.each(function(d, i) {
126126
drawTexts(this, gd, d, i, traces);
127-
128-
var traceToggle = d3.select(this).selectAll('rect')
129-
.data([0]);
130-
131-
traceToggle.enter().append('rect')
132-
.classed('legendtoggle', true)
133-
.style('cursor', 'pointer')
134-
.attr('pointer-events', 'all')
135-
.call(Color.fill, 'rgba(0,0,0,0)');
136-
137-
traceToggle.on('click', function() {
138-
if(gd._dragged) return;
139-
140-
var fullData = gd._fullData,
141-
trace = d[0].trace,
142-
legendgroup = trace.legendgroup,
143-
traceIndicesInGroup = [],
144-
tracei,
145-
newVisible;
146-
147-
if(Plots.traceIs(trace, 'pie')) {
148-
var thisLabel = d[0].label,
149-
newHiddenSlices = hiddenSlices.slice(),
150-
thisLabelIndex = newHiddenSlices.indexOf(thisLabel);
151-
152-
if(thisLabelIndex === -1) newHiddenSlices.push(thisLabel);
153-
else newHiddenSlices.splice(thisLabelIndex, 1);
154-
155-
Plotly.relayout(gd, 'hiddenlabels', newHiddenSlices);
156-
} else {
157-
if(legendgroup === '') {
158-
traceIndicesInGroup = [trace.index];
159-
} else {
160-
for(var i = 0; i < fullData.length; i++) {
161-
tracei = fullData[i];
162-
if(tracei.legendgroup === legendgroup) {
163-
traceIndicesInGroup.push(tracei.index);
164-
}
165-
}
166-
}
167-
168-
newVisible = trace.visible === true ? 'legendonly' : true;
169-
Plotly.restyle(gd, 'visible', newVisible, traceIndicesInGroup);
170-
}
171-
});
127+
setupTraceToggle(gd, this, d[0]);
172128
});
173129

174130
// Position and size the legend
@@ -400,6 +356,56 @@ function drawTexts(context, gd, d, i, traces) {
400356
else text.call(textLayout);
401357
}
402358

359+
function setupTraceToggle(gd, container, legendItem) {
360+
var hiddenSlices = gd._fullLayout.hiddenlabels ?
361+
gd._fullLayout.hiddenlabels.slice() :
362+
[];
363+
364+
var traceToggle = d3.select(container).selectAll('rect')
365+
.data([0]);
366+
367+
traceToggle.enter().append('rect')
368+
.classed('legendtoggle', true)
369+
.style('cursor', 'pointer')
370+
.attr('pointer-events', 'all')
371+
.call(Color.fill, 'rgba(0,0,0,0)');
372+
373+
traceToggle.on('click', function() {
374+
if(gd._dragged) return;
375+
376+
var fullData = gd._fullData,
377+
trace = legendItem.trace,
378+
legendgroup = trace.legendgroup,
379+
traceIndicesInGroup = [],
380+
tracei,
381+
newVisible;
382+
383+
if(Plots.traceIs(trace, 'pie')) {
384+
var thisLabel = legendItem.label,
385+
thisLabelIndex = hiddenSlices.indexOf(thisLabel);
386+
387+
if(thisLabelIndex === -1) hiddenSlices.push(thisLabel);
388+
else hiddenSlices.splice(thisLabelIndex, 1);
389+
390+
Plotly.relayout(gd, 'hiddenlabels', hiddenSlices);
391+
} else {
392+
if(legendgroup === '') {
393+
traceIndicesInGroup = [trace.index];
394+
} else {
395+
for(var i = 0; i < fullData.length; i++) {
396+
tracei = fullData[i];
397+
if(tracei.legendgroup === legendgroup) {
398+
traceIndicesInGroup.push(tracei.index);
399+
}
400+
}
401+
}
402+
403+
newVisible = trace.visible === true ? 'legendonly' : true;
404+
Plotly.restyle(gd, 'visible', newVisible, traceIndicesInGroup);
405+
}
406+
});
407+
}
408+
403409
function computeLegendDimensions(gd, traces) {
404410
var fullLayout = gd._fullLayout,
405411
opts = fullLayout.legend,

0 commit comments

Comments
 (0)