Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3e826eb

Browse files
committed
Make scattergl plot disposal
1 parent bedcdfa commit 3e826eb

File tree

4 files changed

+49
-33
lines changed

4 files changed

+49
-33
lines changed

src/plots/cartesian/dragbox.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ module.exports = function dragBox(gd, plotinfo, x, y, w, h, ns, ew) {
733733

734734
// clear gl frame, if any, since we preserve drawing buffer
735735
// FIXME: code duplication with cartesian.plot
736-
if(fullLayout._glcanvas.size()) {
736+
if(fullLayout._glcanvas && fullLayout._glcanvas.size()) {
737737
fullLayout._glcanvas.each(function(d) {
738738
d.regl.clear({
739739
color: true

src/plots/cartesian/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,26 @@ exports.clean = function(newFullData, newFullLayout, oldFullData, oldFullLayout)
145145

146146
var hadScatter, hasScatter, i;
147147

148+
// destruct scattergl
149+
var oldSceneKeys = Plots.getSubplotIds(oldFullLayout, 'cartesian');
150+
151+
for(i = 0; i < oldSceneKeys.length; i++) {
152+
var id = oldSceneKeys[i],
153+
oldSubplot = oldFullLayout._plots[id];
154+
155+
// old subplot wasn't gl2d; nothing to do
156+
if(!oldSubplot._scene) continue;
157+
158+
// if no traces are present, delete gl2d subplot
159+
var subplotData = Plots.getSubplotData(newFullData, 'gl', id);
160+
161+
if(subplotData.length === 0) {
162+
oldSubplot._scene.destroy();
163+
delete oldFullLayout._plots[id];
164+
}
165+
}
166+
167+
148168
for(i = 0; i < oldModules.length; i++) {
149169
if(oldModules[i].name === 'scatter') {
150170
hadScatter = true;

src/traces/scattergl/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,21 @@ ScatterRegl.calc = function calc(container, trace) {
500500
scene.dirty = false;
501501
};
502502

503+
// remove scene resources
504+
scene.destroy = function destroy() {
505+
if(scene.fill2d) scene.fill2d.destroy();
506+
if(scene.scatter2d) scene.scatter2d.destroy();
507+
if(scene.error2d) scene.error2d.destroy();
508+
if(scene.line2d) scene.line2d.destroy();
509+
if(scene.select2d) scene.select2d.destroy();
510+
511+
scene.lineOptions = null;
512+
scene.fillOptions = null;
513+
scene.markerOptions = null;
514+
scene.errorXOptions = null;
515+
scene.errorYOptions = null;
516+
};
517+
503518
// highlight selected points
504519
scene.select = function select(selection) {
505520
if(!scene.select2d) return;

test/jasmine/tests/gl_plot_interact_test.js

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -857,14 +857,13 @@ describe('Test gl2d plots', function() {
857857

858858
it('should respond to drag interactions', function(done) {
859859
var _mock = Lib.extendDeep({}, mock);
860-
_mock.data[0].type = 'scatter';
861860

862861
var relayoutCallback = jasmine.createSpy('relayoutCallback');
863862

864-
var originalX = [-0.3169014084507042, 5.316901408450704];
865-
var originalY = [-0.5806379476536665, 6.218528262566369];
866-
var newX = [-0.5516431924882629, 5.082159624413145];
867-
var newY = [-1.7947747709072441, 5.004391439312791];
863+
var originalX = [-0.3037383177570093, 5.303738317757009];
864+
var originalY = [-0.5532219548705213, 6.191112269783224];
865+
var newX = [-0.5373831775700935, 5.070093457943925];
866+
var newY = [-1.7575673521301185, 4.986766872523626];
868867
var precision = 5;
869868

870869
Plotly.plot(gd, _mock)
@@ -973,25 +972,6 @@ describe('Test gl2d plots', function() {
973972
.then(done);
974973
});
975974

976-
it('should clear orphan cartesian subplots on addTraces', function(done) {
977-
Plotly.newPlot(gd, [], {
978-
xaxis: { title: 'X' },
979-
yaxis: { title: 'Y' }
980-
})
981-
.then(function() {
982-
return Plotly.addTraces(gd, [{
983-
type: 'scattergl',
984-
x: [1, 2, 3, 4, 5, 6, 7],
985-
y: [0, 5, 8, 9, 8, 5, 0]
986-
}]);
987-
})
988-
.then(function() {
989-
expect(d3.select('.xtitle').size()).toEqual(0);
990-
expect(d3.select('.ytitle').size()).toEqual(0);
991-
})
992-
.then(done);
993-
});
994-
995975
it('supports 1D and 2D Zoom', function(done) {
996976
var centerX, centerY;
997977
Plotly.newPlot(gd,
@@ -1026,8 +1006,8 @@ describe('Test gl2d plots', function() {
10261006

10271007
// no change - too small
10281008
mouseTo([centerX, centerY], [centerX - 5, centerY + 5]);
1029-
expect(gd.layout.xaxis.range).toBeCloseToArray([6, 8], 3);
1030-
expect(gd.layout.yaxis.range).toBeCloseToArray([5, 7], 3);
1009+
expect(gd.layout.xaxis.range).toBeCloseToArray([0, 16], 3);
1010+
expect(gd.layout.yaxis.range).toBeCloseToArray([0, 16], 3);
10311011
})
10321012
.catch(fail)
10331013
.then(done);
@@ -1073,17 +1053,17 @@ describe('Test gl2d plots', function() {
10731053

10741054
// no change - too small
10751055
mouseTo([centerX, centerY], [centerX - 5, centerY + 5]);
1076-
expect(gd.layout.xaxis.range).toBeCloseToArray([4, 6], 3);
1077-
expect(gd.layout.yaxis.range).toBeCloseToArray([9, 10], 3);
1056+
expect(gd.layout.xaxis.range).toBeCloseToArray([-8, 24], 3);
1057+
expect(gd.layout.yaxis.range).toBeCloseToArray([0, 16], 3);
10781058

10791059
return Plotly.relayout(gd, {
10801060
'xaxis.autorange': true,
10811061
'yaxis.autorange': true
10821062
});
10831063
})
10841064
.then(function() {
1085-
expect(gd.layout.xaxis.range).toBeCloseToArray([-8.09195, 24.09195], 3);
1086-
expect(gd.layout.yaxis.range).toBeCloseToArray([-0.04598, 16.04598], 3);
1065+
expect(gd.layout.xaxis.range).toBeCloseToArray([-8.091954022988505, 24.091954022988503], 3);
1066+
expect(gd.layout.yaxis.range).toBeCloseToArray([-0.04597701149425282, 16.04597701149425], 3);
10871067
})
10881068
.catch(fail)
10891069
.then(done);
@@ -1136,9 +1116,10 @@ describe('Test removal of gl contexts', function() {
11361116
y: [2, 1, 3]
11371117
}])
11381118
.then(function() {
1139-
expect(gd._fullLayout._plots.xy._scene2d.glplot).toBeDefined();
1119+
expect(gd._fullLayout._plots.xy._scene).toBeDefined();
11401120

11411121
Plots.cleanPlot([], {}, gd._fullData, gd._fullLayout);
1122+
11421123
expect(gd._fullLayout._plots).toEqual({});
11431124
})
11441125
.then(done);
@@ -1188,7 +1169,7 @@ describe('Test removal of gl contexts', function() {
11881169
.then(done);
11891170
});
11901171

1191-
it('Plotly.newPlot should remove gl context from the graph div of a gl2d plot', function(done) {
1172+
fit('Plotly.newPlot should remove gl context from the graph div of a gl2d plot', function(done) {
11921173
var firstGlplotObject, firstGlContext, firstCanvas;
11931174

11941175
Plotly.plot(gd, [{

0 commit comments

Comments
 (0)