Skip to content

fix #2452 - removing and adding scatter(gl) as not the first module #2455

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 2 commits into from
Mar 8, 2018
Merged
Show file tree
Hide file tree
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
30 changes: 7 additions & 23 deletions src/plots/cartesian/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,35 +216,19 @@ exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout)
var oldModules = oldFullLayout._modules || [],
newModules = newFullLayout._modules || [];

var hadScatter, hasScatter, hadGl, hasGl, i, oldPlots, ids, subplotInfo;
var hadScatter, hasScatter, hadGl, hasGl, i, oldPlots, ids, subplotInfo, moduleName;


for(i = 0; i < oldModules.length; i++) {
if(oldModules[i].name === 'scatter') {
hadScatter = true;
}
break;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The main problem was this break being outside the if. But then since we had the same loop twice and it's unlikely in general for both scatter and scattergl to be present, I combined them into one and dropped the break altogether.

}

for(i = 0; i < newModules.length; i++) {
if(newModules[i].name === 'scatter') {
hasScatter = true;
break;
}
}

for(i = 0; i < oldModules.length; i++) {
if(oldModules[i].name === 'scattergl') {
hadGl = true;
}
break;
moduleName = oldModules[i].name;
if(moduleName === 'scatter') hadScatter = true;
else if(moduleName === 'scattergl') hadGl = true;
}

for(i = 0; i < newModules.length; i++) {
if(newModules[i].name === 'scattergl') {
hasGl = true;
break;
}
moduleName = newModules[i].name;
if(moduleName === 'scatter') hasScatter = true;
else if(moduleName === 'scattergl') hasGl = true;
Copy link
Contributor

Choose a reason for hiding this comment

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

Great. We should starting thinking about generalizing this pattern.

In short, this loop and its has?? flags exist because scatter and scattergl do not clear their content on redraws unlike other trace types. A goal of ours should be to update all cartesian trace module plot steps to something more d3-iomatic for (1) work with animations (2) what could be nice performance gain on redraws.

}

if(hadScatter && !hasScatter) {
Expand Down
4 changes: 3 additions & 1 deletion src/traces/scattergl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,9 @@ function sceneUpdate(gd, subplot) {
scene.selectBatch = null;
scene.unselectBatch = null;

delete subplot._scene;
// we can't just delete _scene, because `destroy` is called in the
// middle of supplyDefaults, before relinkPrivateKeys which will put it back.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

⚠️ ⚠️ This could apply to anything during cleanPlot ⚠️ ⚠️

If you ever run into a situation like this "but I deleted that, how come it's still here???" try setting it to null instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So after fixing the break issue so traces could get removed, scattergl had a new bug that it wouldn't come back after being deleted - because the scene had been destroyed but we were trying to draw traces into it again. Hence my tests below that remove and then re-add the traces. There were related issues, like if you hid the last scattergl trace and then tried to add a totally new one it wouldn't show up either, but it's all the same issue so I just tested original case.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice. I vaguely remember reading that delete was slow. Perhaps setting subplot._scene = null will be a perf boost too 🐎

subplot._scene = null;
};
}

Expand Down
52 changes: 52 additions & 0 deletions test/jasmine/tests/cartesian_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,58 @@ describe('restyle', function() {
.then(done);

});

it('can legend-hide the second and only scatter trace', function(done) {
Plotly.plot(gd, [
{y: [1, 2, 3], type: 'bar'},
{y: [1, 2, 3], xaxis: 'x2', yaxis: 'y2', type: 'scatter'}
], {
xaxis: {domain: [0, 0.4]},
xaxis2: {domain: [0.6, 1]},
yaxis2: {anchor: 'x2'},
width: 600,
height: 400
})
.then(function() {
expect(d3.select('.scatter').size()).toBe(1);
return Plotly.restyle(gd, {visible: 'legendonly'}, 1);
})
.then(function() {
expect(d3.select('.scatter').size()).toBe(0);
return Plotly.restyle(gd, {visible: true}, 1);
})
.then(function() {
expect(d3.select('.scatter').size()).toBe(1);
})
.catch(failTest)
.then(done);
});

it('can legend-hide the second and only scattergl trace', function(done) {
Plotly.plot(gd, [
{y: [1, 2, 3], type: 'bar'},
{y: [1, 2, 3], xaxis: 'x2', yaxis: 'y2', type: 'scattergl'}
], {
xaxis: {domain: [0, 0.4]},
xaxis2: {domain: [0.6, 1]},
yaxis2: {anchor: 'x2'},
width: 600,
height: 400
})
.then(function() {
expect(!!gd._fullLayout._plots.x2y2._scene).toBe(true);
return Plotly.restyle(gd, {visible: 'legendonly'}, 1);
})
.then(function() {
expect(!!gd._fullLayout._plots.x2y2._scene).toBe(false);
return Plotly.restyle(gd, {visible: true}, 1);
})
.then(function() {
expect(!!gd._fullLayout._plots.x2y2._scene).toBe(true);
})
.catch(failTest)
.then(done);
});
});
});

Expand Down