Skip to content

Fix overlaying axis layers #1855

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
Jul 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
38 changes: 30 additions & 8 deletions src/plots/cartesian/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,10 @@ function makeSubplotLayer(plotinfo) {
plotinfo.zerolinelayer = joinLayer(mainplotinfo.overzero, 'g', id);

plotinfo.plot = joinLayer(mainplotinfo.overplot, 'g', id);
plotinfo.xlines = joinLayer(mainplotinfo.overlines, 'path', id);
plotinfo.ylines = joinLayer(mainplotinfo.overlines, 'path', id);
plotinfo.xaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id);
plotinfo.yaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id);
plotinfo.xlines = joinLayer(mainplotinfo.overlines, 'path', id + '-x');
plotinfo.ylines = joinLayer(mainplotinfo.overlines, 'path', id + '-y');
plotinfo.xaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id + '-x');
plotinfo.yaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id + '-y');
}

// common attributes for all subplots, overlays or not
Expand All @@ -368,17 +368,39 @@ function makeSubplotLayer(plotinfo) {
function purgeSubplotLayers(layers, fullLayout) {
if(!layers) return;

layers.each(function(subplot) {
var plotgroup = d3.select(this),
clipId = 'clip' + fullLayout._uid + subplot + 'plot';
var overlayIdsToRemove = {};

layers.each(function(subplotId) {
var plotgroup = d3.select(this);
var clipId = 'clip' + fullLayout._uid + subplotId + 'plot';

plotgroup.remove();
fullLayout._draggers.selectAll('g.' + subplot).remove();
fullLayout._draggers.selectAll('g.' + subplotId).remove();
fullLayout._defs.select('#' + clipId).remove();

overlayIdsToRemove[subplotId] = true;

// do not remove individual axis <clipPath>s here
// as other subplots may need them
});

// must remove overlaid subplot trace layers 'manually'

var subplots = fullLayout._plots;
var subplotIds = Object.keys(subplots);

for(var i = 0; i < subplotIds.length; i++) {
var subplotInfo = subplots[subplotIds[i]];
var overlays = subplotInfo.overlays || [];

for(var j = 0; j < overlays.length; j++) {
var overlayInfo = overlays[j];

if(overlayIdsToRemove[overlayInfo.id]) {
overlayInfo.plot.selectAll('.trace').remove();
}
}
}
}

function joinLayer(parent, nodeType, className) {
Expand Down
Binary file added test/image/baselines/overlaying-axis-lines.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions test/image/mocks/overlaying-axis-lines.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"data": [{
"y": [1, 2, 3]
}, {
"x": [4, 5, 6],
"y": [3, 1, 2],
"xaxis": "x2",
"yaxis": "y2"
}],
"layout": {
"xaxis": {"showline": true},
"yaxis": {"showline": true},
"xaxis2": {"showline": true, "overlaying": "x", "side": "top"},
"yaxis2": {"showline": true, "overlaying": "y", "side": "right"}
}
}
30 changes: 29 additions & 1 deletion test/jasmine/tests/cartesian_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var failTest = require('../assets/fail_test');


describe('restyle', function() {
describe('scatter traces', function() {
var gd;
Expand Down Expand Up @@ -375,4 +374,33 @@ describe('subplot creation / deletion:', function() {
.catch(failTest)
.then(done);
});

it('should clear overlaid subplot trace layers on restyle', function(done) {
var fig = Lib.extendDeep({}, require('@mocks/overlaying-axis-lines.json'));

function _assert(xyCnt, x2y2Cnt) {
expect(d3.select('.subplot.xy').select('.plot').selectAll('.trace').size())
.toBe(xyCnt, 'has correct xy subplot trace count');
expect(d3.select('.overplot').select('.x2y2').selectAll('.trace').size())
.toBe(x2y2Cnt, 'has correct x2y2 oveylaid subplot trace count');
}

Plotly.plot(gd, fig).then(function() {
_assert(1, 1);
return Plotly.restyle(gd, 'visible', false, [1]);
})
.then(function() {
_assert(1, 0);
return Plotly.restyle(gd, 'visible', true);
})
.then(function() {
_assert(1, 1);
return Plotly.restyle(gd, 'visible', false);
})
.then(function() {
_assert(0, 0);
})
.catch(failTest)
.then(done);
});
});