Skip to content

Commit 9c35a14

Browse files
authored
Merge pull request #1855 from plotly/overlaying-axis-lines-bug
Fix overlaying axis layers
2 parents fbc0296 + 9c4157b commit 9c35a14

File tree

4 files changed

+75
-9
lines changed

4 files changed

+75
-9
lines changed

src/plots/cartesian/index.js

+30-8
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ function makeSubplotLayer(plotinfo) {
347347
plotinfo.zerolinelayer = joinLayer(mainplotinfo.overzero, 'g', id);
348348

349349
plotinfo.plot = joinLayer(mainplotinfo.overplot, 'g', id);
350-
plotinfo.xlines = joinLayer(mainplotinfo.overlines, 'path', id);
351-
plotinfo.ylines = joinLayer(mainplotinfo.overlines, 'path', id);
352-
plotinfo.xaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id);
353-
plotinfo.yaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id);
350+
plotinfo.xlines = joinLayer(mainplotinfo.overlines, 'path', id + '-x');
351+
plotinfo.ylines = joinLayer(mainplotinfo.overlines, 'path', id + '-y');
352+
plotinfo.xaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id + '-x');
353+
plotinfo.yaxislayer = joinLayer(mainplotinfo.overaxes, 'g', id + '-y');
354354
}
355355

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

371-
layers.each(function(subplot) {
372-
var plotgroup = d3.select(this),
373-
clipId = 'clip' + fullLayout._uid + subplot + 'plot';
371+
var overlayIdsToRemove = {};
372+
373+
layers.each(function(subplotId) {
374+
var plotgroup = d3.select(this);
375+
var clipId = 'clip' + fullLayout._uid + subplotId + 'plot';
374376

375377
plotgroup.remove();
376-
fullLayout._draggers.selectAll('g.' + subplot).remove();
378+
fullLayout._draggers.selectAll('g.' + subplotId).remove();
377379
fullLayout._defs.select('#' + clipId).remove();
378380

381+
overlayIdsToRemove[subplotId] = true;
382+
379383
// do not remove individual axis <clipPath>s here
380384
// as other subplots may need them
381385
});
386+
387+
// must remove overlaid subplot trace layers 'manually'
388+
389+
var subplots = fullLayout._plots;
390+
var subplotIds = Object.keys(subplots);
391+
392+
for(var i = 0; i < subplotIds.length; i++) {
393+
var subplotInfo = subplots[subplotIds[i]];
394+
var overlays = subplotInfo.overlays || [];
395+
396+
for(var j = 0; j < overlays.length; j++) {
397+
var overlayInfo = overlays[j];
398+
399+
if(overlayIdsToRemove[overlayInfo.id]) {
400+
overlayInfo.plot.selectAll('.trace').remove();
401+
}
402+
}
403+
}
382404
}
383405

384406
function joinLayer(parent, nodeType, className) {
30.9 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"data": [{
3+
"y": [1, 2, 3]
4+
}, {
5+
"x": [4, 5, 6],
6+
"y": [3, 1, 2],
7+
"xaxis": "x2",
8+
"yaxis": "y2"
9+
}],
10+
"layout": {
11+
"xaxis": {"showline": true},
12+
"yaxis": {"showline": true},
13+
"xaxis2": {"showline": true, "overlaying": "x", "side": "top"},
14+
"yaxis2": {"showline": true, "overlaying": "y", "side": "right"}
15+
}
16+
}

test/jasmine/tests/cartesian_test.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ var createGraphDiv = require('../assets/create_graph_div');
88
var destroyGraphDiv = require('../assets/destroy_graph_div');
99
var failTest = require('../assets/fail_test');
1010

11-
1211
describe('restyle', function() {
1312
describe('scatter traces', function() {
1413
var gd;
@@ -375,4 +374,33 @@ describe('subplot creation / deletion:', function() {
375374
.catch(failTest)
376375
.then(done);
377376
});
377+
378+
it('should clear overlaid subplot trace layers on restyle', function(done) {
379+
var fig = Lib.extendDeep({}, require('@mocks/overlaying-axis-lines.json'));
380+
381+
function _assert(xyCnt, x2y2Cnt) {
382+
expect(d3.select('.subplot.xy').select('.plot').selectAll('.trace').size())
383+
.toBe(xyCnt, 'has correct xy subplot trace count');
384+
expect(d3.select('.overplot').select('.x2y2').selectAll('.trace').size())
385+
.toBe(x2y2Cnt, 'has correct x2y2 oveylaid subplot trace count');
386+
}
387+
388+
Plotly.plot(gd, fig).then(function() {
389+
_assert(1, 1);
390+
return Plotly.restyle(gd, 'visible', false, [1]);
391+
})
392+
.then(function() {
393+
_assert(1, 0);
394+
return Plotly.restyle(gd, 'visible', true);
395+
})
396+
.then(function() {
397+
_assert(1, 1);
398+
return Plotly.restyle(gd, 'visible', false);
399+
})
400+
.then(function() {
401+
_assert(0, 0);
402+
})
403+
.catch(failTest)
404+
.then(done);
405+
});
378406
});

0 commit comments

Comments
 (0)