-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathcartesian_test.js
579 lines (480 loc) · 21 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
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('axis line visibility', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('can show and hide axis lines', function(done) {
Plotly.newPlot(gd, [{y: [1, 2]}], {width: 400, height: 400})
.then(function() {
expect(gd.querySelector('.xlines-above').attributes.d.value).toBe('M0,0');
expect(gd.querySelector('.ylines-above').attributes.d.value).toBe('M0,0');
return Plotly.relayout(gd, {'xaxis.showline': true, 'yaxis.showline': true});
})
.then(function() {
expect(gd.querySelector('.xlines-above').attributes.d.value).not.toBe('M0,0');
expect(gd.querySelector('.ylines-above').attributes.d.value).not.toBe('M0,0');
return Plotly.relayout(gd, {'xaxis.showline': false, 'yaxis.showline': false});
})
.then(function() {
expect(gd.querySelector('.xlines-above').attributes.d.value).toBe('M0,0');
expect(gd.querySelector('.ylines-above').attributes.d.value).toBe('M0,0');
})
.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 assertOrphanSubplot(len) {
expect(d3.select('.subplot.xy').size()).toEqual(len);
expect(d3.select('.ytitle').size()).toEqual(len);
expect(d3.select('.ytitle').size()).toEqual(len);
// we only make one orphan subplot now
expect(d3.select('.subplot.x2y2').size()).toEqual(0);
expect(d3.select('.x2title').size()).toEqual(0);
expect(d3.select('.x2title').size()).toEqual(0);
}
Plotly.plot(gd, [], {
xaxis: { title: 'X' },
yaxis: { title: 'Y' },
xaxis2: { title: 'X2', anchor: 'y2' },
yaxis2: { title: 'Y2', anchor: 'x2' }
})
.then(function() {
assertOrphanSubplot(1);
return Plotly.addTraces(gd, [{
type: 'scattergeo',
lon: [10, 20, 30],
lat: [20, 30, 10]
}]);
})
.then(function() {
assertOrphanSubplot(0);
})
.catch(failTest)
.then(done);
});
it('should remove unused axes when deleting traces', function(done) {
Plotly.newPlot(gd,
[{y: [1, 2, 3]}, {y: [10, 30, 20], yaxis: 'y2'}],
{yaxis2: {side: 'right', overlaying: 'y', title: 'Hi!'}}
)
.then(function() {
expect(gd.querySelectorAll('.xy2,.xy2-x,.xy2-y').length).not.toBe(0);
expect(gd.querySelectorAll('.y2title').length).toBe(1);
expect(gd._fullLayout._subplots.cartesian).toEqual(['xy', 'xy2']);
expect(gd._fullLayout._subplots.yaxis).toEqual(['y', 'y2']);
return Plotly.deleteTraces(gd, [1]);
})
.then(function() {
expect(gd.querySelectorAll('.xy2,.xy2-x,.xy2-y').length).toBe(0);
expect(gd.querySelectorAll('.y2title').length).toBe(0);
expect(gd._fullLayout._subplots.cartesian).toEqual(['xy']);
expect(gd._fullLayout._subplots.yaxis).toEqual(['y']);
})
.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);
});
it('should clear obsolete content out of axis layers when relayout\'ing *layer*', function(done) {
var fig = Lib.extendDeep({}, require('@mocks/overlaying-axis-lines.json'));
function assertPathDatum(sel, expected, msg) {
expect(sel.attr('d') === null ? false : true).toBe(expected, msg);
}
function assertChildrenCnt(sel, expected, msg) {
expect(sel.selectAll('*').size()).toBe(expected, msg);
}
function _assert(xBelow, yBelow, xAbove, yAbove) {
var g = d3.select('.subplot.xy');
assertPathDatum(g.select('.xlines-below'), xBelow[0], 'xlines below');
assertChildrenCnt(g.select('.xaxislayer-below'), xBelow[1], 'xaxislayer below');
assertPathDatum(g.select('.ylines-below'), yBelow[0], 'ylines below');
assertChildrenCnt(g.select('.yaxislayer-below'), yBelow[1], 'yaxislayer below');
assertPathDatum(g.select('.xlines-above'), xAbove[0], 'xlines above');
assertChildrenCnt(g.select('.xaxislayer-above'), xAbove[1], 'xaxislayer above');
assertPathDatum(g.select('.ylines-above'), yAbove[0], 'ylines above');
assertChildrenCnt(g.select('.yaxislayer-above'), yAbove[1], 'yaxislayer above');
}
Plotly.plot(gd, fig).then(function() {
_assert(
[false, 0],
[false, 0],
[true, 10],
[true, 10]
);
return Plotly.relayout(gd, 'xaxis.layer', 'below traces');
})
.then(function() {
_assert(
[true, 10],
[false, 0],
[false, 0],
[true, 10]
);
return Plotly.relayout(gd, 'yaxis.layer', 'below traces');
})
.then(function() {
_assert(
[true, 10],
[true, 10],
[false, 0],
[false, 0]
);
return Plotly.relayout(gd, { 'xaxis.layer': null, 'yaxis.layer': null });
})
.then(function() {
_assert(
[false, 0],
[false, 0],
[true, 10],
[true, 10]
);
return Plotly.relayout(gd, { 'xaxis.layer': 'below traces', 'yaxis.layer': 'below traces' });
})
.then(function() {
_assert(
[true, 10],
[true, 10],
[false, 0],
[false, 0]
);
})
.catch(failTest)
.then(done);
});
it('clear axis ticks, labels and title when relayout an axis to `*visible:false*', function(done) {
function _assert(xaxis, yaxis) {
var g = d3.select('.subplot.xy');
var info = d3.select('.infolayer');
expect(g.selectAll('.xtick').size()).toBe(xaxis[0], 'x tick cnt');
expect(g.selectAll('.gridlayer .xgrid').size()).toBe(xaxis[1], 'x gridline cnt');
expect(info.selectAll('.g-xtitle').size()).toBe(xaxis[2], 'x title cnt');
expect(g.selectAll('.ytick').size()).toBe(yaxis[0], 'y tick cnt');
expect(g.selectAll('.gridlayer .ygrid').size()).toBe(yaxis[1], 'y gridline cnt');
expect(info.selectAll('.g-ytitle').size()).toBe(yaxis[2], 'y title cnt');
}
Plotly.plot(gd, [{
y: [1, 2, 1]
}], {
xaxis: {title: 'X'},
yaxis: {title: 'Y'}
})
.then(function() {
_assert([5, 4, 1], [6, 6, 1]);
return Plotly.relayout(gd, 'xaxis.visible', false);
})
.then(function() {
_assert([0, 0, 0], [6, 6, 1]);
return Plotly.relayout(gd, 'yaxis.visible', false);
})
.then(function() {
_assert([0, 0, 0], [0, 0, 0]);
return Plotly.relayout(gd, {
'xaxis.visible': true,
'yaxis.visible': true
});
})
.then(function() {
_assert([5, 4, 1], [6, 6, 1]);
})
.catch(failTest)
.then(done);
});
});