-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathgl2d_click_test.js
539 lines (469 loc) · 16.2 KB
/
gl2d_click_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
var Plotly = require('../../../lib/index');
var Lib = require('../../../src/lib');
var d3Select = require('../../strict-d3').select;
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var customAssertions = require('../assets/custom_assertions');
var assertHoverLabelStyle = customAssertions.assertHoverLabelStyle;
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
// cartesian click events events use the hover data
// from the mousemove events and then simulate
// a click event on mouseup
var click = require('../assets/timed_click');
var hover = require('../assets/hover');
var delay = require('../assets/delay');
var mouseEvent = require('../assets/mouse_event');
var mock0 = require('../../image/mocks/gl2d_scatter-continuous-clustering.json');
var mock1 = require('../../image/mocks/gl2d_14.json');
var mock2 = {
data: [{
x: [1, 2, 3, 4],
y: [12, 3, 14, 4],
type: 'scattergl',
mode: 'markers'
}, {
x: [4, 5, 6, 7],
y: [1, 31, 24, 14],
type: 'scattergl',
mode: 'markers'
}, {
x: [8, 9, 10, 11],
y: [18, 13, 10, 3],
type: 'scattergl',
mode: 'markers'
}],
layout: {}
};
describe('Test hover and click interactions', function() {
var gd;
function makeHoverFn(gd, x, y) {
return function() {
return new Promise(function(resolve) {
gd.on('plotly_hover', resolve);
hover(x, y);
});
};
}
function makeClickFn(gd, x, y) {
return function() {
return new Promise(function(resolve) {
gd.on('plotly_click', resolve);
click(x, y);
});
};
}
function makeUnhoverFn(gd, x0, y0) {
return function() {
return new Promise(function(resolve) {
var initialElement = document.elementFromPoint(x0, y0);
// fairly realistic simulation of moving with the cursor
var canceler = setInterval(function() {
x0 -= 2;
y0 -= 2;
hover(x0, y0);
var nowElement = document.elementFromPoint(x0, y0);
if(nowElement !== initialElement) {
mouseEvent('mouseout', x0, y0, {element: initialElement});
}
}, 10);
gd.on('plotly_unhover', function() {
clearInterval(canceler);
resolve('emitted plotly_unhover');
});
setTimeout(function() {
clearInterval(canceler);
resolve(null);
}, 350);
});
};
}
function assertEventData(actual, expected, msg) {
expect(actual.points.length).toEqual(1, 'points length');
var pt = actual.points[0];
expect(Object.keys(pt)).toEqual(jasmine.arrayContaining([
'x', 'y', 'curveNumber', 'pointNumber',
'data', 'fullData', 'xaxis', 'yaxis'
]), 'event data keys');
expect(pt.xaxis.domain.length).toBe(2, msg + ' - xaxis');
expect(pt.yaxis.domain.length).toBe(2, msg + ' - yaxis');
expect(pt.x).toBe(expected.x, msg + ' - x');
expect(pt.y).toBe(expected.y, msg + ' - y');
expect(pt.curveNumber).toBe(expected.curveNumber, msg + ' - curve number');
expect(String(pt.pointNumber)).toBe(String(expected.pointNumber), msg + ' - point number');
}
// returns basic hover/click/unhover runner for one xy position
function makeRunner(pos, expected, opts) {
opts = opts || {};
var _hover = makeHoverFn(gd, pos[0], pos[1]);
var _click = makeClickFn(gd, pos[0], pos[1]);
var _unhover = opts.noUnHover ?
function() { return 'emitted plotly_unhover'; } :
makeUnhoverFn(gd, pos[0], pos[1]);
return function() {
return delay(100)()
.then(_hover)
.then(function(eventData) {
assertEventData(eventData, expected, opts.msg);
var g = d3Select('g.hovertext');
if(g.node() === null) {
expect(expected.noHoverLabel).toBe(true);
} else {
assertHoverLabelStyle(g, expected, opts.msg);
}
if(expected.label) {
assertHoverLabelContent({
nums: expected.label[0],
name: expected.label[1]
});
}
})
.then(_click)
.then(function(eventData) {
assertEventData(eventData, expected, opts.msg);
})
.then(_unhover)
.then(function(eventData) {
expect(eventData).toBe('emitted plotly_unhover', opts.msg);
});
};
}
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(function(done) {
Plotly.purge(gd);
destroyGraphDiv();
setTimeout(done, 1500);
});
it('@gl should output correct event data for scattergl', function(done) {
var _mock = Lib.extendDeep({}, mock1);
_mock.layout.hoverlabel = {
font: {
size: 20,
color: 'yellow'
}
};
_mock.data[0].hoverinfo = _mock.data[0].x.map(function(_, i) { return i % 2 ? 'y' : 'x'; });
_mock.data[0].hoverlabel = {
bgcolor: 'blue',
bordercolor: _mock.data[0].x.map(function(_, i) { return i % 2 ? 'red' : 'green'; })
};
var run = makeRunner([634, 321], {
x: 15.772,
y: 0.387,
label: ['0.387', null],
curveNumber: 0,
pointNumber: 33,
bgcolor: 'rgb(0, 0, 255)',
bordercolor: 'rgb(255, 0, 0)',
fontSize: 20,
fontFamily: 'Arial',
fontColor: 'rgb(255, 255, 0)'
}, {
msg: 'scattergl'
});
Plotly.newPlot(gd, _mock)
.then(run)
.then(done, done.fail);
});
it('@gl should output correct event data for scattergl in *select* dragmode', function(done) {
var _mock = Lib.extendDeep({}, mock1);
_mock.layout.dragmode = 'select';
_mock.layout.hoverlabel = {
font: {
size: 20,
color: 'yellow'
}
};
_mock.data[0].hoverinfo = _mock.data[0].x.map(function(_, i) { return i % 2 ? 'y' : 'x'; });
_mock.data[0].hoverlabel = {
bgcolor: 'blue',
bordercolor: _mock.data[0].x.map(function(_, i) { return i % 2 ? 'red' : 'green'; })
};
var run = makeRunner([634, 321], {
x: 15.772,
y: 0.387,
label: ['0.387', null],
curveNumber: 0,
pointNumber: 33,
bgcolor: 'rgb(0, 0, 255)',
bordercolor: 'rgb(255, 0, 0)',
fontSize: 20,
fontFamily: 'Arial',
fontColor: 'rgb(255, 255, 0)'
}, {
msg: 'scattergl'
});
Plotly.newPlot(gd, _mock)
.then(run)
.then(done, done.fail);
});
it('@gl should output correct event data for scattergl in *lasso* dragmode', function(done) {
var _mock = Lib.extendDeep({}, mock1);
_mock.layout.dragmode = 'lasso';
_mock.layout.hoverlabel = {
font: {
size: 20,
color: 'yellow'
}
};
_mock.data[0].hoverinfo = _mock.data[0].x.map(function(_, i) { return i % 2 ? 'y' : 'x'; });
_mock.data[0].hoverlabel = {
bgcolor: 'blue',
bordercolor: _mock.data[0].x.map(function(_, i) { return i % 2 ? 'red' : 'green'; })
};
var run = makeRunner([634, 321], {
x: 15.772,
y: 0.387,
label: ['0.387', null],
curveNumber: 0,
pointNumber: 33,
bgcolor: 'rgb(0, 0, 255)',
bordercolor: 'rgb(255, 0, 0)',
fontSize: 20,
fontFamily: 'Arial',
fontColor: 'rgb(255, 255, 0)'
}, {
msg: 'scattergl'
});
Plotly.newPlot(gd, _mock)
.then(run)
.then(done, done.fail);
});
it('@gl should output correct event data for scattergl with hoverinfo: \'none\'', function(done) {
var _mock = Lib.extendDeep({}, mock1);
_mock.data[0].hoverinfo = 'none';
var run = makeRunner([634, 321], {
x: 15.772,
y: 0.387,
curveNumber: 0,
pointNumber: 33,
noHoverLabel: true
}, {
msg: 'scattergl with hoverinfo'
});
Plotly.newPlot(gd, _mock)
.then(run)
.then(done, done.fail);
});
it('@gl should show correct label for scattergl when hovertext is set', function(done) {
var _mock = Lib.extendDeep({}, mock1);
_mock.data[0].hovertext = 'text';
_mock.data[0].hovertext = 'HoVeRtExT';
_mock.layout.hovermode = 'closest';
var run = makeRunner([634, 321], {
x: 15.772,
y: 0.387,
label: ['(15.772, 0.387)\nHoVeRtExT', null],
curveNumber: 0,
pointNumber: 33,
bgcolor: 'rgb(0, 0, 238)',
bordercolor: 'rgb(255, 255, 255)',
fontSize: 13,
fontFamily: 'Arial',
fontColor: 'rgb(255, 255, 255)'
}, {
msg: 'scattergl with hovertext'
});
Plotly.newPlot(gd, _mock)
.then(run)
.then(done, done.fail);
});
it('@gl should not error when scattergl trace has missing points', function(done) {
var _mock = {
data: [{
type: 'scattergl',
mode: 'markers',
x: [1, 2, 3, 4],
y: [10, 15, null, 17],
}],
layout: {
width: 500,
height: 500
}
};
Plotly.newPlot(gd, _mock)
.then(function() {
gd.on('plotly_hover', function() {
fail('should not trigger plotly_hover event');
});
})
.then(function() {
var xp = 300;
var yp = 250;
var interval = setInterval(function() { hover(xp--, yp--); }, 10);
return delay(100)().then(function() { clearInterval(interval); });
})
.then(done, done.fail);
});
it('@gl should show last point data for overlapped scattergl points with hovermode set to closest', function(done) {
var _mock = Lib.extendDeep({}, mock1);
_mock.data[0].hovertext = 'text';
_mock.data[0].hovertext = 'HoVeRtExT';
_mock.layout.hovermode = 'closest';
var run = makeRunner([200, 200], {
x: 2,
y: 0,
label: ['(2, 0)\nTRUE', null],
curveNumber: 0,
pointNumber: 3,
bgcolor: 'rgb(31, 119, 180)',
bordercolor: 'rgb(255, 255, 255)',
fontSize: 13,
fontFamily: 'Arial',
fontColor: 'rgb(255, 255, 255)'
}, {
msg: 'scattergl with hovertext'
});
Plotly.newPlot(gd, {
data: [{
text: ['', 'FALSE', '', 'TRUE'],
x: [1, 2, 3, 2],
y: [0, 0, 0, 0],
type: 'scattergl',
mode: 'markers',
marker: { size: 40 }
}],
layout: {
width: 400,
height: 400,
hovermode: 'closest'
}
})
.then(run)
.then(done, done.fail);
});
it('@gl scattergl should propagate marker colors to hover labels', function(done) {
var _mock = Lib.extendDeep({}, mock0);
_mock.layout.hovermode = 'x';
_mock.layout.width = 800;
_mock.layout.height = 600;
var run = makeRunner([700, 300], {
x: 15075859,
y: 79183,
curveNumber: 0,
pointNumber: 0,
bgcolor: 'rgb(202, 178, 214)',
bordercolor: 'rgb(68, 68, 68)',
fontSize: 13,
fontFamily: 'Arial',
fontColor: 'rgb(68, 68, 68)'
}, {
msg: 'scattergl marker colors'
});
Plotly.newPlot(gd, _mock)
.then(run)
.then(done, done.fail);
});
it('@gl should output correct event data for scattergl after visibility restyle', function(done) {
var _mock = Lib.extendDeep({}, mock2);
var run = makeRunner([435, 216], {
x: 8,
y: 18,
curveNumber: 2,
pointNumber: 0,
bgcolor: 'rgb(44, 160, 44)',
bordercolor: 'rgb(255, 255, 255)',
fontSize: 13,
fontFamily: 'Arial',
fontColor: 'rgb(255, 255, 255)'
}, {
msg: 'scattergl before visibility restyle'
});
// after the restyle, autorange changes the y range
var run2 = makeRunner([435, 106], {
x: 8,
y: 18,
curveNumber: 2,
pointNumber: 0,
bgcolor: 'rgb(255, 127, 14)',
bordercolor: 'rgb(68, 68, 68)',
fontSize: 13,
fontFamily: 'Arial',
fontColor: 'rgb(68, 68, 68)'
}, {
msg: 'scattergl after visibility restyle'
});
Plotly.newPlot(gd, _mock)
.then(run)
.then(function() {
return Plotly.restyle(gd, 'visible', false, [1]);
})
.then(run2)
.then(done, done.fail);
});
it('@gl should output correct event data for scattergl-fancy', function(done) {
var _mock = Lib.extendDeep({}, mock2);
_mock.data[0].mode = 'markers+lines';
_mock.data[1].mode = 'markers+lines';
_mock.data[2].mode = 'markers+lines';
var run = makeRunner([435, 216], {
x: 8,
y: 18,
curveNumber: 2,
pointNumber: 0,
bgcolor: 'rgb(44, 160, 44)',
bordercolor: 'rgb(255, 255, 255)',
fontSize: 13,
fontFamily: 'Arial',
fontColor: 'rgb(255, 255, 255)'
}, {
msg: 'scattergl fancy before visibility restyle'
});
// after the restyle, autorange changes the x AND y ranges
// I don't get why the x range changes, nor why the y changes in
// a different way than in the previous test, but they do look
// correct on the screen during the test.
var run2 = makeRunner([426, 116], {
x: 8,
y: 18,
curveNumber: 2,
pointNumber: 0,
bgcolor: 'rgb(255, 127, 14)',
bordercolor: 'rgb(68, 68, 68)',
fontSize: 13,
fontFamily: 'Arial',
fontColor: 'rgb(68, 68, 68)'
}, {
msg: 'scattergl fancy after visibility restyle'
});
Plotly.newPlot(gd, _mock)
.then(run)
.then(function() {
return Plotly.restyle(gd, 'visible', false, [1]);
})
.then(run2)
.then(done, done.fail);
});
});
describe('hover with (x|y)period positioning', function() {
'use strict';
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
function _hover(x, y) {
delete gd._hoverdata;
Lib.clearThrottle();
mouseEvent('mousemove', x, y);
}
it('@gl shows hover info for scattergl', function(done) {
Plotly.newPlot(gd, require('../../image/mocks/gl2d_period_positioning.json'))
.then(function() { _hover(100, 255); })
.then(function() {
assertHoverLabelContent({
name: '',
nums: '(Jan 2001, Jan 1, 1970)'
});
})
.then(function() { _hover(470, 45); })
.then(function() {
assertHoverLabelContent({
name: '',
nums: '(Jan 2006, Jun 1, 1970)'
});
})
.then(done, done.fail);
});
});