Skip to content

Commit cbaae9a

Browse files
authored
Merge pull request #3365 from plotly/fix3364-surface-check-xyz
Surface additional checks for handling empty z arrays and minimum number of rows and columns
2 parents fc0ba5d + b76710d commit cbaae9a

File tree

3 files changed

+124
-6
lines changed

3 files changed

+124
-6
lines changed

src/traces/surface/defaults.js

+7-4
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,18 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
2323
return Lib.coerce(traceIn, traceOut, attributes, attr, dflt);
2424
}
2525

26+
var x = coerce('x');
27+
var y = coerce('y');
28+
2629
var z = coerce('z');
27-
if(!z) {
30+
if(!z || !z.length ||
31+
(x ? (x.length < 1) : false) ||
32+
(y ? (y.length < 1) : false)
33+
) {
2834
traceOut.visible = false;
2935
return;
3036
}
3137

32-
var x = coerce('x');
33-
coerce('y');
34-
3538
traceOut._xlength = (Array.isArray(x) && Lib.isArrayOrTypedArray(x[0])) ? z.length : z[0].length;
3639
traceOut._ylength = z.length;
3740

test/jasmine/tests/gl3d_plot_interact_test.js

-2
Original file line numberDiff line numberDiff line change
@@ -670,13 +670,11 @@ describe('Test gl3d plots', function() {
670670
.then(done);
671671
});
672672

673-
674673
it('@gl should avoid passing blank texts to webgl', function(done) {
675674
function assertIsFilled(msg) {
676675
var fullLayout = gd._fullLayout;
677676
expect(fullLayout.scene._scene.glplot.objects[0].glyphBuffer.length).not.toBe(0, msg);
678677
}
679-
680678
Plotly.plot(gd, [{
681679
type: 'scatter3d',
682680
mode: 'text',

test/jasmine/tests/surface_test.js

+117
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
var Surface = require('@src/traces/surface');
2+
var Plotly = require('@lib/index');
3+
var failTest = require('../assets/fail_test');
4+
var createGraphDiv = require('../assets/create_graph_div');
5+
var destroyGraphDiv = require('../assets/destroy_graph_div');
26

37
var Lib = require('@src/lib');
48

@@ -178,5 +182,118 @@ describe('Test surface', function() {
178182
expect(traceOut.ycalendar).toBe('ethiopian');
179183
expect(traceOut.zcalendar).toBe('mayan');
180184
});
185+
186+
});
187+
188+
describe('Test dimension and expected visibility tests', function() {
189+
var gd;
190+
191+
beforeEach(function() {
192+
gd = createGraphDiv();
193+
});
194+
195+
afterEach(function() {
196+
Plotly.purge(gd);
197+
destroyGraphDiv();
198+
});
199+
200+
function assertVisibility(exp, msg) {
201+
expect(gd._fullData[0]).not.toBe(undefined, 'no visibility!');
202+
expect(gd._fullData[0].visible).toBe(exp, msg);
203+
}
204+
205+
it('@gl surface should be invisible when the z array is empty', function(done) {
206+
Plotly.plot(gd, [{
207+
'type': 'surface',
208+
'z': []
209+
}])
210+
.then(function() {
211+
assertVisibility(false, 'not to be visible');
212+
})
213+
.catch(failTest)
214+
.then(done);
215+
});
216+
217+
it('@gl surface should be invisible when the x array is defined but is empty', function(done) {
218+
Plotly.plot(gd, [{
219+
'type': 'surface',
220+
'x': [],
221+
'y': [0, 1],
222+
'z': []
223+
}])
224+
.then(function() {
225+
assertVisibility(false, 'not to be visible');
226+
})
227+
.catch(failTest)
228+
.then(done);
229+
});
230+
231+
it('@gl surface should be invisible when the y array is defined but is empty', function(done) {
232+
Plotly.plot(gd, [{
233+
'type': 'surface',
234+
'x': [0, 1],
235+
'y': [],
236+
'z': []
237+
}])
238+
.then(function() {
239+
assertVisibility(false, 'not to be visible');
240+
})
241+
.catch(failTest)
242+
.then(done);
243+
});
244+
245+
it('@gl surface should be invisible when the x array is defined and has at least one item', function(done) {
246+
Plotly.plot(gd, [{
247+
'type': 'surface',
248+
'x': [0],
249+
'y': [0, 1],
250+
'z': [[1], [2]]
251+
}])
252+
.then(function() {
253+
assertVisibility(true, 'to be visible');
254+
})
255+
.catch(failTest)
256+
.then(done);
257+
});
258+
259+
it('@gl surface should be invisible when the y array is defined and has at least one item', function(done) {
260+
Plotly.plot(gd, [{
261+
'type': 'surface',
262+
'x': [0, 1],
263+
'y': [0],
264+
'z': [[1, 2]]
265+
}])
266+
.then(function() {
267+
assertVisibility(true, 'to be visible');
268+
})
269+
.catch(failTest)
270+
.then(done);
271+
});
272+
273+
it('@gl surface should be visible when the x and y are not provided; but z array is provided', function(done) {
274+
Plotly.plot(gd, [{
275+
'type': 'surface',
276+
'z': [[1, 2], [3, 4]]
277+
}])
278+
.then(function() {
279+
assertVisibility(true, 'to be visible');
280+
})
281+
.catch(failTest)
282+
.then(done);
283+
});
284+
285+
it('@gl surface should be invisible when the x and y are provided; but z array is not provided', function(done) {
286+
Plotly.plot(gd, [{
287+
'type': 'surface',
288+
'x': [0, 1],
289+
'y': [0, 1]
290+
}])
291+
.then(function() {
292+
assertVisibility(false, 'to be invisible');
293+
})
294+
.catch(failTest)
295+
.then(done);
296+
});
297+
181298
});
182299
});

0 commit comments

Comments
 (0)