Skip to content

Commit d6151fa

Browse files
committed
Fix #2392 - clean up removed axes
1 parent 4bb3d40 commit d6151fa

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/plots/cartesian/index.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,17 @@ exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout)
306306
purgeSubplotLayers(oldFullLayout._cartesianlayer.selectAll('.subplot'), oldFullLayout);
307307
oldFullLayout._defs.selectAll('.axesclip').remove();
308308
}
309+
// otherwise look for subplots we need to remove
310+
else if(oldSubplotList.cartesian) {
311+
for(i = 0; i < oldSubplotList.cartesian.length; i++) {
312+
var oldSubplotId = oldSubplotList.cartesian[i];
313+
if(newSubplotList.cartesian.indexOf(oldSubplotId) === -1) {
314+
var selector = '.' + oldSubplotId + ',.' + oldSubplotId + '-x,.' + oldSubplotId + '-y';
315+
oldFullLayout._cartesianlayer.selectAll(selector).remove();
316+
removeSubplotExtras(oldSubplotId, oldFullLayout);
317+
}
318+
}
319+
}
309320
};
310321

311322
exports.drawFramework = function(gd) {
@@ -484,11 +495,9 @@ function purgeSubplotLayers(layers, fullLayout) {
484495

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

489499
plotgroup.remove();
490-
fullLayout._draggers.selectAll('g.' + subplotId).remove();
491-
fullLayout._defs.select('#' + clipId).remove();
500+
removeSubplotExtras(subplotId, fullLayout);
492501

493502
overlayIdsToRemove[subplotId] = true;
494503

@@ -515,6 +524,11 @@ function purgeSubplotLayers(layers, fullLayout) {
515524
}
516525
}
517526

527+
function removeSubplotExtras(subplotId, fullLayout) {
528+
fullLayout._draggers.selectAll('g.' + subplotId).remove();
529+
fullLayout._defs.select('#clip' + fullLayout._uid + subplotId + 'plot').remove();
530+
}
531+
518532
function joinLayer(parent, nodeType, className, dataVal) {
519533
var layer = parent.selectAll('.' + className)
520534
.data([dataVal || 0]);

test/jasmine/tests/cartesian_test.js

+23
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,29 @@ describe('subplot creation / deletion:', function() {
345345
.then(done);
346346
});
347347

348+
it('should remove unused axes when deleting traces', function(done) {
349+
Plotly.newPlot(gd,
350+
[{y: [1, 2, 3]}, {y: [10, 30, 20], yaxis: 'y2'}],
351+
{yaxis2: {side: 'right', overlaying: 'y', title: 'Hi!'}}
352+
)
353+
.then(function() {
354+
expect(gd.querySelectorAll('.xy2,.xy2-x,.xy2-y').length).not.toBe(0);
355+
expect(gd.querySelectorAll('.y2title').length).toBe(1);
356+
expect(gd._fullLayout._subplots.cartesian).toEqual(['xy', 'xy2']);
357+
expect(gd._fullLayout._subplots.yaxis).toEqual(['y', 'y2']);
358+
359+
return Plotly.deleteTraces(gd, [1]);
360+
})
361+
.then(function() {
362+
expect(gd.querySelectorAll('.xy2,.xy2-x,.xy2-y').length).toBe(0);
363+
expect(gd.querySelectorAll('.y2title').length).toBe(0);
364+
expect(gd._fullLayout._subplots.cartesian).toEqual(['xy']);
365+
expect(gd._fullLayout._subplots.yaxis).toEqual(['y']);
366+
})
367+
.catch(failTest)
368+
.then(done);
369+
});
370+
348371
it('puts plot backgrounds behind everything except if they overlap', function(done) {
349372
function checkBGLayers(behindCount, x2y2Count) {
350373
expect(gd.querySelectorAll('.bglayer rect.bg').length).toBe(behindCount);

0 commit comments

Comments
 (0)