Skip to content

Rewrite legend visibility toggling #2022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Sep 20, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions src/components/legend/draw.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,15 +413,15 @@ function drawTexts(g, gd) {
var groupbyIndices = Registry.getTransformIndices(fullInput, 'groupby');
var index = groupbyIndices[groupbyIndices.length - 1];

var carr = Lib.keyedContainer(fullInput, 'transforms[' + index + '].styles', 'target', 'value.name');
var kcont = Lib.keyedContainer(fullInput, 'transforms[' + index + '].styles', 'target', 'value.name');

if(origText === '') {
carr.remove(legendItem.trace._group);
kcont.remove(legendItem.trace._group);
} else {
carr.set(legendItem.trace._group, text);
kcont.set(legendItem.trace._group, text);
}

update = carr.constructUpdate();
update = kcont.constructUpdate();
} else {
update.name = text;
}
Expand Down Expand Up @@ -490,7 +490,7 @@ function handleClick(g, gd, numClicks) {
var fullTrace = legendItem.trace;
var legendgroup = fullTrace.legendgroup;

var i, j, carr, key, keys, val;
var i, j, kcont, key, keys, val;
var attrUpdate = {};
var attrIndices = [];
var carrs = [];
Expand All @@ -516,20 +516,28 @@ function handleClick(g, gd, numClicks) {
function setVisibility(fullTrace, visibility) {
var fullInput = fullTrace._fullInput;
if(Registry.hasTransform(fullInput, 'groupby')) {
var carr = carrs[fullInput.index];
if(!carr) {
var kcont = carrs[fullInput.index];
if(!kcont) {
var groupbyIndices = Registry.getTransformIndices(fullInput, 'groupby');
var lastGroupbyIndex = groupbyIndices[groupbyIndices.length - 1];
carr = Lib.keyedContainer(fullInput, 'transforms[' + lastGroupbyIndex + '].styles', 'target', 'value.visible');
carrs[fullInput.index] = carr;
kcont = Lib.keyedContainer(fullInput, 'transforms[' + lastGroupbyIndex + '].styles', 'target', 'value.visible');
carrs[fullInput.index] = kcont;
}

// If not specified, assume visible:
var curState = carr.get(fullTrace._group) || true;
var curState = kcont.get(fullTrace._group);

// If not specified, assume visible. This happens if there are other style
// properties set for a group but not the visibility. There are many similar
// ways to do this (e.g. why not just `curState = fullTrace.visible`??? The
// answer is: because it breaks other things like groupby trace names in
// subtle ways.)
if(curState === undefined) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment 📚

curState = true;
}

if(curState !== false) {
// true -> legendonly. All others toggle to true:
carr.set(fullTrace._group, visibility);
kcont.set(fullTrace._group, visibility);
}
carrIdx[fullInput.index] = insertUpdate(fullInput.index, 'visible', fullInput.visible === false ? false : true);
} else {
Expand Down Expand Up @@ -593,15 +601,14 @@ function handleClick(g, gd, numClicks) {
case false:
nextVisibility = false;
break;
default:
case 'legendonly':
nextVisibility = true;
break;
}

if(hasLegendgroup) {
for(i = 0; i < fullData.length; i++) {
if(fullData[i].visible && fullData[i].legendgroup === legendgroup) {
if(fullData[i].visible !== false && fullData[i].legendgroup === legendgroup) {
setVisibility(fullData[i], nextVisibility);
}
}
Expand Down Expand Up @@ -652,9 +659,9 @@ function handleClick(g, gd, numClicks) {
}

for(i = 0; i < carrs.length; i++) {
carr = carrs[i];
if(!carr) continue;
var update = carr.constructUpdate();
kcont = carrs[i];
if(!kcont) continue;
var update = kcont.constructUpdate();

var updateKeys = Object.keys(update);
for(j = 0; j < updateKeys.length; j++) {
Expand Down