Skip to content

Commit cdb570a

Browse files
committed
Add handleTitleClick function for toggling visibility via legend title
1 parent b2ef711 commit cdb570a

1 file changed

Lines changed: 62 additions & 1 deletion

File tree

src/components/legend/handle_click.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var pushUnique = Lib.pushUnique;
66

77
var SHOWISOLATETIP = true;
88

9-
module.exports = function handleClick(g, gd, numClicks) {
9+
exports.handleClick = function handleClick(g, gd, numClicks) {
1010
var fullLayout = gd._fullLayout;
1111

1212
if(gd._dragged || gd._editing) return;
@@ -273,3 +273,64 @@ module.exports = function handleClick(g, gd, numClicks) {
273273
}
274274
}
275275
};
276+
277+
exports.handleTitleClick = function handleTitleClick(gd, legendObj, mode) {
278+
var fullLayout = gd._fullLayout;
279+
var fullData = gd._fullData;
280+
var legendId = legendObj._id || 'legend';
281+
var shapesWithLegend = (fullLayout.shapes || []).filter(function(d) { return d.showlegend; });
282+
var allLegendItems = fullData.concat(shapesWithLegend);
283+
284+
function isInLegend(item) {
285+
return (item.legend || 'legend') === legendId;
286+
}
287+
288+
var toggleThisLegend;
289+
var toggleOtherLegends;
290+
291+
if(mode === 'toggle') {
292+
// If any item is visible in this legend, hide all. If all are hidden, show all
293+
var anyVisibleHere = allLegendItems.some(function(item) {
294+
return isInLegend(item) && item.visible === true;
295+
});
296+
297+
toggleThisLegend = !anyVisibleHere;
298+
toggleOtherLegends = null;
299+
} else {
300+
// isolate this legend or set all legends to visible
301+
var anyVisibleElsewhere = allLegendItems.some(function(item) {
302+
return !isInLegend(item) && item.visible === true && item.showlegend !== false;
303+
});
304+
305+
toggleThisLegend = true;
306+
toggleOtherLegends = !anyVisibleElsewhere;
307+
}
308+
309+
var dataUpdate = { visible: [] };
310+
var dataIndices = [];
311+
var updatedShapes = (fullLayout.shapes || []).map(function(d) { return d._input; });
312+
var shapesUpdated = false;
313+
314+
for(var i = 0; i < allLegendItems.length; i++) {
315+
var item = allLegendItems[i];
316+
var shouldShow = isInLegend(item) ? toggleThisLegend : toggleOtherLegends;
317+
var newVis = shouldShow ? true : 'legendonly';
318+
319+
// Only update if the item is visible and the visibility is different from the new visibility
320+
if ((item.visible !== false) && (shouldShow !== null) && (item.visible !== newVis)) {
321+
if(item._isShape) {
322+
updatedShapes[item._index].visible = newVis;
323+
shapesUpdated = true;
324+
} else {
325+
dataIndices.push(item.index);
326+
dataUpdate.visible.push(newVis);
327+
}
328+
}
329+
}
330+
331+
if(shapesUpdated) {
332+
Registry.call('_guiUpdate', gd, dataUpdate, {shapes: updatedShapes}, dataIndices);
333+
} else if(dataIndices.length) {
334+
Registry.call('_guiRestyle', gd, dataUpdate, dataIndices);
335+
}
336+
};

0 commit comments

Comments
 (0)