-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcartesian_test.js
406 lines (328 loc) · 14.6 KB
/
cartesian_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
var d3 = require('d3');
var Plotly = require('@lib/index');
var Lib = require('@src/lib');
var Drawing = require('@src/components/drawing');
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;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('reuses SVG fills', function(done) {
var fills, firstToZero, secondToZero, firstToNext, secondToNext;
var mock = Lib.extendDeep({}, require('@mocks/basic_area.json'));
Plotly.plot(gd, mock.data, mock.layout).then(function() {
// Assert there are two fills:
fills = d3.selectAll('g.trace.scatter .js-fill')[0];
// First is tozero, second is tonext:
expect(d3.selectAll('g.trace.scatter .js-fill').size()).toEqual(2);
expect(fills[0].classList.contains('js-tozero')).toBe(true);
expect(fills[0].classList.contains('js-tonext')).toBe(false);
expect(fills[1].classList.contains('js-tozero')).toBe(false);
expect(fills[1].classList.contains('js-tonext')).toBe(true);
firstToZero = fills[0];
firstToNext = fills[1];
}).then(function() {
return Plotly.restyle(gd, {visible: [false]}, [1]);
}).then(function() {
// Trace 1 hidden leaves only trace zero's tozero fill:
expect(d3.selectAll('g.trace.scatter .js-fill').size()).toEqual(1);
expect(fills[0].classList.contains('js-tozero')).toBe(true);
expect(fills[0].classList.contains('js-tonext')).toBe(false);
}).then(function() {
return Plotly.restyle(gd, {visible: [true]}, [1]);
}).then(function() {
// Reshow means two fills again AND order is preserved:
fills = d3.selectAll('g.trace.scatter .js-fill')[0];
// First is tozero, second is tonext:
expect(d3.selectAll('g.trace.scatter .js-fill').size()).toEqual(2);
expect(fills[0].classList.contains('js-tozero')).toBe(true);
expect(fills[0].classList.contains('js-tonext')).toBe(false);
expect(fills[1].classList.contains('js-tozero')).toBe(false);
expect(fills[1].classList.contains('js-tonext')).toBe(true);
secondToZero = fills[0];
secondToNext = fills[1];
// The identity of the first is retained:
expect(firstToZero).toBe(secondToZero);
// The second has been recreated so is different:
expect(firstToNext).not.toBe(secondToNext);
return Plotly.restyle(gd, 'visible', false);
}).then(function() {
expect(d3.selectAll('g.trace.scatter').size()).toEqual(0);
})
.catch(failTest)
.then(done);
});
it('reuses SVG lines', function(done) {
var lines, firstLine1, secondLine1, firstLine2, secondLine2;
var mock = Lib.extendDeep({}, require('@mocks/basic_line.json'));
Plotly.plot(gd, mock.data, mock.layout).then(function() {
lines = d3.selectAll('g.scatter.trace .js-line');
firstLine1 = lines[0][0];
firstLine2 = lines[0][1];
// One line for each trace:
expect(lines.size()).toEqual(2);
}).then(function() {
return Plotly.restyle(gd, {visible: [false]}, [0]);
}).then(function() {
lines = d3.selectAll('g.scatter.trace .js-line');
// Only one line now and it's equal to the second trace's line from above:
expect(lines.size()).toEqual(1);
expect(lines[0][0]).toBe(firstLine2);
}).then(function() {
return Plotly.restyle(gd, {visible: [true]}, [0]);
}).then(function() {
lines = d3.selectAll('g.scatter.trace .js-line');
secondLine1 = lines[0][0];
secondLine2 = lines[0][1];
// Two lines once again:
expect(lines.size()).toEqual(2);
// First line has been removed and recreated:
expect(firstLine1).not.toBe(secondLine1);
// Second line was persisted:
expect(firstLine2).toBe(secondLine2);
})
.catch(failTest)
.then(done);
});
it('can change scatter mode', function(done) {
var mock = Lib.extendDeep({}, require('@mocks/text_chart_basic.json'));
function assertScatterModeSizes(lineSize, pointSize, textSize) {
var gd3 = d3.select(gd),
lines = gd3.selectAll('g.scatter.trace .js-line'),
points = gd3.selectAll('g.scatter.trace path.point'),
texts = gd3.selectAll('g.scatter.trace text');
expect(lines.size()).toEqual(lineSize);
expect(points.size()).toEqual(pointSize);
expect(texts.size()).toEqual(textSize);
}
Plotly.plot(gd, mock.data, mock.layout).then(function() {
assertScatterModeSizes(2, 6, 9);
return Plotly.restyle(gd, 'mode', 'lines');
})
.then(function() {
assertScatterModeSizes(3, 0, 0);
return Plotly.restyle(gd, 'mode', 'markers');
})
.then(function() {
assertScatterModeSizes(0, 9, 0);
return Plotly.restyle(gd, 'mode', 'markers+text');
})
.then(function() {
assertScatterModeSizes(0, 9, 9);
return Plotly.restyle(gd, 'mode', 'text');
})
.then(function() {
assertScatterModeSizes(0, 0, 9);
return Plotly.restyle(gd, 'mode', 'markers+text+lines');
})
.then(function() {
assertScatterModeSizes(3, 9, 9);
})
.catch(failTest)
.then(done);
});
});
});
describe('relayout', function() {
describe('axis category attributes', function() {
var mock = require('@mocks/basic_bar.json');
var gd, mockCopy;
beforeEach(function() {
mockCopy = Lib.extendDeep({}, mock);
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should response to \'categoryarray\' and \'categoryorder\' updates', function(done) {
function assertCategories(list) {
d3.selectAll('g.xtick').each(function(_, i) {
var tick = d3.select(this).select('text');
expect(tick.html()).toEqual(list[i]);
});
}
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(function() {
assertCategories(['giraffes', 'orangutans', 'monkeys']);
return Plotly.relayout(gd, 'xaxis.categoryorder', 'category descending');
}).then(function() {
var list = ['orangutans', 'monkeys', 'giraffes'];
expect(gd._fullLayout.xaxis._initialCategories).toEqual(list);
assertCategories(list);
return Plotly.relayout(gd, 'xaxis.categoryorder', null);
}).then(function() {
assertCategories(['giraffes', 'orangutans', 'monkeys']);
return Plotly.relayout(gd, {
'xaxis.categoryarray': ['monkeys', 'giraffes', 'orangutans']
});
}).then(function() {
var list = ['monkeys', 'giraffes', 'orangutans'];
expect(gd.layout.xaxis.categoryarray).toEqual(list);
expect(gd._fullLayout.xaxis.categoryarray).toEqual(list);
expect(gd._fullLayout.xaxis._initialCategories).toEqual(list);
assertCategories(list);
})
.catch(failTest)
.then(done);
});
});
describe('axis ranges', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should translate points and text element', function(done) {
var mockData = [{
x: [1],
y: [1],
text: ['A'],
mode: 'markers+text'
}];
function assertPointTranslate(pointT, textT) {
var TOLERANCE = 10;
var gd3 = d3.select(gd),
points = gd3.selectAll('g.scatter.trace path.point'),
texts = gd3.selectAll('g.scatter.trace text');
expect(points.size()).toEqual(1);
expect(texts.size()).toEqual(1);
expect(points.attr('x')).toBe(null);
expect(points.attr('y')).toBe(null);
expect(texts.attr('transform')).toBe(null);
var translate = Drawing.getTranslate(points);
expect(Math.abs(translate.x - pointT[0])).toBeLessThan(TOLERANCE);
expect(Math.abs(translate.y - pointT[1])).toBeLessThan(TOLERANCE);
expect(Math.abs(texts.attr('x') - textT[0])).toBeLessThan(TOLERANCE);
expect(Math.abs(texts.attr('y') - textT[1])).toBeLessThan(TOLERANCE);
}
Plotly.plot(gd, mockData).then(function() {
assertPointTranslate([270, 135], [270, 135]);
return Plotly.relayout(gd, 'xaxis.range', [2, 3]);
})
.then(function() {
assertPointTranslate([-540, 135], [-540, 135]);
})
.catch(failTest)
.then(done);
});
});
});
describe('subplot creation / deletion:', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should clear orphan subplot when adding traces to blank graph', function(done) {
function assertCartesianSubplot(len) {
expect(d3.select('.subplot.xy').size()).toEqual(len);
expect(d3.select('.subplot.x2y2').size()).toEqual(len);
expect(d3.select('.x2title').size()).toEqual(len);
expect(d3.select('.x2title').size()).toEqual(len);
expect(d3.select('.ytitle').size()).toEqual(len);
expect(d3.select('.ytitle').size()).toEqual(len);
}
Plotly.plot(gd, [], {
xaxis: { title: 'X' },
yaxis: { title: 'Y' },
xaxis2: { title: 'X2', anchor: 'y2' },
yaxis2: { title: 'Y2', anchor: 'x2' }
})
.then(function() {
assertCartesianSubplot(1);
return Plotly.addTraces(gd, [{
type: 'scattergeo',
lon: [10, 20, 30],
lat: [20, 30, 10]
}]);
})
.then(function() {
assertCartesianSubplot(0);
})
.catch(failTest)
.then(done);
});
it('puts plot backgrounds behind everything except if they overlap', function(done) {
function checkBGLayers(behindCount, x2y2Count) {
expect(gd.querySelectorAll('.bglayer rect.bg').length).toBe(behindCount);
expect(gd.querySelectorAll('.subplot.x2y2 rect.bg').length).toBe(x2y2Count);
// xy is the first subplot, so it never gets put in front of others
expect(gd.querySelectorAll('.subplot.xy rect.bg').length).toBe(0);
// xy3 is an overlay, so never gets its own bg
expect(gd.querySelectorAll('.subplot.xy3 rect.bg').length).toBe(0);
// verify that these are *all* the subplots and backgrounds we have
expect(gd.querySelectorAll('.subplot').length).toBe(3);
['xy', 'x2y2', 'xy3'].forEach(function(subplot) {
expect(gd.querySelectorAll('.subplot.' + subplot).length).toBe(1);
});
expect(gd.querySelectorAll('.bg').length).toBe(behindCount + x2y2Count);
}
Plotly.plot(gd, [
{y: [1, 2, 3]},
{y: [2, 3, 1], xaxis: 'x2', yaxis: 'y2'},
{y: [3, 1, 2], yaxis: 'y3'}
], {
xaxis: {domain: [0, 0.5]},
xaxis2: {domain: [0.5, 1], anchor: 'y2'},
yaxis: {domain: [0, 1]},
yaxis2: {domain: [0.5, 1], anchor: 'x2'},
yaxis3: {overlaying: 'y'},
// legend makes its own .bg rect - delete so we can ignore that here
showlegend: false
})
.then(function() {
// touching but not overlapping: all backgrounds are in back
checkBGLayers(2, 0);
// now add a slight overlap: that's enough to put x2y2 in front
return Plotly.relayout(gd, {'xaxis2.domain': [0.49, 1]});
})
.then(function() {
checkBGLayers(1, 1);
// x ranges overlap, but now y ranges are disjoint
return Plotly.relayout(gd, {'xaxis2.domain': [0, 1], 'yaxis.domain': [0, 0.5]});
})
.then(function() {
checkBGLayers(2, 0);
// regular inset
return Plotly.relayout(gd, {
'xaxis.domain': [0, 1],
'yaxis.domain': [0, 1],
'xaxis2.domain': [0.6, 0.9],
'yaxis2.domain': [0.6, 0.9]
});
})
.then(function() {
checkBGLayers(1, 1);
})
.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);
});
});