forked from plotly/plotly.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhover_label_test.js
986 lines (778 loc) · 35.2 KB
/
hover_label_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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
var d3 = require('d3');
var Plotly = require('@lib/index');
var Fx = require('@src/plots/cartesian/graph_interact');
var constants = require('@src/plots/cartesian/constants');
var Lib = require('@src/lib');
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var mouseEvent = require('../assets/mouse_event');
var click = require('../assets/click');
var delay = require('../assets/delay');
var doubleClick = require('../assets/double_click');
var fail = require('../assets/fail_test');
describe('hover info', function() {
'use strict';
var mock = require('@mocks/14.json'),
evt = {
clientX: mock.layout.width / 2,
clientY: mock.layout.height / 2
};
afterEach(destroyGraphDiv);
describe('hover info', function() {
var mockCopy = Lib.extendDeep({}, mock);
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
expect(d3.selectAll('g.hovertext').select('text').html()).toEqual('1');
});
});
describe('hover info x', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].hoverinfo = 'x';
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover x', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').size()).toEqual(0);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
});
});
describe('hover info y', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].hoverinfo = 'y';
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover y', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(0);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').select('text').html()).toEqual('1');
});
});
describe('hover info text', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].text = [];
// we convert newlines to spaces
// see https://github.com/plotly/plotly.js/issues/746
mockCopy.data[0].text[17] = 'hover\ntext\n\rwith\r\nspaces\n\nnot\rnewlines';
mockCopy.data[0].hoverinfo = 'text';
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover text', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(0);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').select('text').html())
.toEqual('hover text with spaces not newlines');
});
});
describe('hover info all', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].text = [];
mockCopy.data[0].text[17] = 'hover text';
mockCopy.data[0].hoverinfo = 'all';
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover all', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
expect(d3.selectAll('g.hovertext').select('text').selectAll('tspan').size()).toEqual(2);
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][0].innerHTML).toEqual('1');
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][1].innerHTML).toEqual('hover text');
});
});
describe('hover info with bad name', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].text = [];
mockCopy.data[0].text[17] = 'hover text';
mockCopy.data[0].hoverinfo = 'all';
mockCopy.data[0].name = '<img src=x onerror=y>';
mockCopy.data.push({
x: [0.002, 0.004],
y: [12.5, 16.25],
mode: 'lines+markers',
name: 'another trace'
});
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('cleans the name', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
expect(d3.selectAll('g.hovertext').select('text.nums').selectAll('tspan').size()).toEqual(2);
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][0].innerHTML).toEqual('1');
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][1].innerHTML).toEqual('hover text');
expect(d3.selectAll('g.hovertext').selectAll('text.name').node().innerHTML).toEqual('<img src=x o...');
});
});
describe('hover info y+text', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].text = [];
mockCopy.data[0].text[17] = 'hover text';
mockCopy.data[0].hoverinfo = 'y+text';
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover y+text', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(0);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').selectAll('tspan').size()).toEqual(2);
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][0].innerHTML).toEqual('1');
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][1].innerHTML).toEqual('hover text');
});
});
describe('hover info x+text', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].text = [];
mockCopy.data[0].text[17] = 'hover text';
mockCopy.data[0].hoverinfo = 'x+text';
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover x+text', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
expect(d3.selectAll('g.hovertext').select('text').html()).toEqual('hover text');
});
});
describe('hover error x text (log axis positive)', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].error_x = { array: [] };
mockCopy.data[0].error_x.array[17] = 1;
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover x+text', function() {
Fx.hover('graph', evt, 'xy');
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388 ± 1');
});
});
describe('hover error text (log axis 0)', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].error_x = { array: [] };
mockCopy.data[0].error_x.array[17] = 0;
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover x+text', function() {
Fx.hover('graph', evt, 'xy');
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
});
});
describe('hover error text (log axis negative)', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].error_x = { array: [] };
mockCopy.data[0].error_x.array[17] = -1;
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover x+text', function() {
Fx.hover('graph', evt, 'xy');
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0.388');
});
});
describe('hover info text with html', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].text = [];
mockCopy.data[0].text[17] = 'hover<br>text';
mockCopy.data[0].hoverinfo = 'text';
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('responds to hover text with html', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(0);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][0].innerHTML).toEqual('hover');
expect(d3.selectAll('g.hovertext').selectAll('tspan')[0][1].innerHTML).toEqual('text');
expect(d3.selectAll('g.hovertext').select('text').selectAll('tspan').size()).toEqual(2);
});
});
describe('hover info skip', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].hoverinfo = 'skip';
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('does not hover if hover info is set to skip', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
expect(gd._hoverdata, undefined);
});
});
describe('hover info none', function() {
var mockCopy = Lib.extendDeep({}, mock);
mockCopy.data[0].hoverinfo = 'none';
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mockCopy.data, mockCopy.layout).then(done);
});
it('does not render if hover is set to none', function() {
var gd = document.getElementById('graph');
Fx.hover('graph', evt, 'xy');
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.curveNumber).toEqual(0);
expect(hoverTrace.pointNumber).toEqual(17);
expect(hoverTrace.x).toEqual(0.388);
expect(hoverTrace.y).toEqual(1);
expect(d3.selectAll('g.axistext').size()).toEqual(0);
expect(d3.selectAll('g.hovertext').size()).toEqual(0);
});
});
describe('\'closest\' hover info (superimposed case)', function() {
var mockCopy = Lib.extendDeep({}, mock);
// superimposed traces
mockCopy.data.push(Lib.extendDeep({}, mockCopy.data[0]));
mockCopy.layout.hovermode = 'closest';
var gd;
beforeEach(function(done) {
gd = createGraphDiv();
Plotly.plot(gd, mockCopy.data, mockCopy.layout).then(done);
});
it('render hover labels of the above trace', function() {
Fx.hover('graph', evt, 'xy');
expect(gd._hoverdata.length).toEqual(1);
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.fullData.index).toEqual(1);
expect(hoverTrace.curveNumber).toEqual(1);
expect(hoverTrace.pointNumber).toEqual(16);
expect(hoverTrace.x).toEqual(0.33);
expect(hoverTrace.y).toEqual(1.25);
expect(d3.selectAll('g.axistext').size()).toEqual(0);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
var expectations = ['PV learning ...', '(0.33, 1.25)'];
d3.selectAll('g.hovertext').selectAll('text').each(function(_, i) {
expect(d3.select(this).html()).toEqual(expectations[i]);
});
});
it('render only non-hoverinfo \'none\' hover labels', function(done) {
Plotly.restyle(gd, 'hoverinfo', ['none', 'name']).then(function() {
Fx.hover('graph', evt, 'xy');
expect(gd._hoverdata.length).toEqual(1);
var hoverTrace = gd._hoverdata[0];
expect(hoverTrace.fullData.index).toEqual(1);
expect(hoverTrace.curveNumber).toEqual(1);
expect(hoverTrace.pointNumber).toEqual(16);
expect(hoverTrace.x).toEqual(0.33);
expect(hoverTrace.y).toEqual(1.25);
expect(d3.selectAll('g.axistext').size()).toEqual(0);
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
var text = d3.selectAll('g.hovertext').select('text');
expect(text.size()).toEqual(1);
expect(text.html()).toEqual('PV learning ...');
done();
});
});
});
describe('hoverformat', function() {
var data = [{
x: [1, 2, 3],
y: [0.12345, 0.23456, 0.34567]
}],
layout = {
yaxis: { showticklabels: true, hoverformat: ',.2r' },
width: 600,
height: 400
};
beforeEach(function() {
this.gd = createGraphDiv();
});
it('should display the correct format when ticklabels true', function() {
Plotly.plot(this.gd, data, layout);
mouseEvent('mousemove', 303, 213);
var hovers = d3.selectAll('g.hovertext');
expect(hovers.size()).toEqual(1);
expect(hovers.select('text')[0][0].textContent).toEqual('0.23');
});
it('should display the correct format when ticklabels false', function() {
layout.yaxis.showticklabels = false;
Plotly.plot(this.gd, data, layout);
mouseEvent('mousemove', 303, 213);
var hovers = d3.selectAll('g.hovertext');
expect(hovers.size()).toEqual(1);
expect(hovers.select('text')[0][0].textContent).toEqual('0.23');
});
});
describe('textmode', function() {
var data = [{
x: [1, 2, 3, 4],
y: [2, 3, 4, 5],
mode: 'text',
hoverinfo: 'text',
text: ['test', null, 42, undefined]
}],
layout = {
width: 600,
height: 400
};
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), data, layout).then(done);
});
it('should show text labels', function() {
mouseEvent('mousemove', 108, 303);
var hovers = d3.selectAll('g.hovertext');
expect(hovers.size()).toEqual(1);
expect(hovers.select('text')[0][0].textContent).toEqual('test');
});
it('should show number labels', function() {
mouseEvent('mousemove', 363, 173);
var hovers = d3.selectAll('g.hovertext');
expect(hovers.size()).toEqual(1);
expect(hovers.select('text')[0][0].textContent).toEqual('42');
});
it('should not show null text labels', function() {
mouseEvent('mousemove', 229, 239);
var hovers = d3.selectAll('g.hovertext');
expect(hovers.size()).toEqual(0);
});
it('should not show undefined text labels', function() {
mouseEvent('mousemove', 493, 108);
var hovers = d3.selectAll('g.hovertext');
expect(hovers.size()).toEqual(0);
});
});
describe('hover events', function() {
var data = [{x: [1, 2, 3], y: [1, 3, 2], type: 'bar'}];
var layout = {width: 600, height: 400};
var gd;
beforeEach(function(done) {
gd = createGraphDiv();
Plotly.plot(gd, data, layout).then(done);
});
fit('should emit events only if the event looks user-driven', function(done) {
var hoverHandler = jasmine.createSpy();
gd.on('plotly_hover', hoverHandler);
var gdBB = gd.getBoundingClientRect();
var event = {clientX: gdBB.left + 300, clientY: gdBB.top + 200};
Promise.resolve().then(function() {
Fx.hover(gd, event, 'xy');
})
.then(delay(constants.HOVERMINTIME * 1.1))
.then(function() {
Fx.unhover(gd);
})
.then(function() {
expect(hoverHandler).not.toHaveBeenCalled();
var dragger = gd.querySelector('.nsewdrag');
Fx.hover(gd, Lib.extendFlat({target: dragger}, event), 'xy');
})
.then(function() {
expect(hoverHandler).toHaveBeenCalledTimes(1);
})
.catch(fail)
.then(done);
});
});
});
describe('hover info on stacked subplots', function() {
'use strict';
afterEach(destroyGraphDiv);
describe('hover info on stacked subplots with shared x-axis', function() {
var mock = require('@mocks/stacked_coupled_subplots.json');
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
});
it('responds to hover', function() {
var gd = document.getElementById('graph');
Plotly.Fx.hover(gd, {xval: 3}, ['xy', 'xy2', 'xy3']);
expect(gd._hoverdata.length).toEqual(2);
expect(gd._hoverdata[0]).toEqual(jasmine.objectContaining(
{
curveNumber: 1,
pointNumber: 1,
x: 3,
y: 110
}));
expect(gd._hoverdata[1]).toEqual(jasmine.objectContaining(
{
curveNumber: 2,
pointNumber: 0,
x: 3,
y: 1000
}));
// There should be a single label on the x-axis with the shared x value, 3.
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('3');
// There should be two points being hovered over, in two different traces, one in each plot.
expect(d3.selectAll('g.hovertext').size()).toEqual(2);
var textNodes = d3.selectAll('g.hovertext').selectAll('text');
expect(textNodes[0][0].innerHTML).toEqual('trace 1');
expect(textNodes[0][1].innerHTML).toEqual('110');
expect(textNodes[1][0].innerHTML).toEqual('trace 2');
expect(textNodes[1][1].innerHTML).toEqual('1000');
});
});
describe('hover info on stacked subplots with shared y-axis', function() {
var mock = require('@mocks/stacked_subplots_shared_yaxis.json');
beforeEach(function(done) {
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(done);
});
it('responds to hover', function() {
var gd = document.getElementById('graph');
Plotly.Fx.hover(gd, {yval: 0}, ['xy', 'x2y', 'x3y']);
expect(gd._hoverdata.length).toEqual(3);
expect(gd._hoverdata[0]).toEqual(jasmine.objectContaining(
{
curveNumber: 0,
pointNumber: 0,
x: 1,
y: 0
}));
expect(gd._hoverdata[1]).toEqual(jasmine.objectContaining(
{
curveNumber: 1,
pointNumber: 0,
x: 2.1,
y: 0
}));
expect(gd._hoverdata[2]).toEqual(jasmine.objectContaining(
{
curveNumber: 2,
pointNumber: 0,
x: 3,
y: 0
}));
// There should be a single label on the y-axis with the shared y value, 0.
expect(d3.selectAll('g.axistext').size()).toEqual(1);
expect(d3.selectAll('g.axistext').select('text').html()).toEqual('0');
// There should be three points being hovered over, in three different traces, one in each plot.
expect(d3.selectAll('g.hovertext').size()).toEqual(3);
var textNodes = d3.selectAll('g.hovertext').selectAll('text');
expect(textNodes[0][0].innerHTML).toEqual('trace 0');
expect(textNodes[0][1].innerHTML).toEqual('1');
expect(textNodes[1][0].innerHTML).toEqual('trace 1');
expect(textNodes[1][1].innerHTML).toEqual('2.1');
expect(textNodes[2][0].innerHTML).toEqual('trace 2');
expect(textNodes[2][1].innerHTML).toEqual('3');
});
});
});
describe('hover info on overlaid subplots', function() {
'use strict';
afterEach(destroyGraphDiv);
it('should respond to hover', function(done) {
var mock = require('@mocks/autorange-tozero-rangemode.json');
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(function() {
mouseEvent('mousemove', 768, 345);
var axisText = d3.selectAll('g.axistext'),
hoverText = d3.selectAll('g.hovertext');
expect(axisText.size()).toEqual(1, 'with 1 label on axis');
expect(hoverText.size()).toEqual(2, 'with 2 labels on the overlaid pts');
expect(axisText.select('text').html()).toEqual('1', 'with correct axis label');
var textNodes = hoverText.selectAll('text');
expect(textNodes[0][0].innerHTML).toEqual('Take Rate', 'with correct hover labels');
expect(textNodes[0][1].innerHTML).toEqual('0.35', 'with correct hover labels');
expect(textNodes[1][0].innerHTML).toEqual('Revenue', 'with correct hover labels');
expect(textNodes[1][1].innerHTML).toEqual('2,352.5', 'with correct hover labels');
}).then(done);
});
});
describe('hover after resizing', function() {
'use strict';
afterEach(destroyGraphDiv);
function _click(pos) {
return new Promise(function(resolve) {
click(pos[0], pos[1]);
setTimeout(function() {
resolve();
}, constants.HOVERMINTIME);
});
}
function assertLabelCount(pos, cnt, msg) {
return new Promise(function(resolve) {
mouseEvent('mousemove', pos[0], pos[1]);
setTimeout(function() {
var hoverText = d3.selectAll('g.hovertext');
expect(hoverText.size()).toEqual(cnt, msg);
resolve();
}, constants.HOVERMINTIME);
});
}
it('should work', function(done) {
var data = [{ y: [2, 1, 2] }],
layout = { width: 600, height: 500 },
gd = createGraphDiv();
var pos0 = [305, 403],
pos1 = [401, 122];
Plotly.plot(gd, data, layout).then(function() {
// to test https://github.com/plotly/plotly.js/issues/1044
return _click(pos0);
})
.then(function() {
return assertLabelCount(pos0, 1, 'before resize, showing pt label');
})
.then(function() {
return assertLabelCount(pos1, 0, 'before resize, not showing blank spot');
})
.then(function() {
return Plotly.relayout(gd, 'width', 500);
})
.then(function() {
return assertLabelCount(pos0, 0, 'after resize, not showing blank spot');
})
.then(function() {
return assertLabelCount(pos1, 1, 'after resize, showing pt label');
})
.then(function() {
return Plotly.relayout(gd, 'width', 600);
})
.then(function() {
return assertLabelCount(pos0, 1, 'back to initial, showing pt label');
})
.then(function() {
return assertLabelCount(pos1, 0, 'back to initial, not showing blank spot');
})
.then(done);
});
});
describe('hover on fill', function() {
'use strict';
afterEach(destroyGraphDiv);
function assertLabelsCorrect(mousePos, labelPos, labelText) {
return new Promise(function(resolve) {
mouseEvent('mousemove', mousePos[0], mousePos[1]);
setTimeout(function() {
var hoverText = d3.selectAll('g.hovertext');
expect(hoverText.size()).toEqual(1);
expect(hoverText.text()).toEqual(labelText);
var transformParts = hoverText.attr('transform').split('(');
expect(transformParts[0]).toEqual('translate');
var transformCoords = transformParts[1].split(')')[0].split(',');
expect(+transformCoords[0]).toBeCloseTo(labelPos[0], -1.2, labelText + ':x');
expect(+transformCoords[1]).toBeCloseTo(labelPos[1], -1.2, labelText + ':y');
resolve();
}, constants.HOVERMINTIME);
});
}
it('should always show one label in the right place', function(done) {
var mock = Lib.extendDeep({}, require('@mocks/scatter_fill_self_next.json'));
mock.data.forEach(function(trace) { trace.hoveron = 'fills'; });
Plotly.plot(createGraphDiv(), mock.data, mock.layout).then(function() {
return assertLabelsCorrect([242, 142], [252, 133.8], 'trace 2');
}).then(function() {
return assertLabelsCorrect([242, 292], [233, 210], 'trace 1');
}).then(function() {
return assertLabelsCorrect([147, 252], [158.925, 248.1], 'trace 0');
}).then(done);
});
it('should work for scatterternary too', function(done) {
var mock = Lib.extendDeep({}, require('@mocks/ternary_fill.json'));
var gd = createGraphDiv();
Plotly.plot(gd, mock.data, mock.layout).then(function() {
// hover over a point when that's closest, even if you're over
// a fill, because by default we have hoveron='points+fills'
return assertLabelsCorrect([237, 150], [240.0, 144],
'trace 2Component A: 0.8Component B: 0.1Component C: 0.1');
}).then(function() {
// the rest are hovers over fills
return assertLabelsCorrect([237, 170], [247.7, 166], 'trace 2');
}).then(function() {
return assertLabelsCorrect([237, 218], [266.75, 265], 'trace 1');
}).then(function() {
return assertLabelsCorrect([237, 240], [247.7, 254], 'trace 0');
}).then(function() {
// zoom in to test clipping of large out-of-viewport shapes
return Plotly.relayout(gd, {
'ternary.aaxis.min': 0.5,
'ternary.baxis.min': 0.25
});
}).then(function() {
// this particular one has a hover label disconnected from the shape itself
// so if we ever fix this, the test will have to be fixed too.
return assertLabelsCorrect([295, 218], [275.1, 166], 'trace 2');
}).then(function() {
// trigger an autoscale redraw, which goes through dragElement
return doubleClick(237, 251);
}).then(function() {
// then make sure we can still select a *different* item afterward
return assertLabelsCorrect([237, 218], [266.75, 265], 'trace 1');
}).then(done);
});
});
describe('hover updates', function() {
'use strict';
afterEach(destroyGraphDiv);
function assertLabelsCorrect(mousePos, labelPos, labelText) {
return new Promise(function(resolve) {
if(mousePos) {
mouseEvent('mousemove', mousePos[0], mousePos[1]);
}
setTimeout(function() {
var hoverText = d3.selectAll('g.hovertext');
if(labelPos) {
expect(hoverText.size()).toEqual(1);
expect(hoverText.text()).toEqual(labelText);
var transformParts = hoverText.attr('transform').split('(');
expect(transformParts[0]).toEqual('translate');
var transformCoords = transformParts[1].split(')')[0].split(',');
expect(+transformCoords[0]).toBeCloseTo(labelPos[0], -1, labelText + ':x');
expect(+transformCoords[1]).toBeCloseTo(labelPos[1], -1, labelText + ':y');
} else {
expect(hoverText.size()).toEqual(0);
}
resolve();
}, constants.HOVERMINTIME);
});
}
it('should update the labels on animation', function(done) {
var mock = {
data: [
{x: [0.5], y: [0.5], showlegend: false},
{x: [0], y: [0], showlegend: false},
],
layout: {
margin: {t: 0, r: 0, b: 0, l: 0},
width: 200,
height: 200,
xaxis: {range: [0, 1]},
yaxis: {range: [0, 1]},
}
};
var gd = createGraphDiv();
Plotly.plot(gd, mock).then(function() {
// The label text gets concatenated together when queried. Such is life.
return assertLabelsCorrect([100, 100], [103, 100], 'trace 00.5');
}).then(function() {
return Plotly.animate(gd, [{
data: [{x: [0], y: [0]}, {x: [0.5], y: [0.5]}],
traces: [0, 1],
}], {frame: {redraw: false, duration: 0}});
}).then(function() {
// No mouse event this time. Just change the data and check the label.
// Ditto on concatenation. This is "trace 1" + "0.5"
return assertLabelsCorrect(null, [103, 100], 'trace 10.5');
}).then(function() {
// Restyle to move the point out of the window:
return Plotly.relayout(gd, {'xaxis.range': [2, 3]});
}).then(function() {
// Assert label removed:
return assertLabelsCorrect(null, null);
}).then(function() {
// Move back to the original xaxis range:
return Plotly.relayout(gd, {'xaxis.range': [0, 1]});
}).then(function() {
// Assert label restored:
return assertLabelsCorrect(null, [103, 100], 'trace 10.5');
}).catch(fail).then(done);
});
it('should not trigger infinite loop of plotly_unhover events', function(done) {
var gd = createGraphDiv();
var colors0 = ['#00000', '#00000', '#00000', '#00000', '#00000', '#00000', '#00000'];
function unhover() {
return new Promise(function(resolve) {
mouseEvent('mousemove', 394, 285);
setTimeout(function() {
resolve();
}, constants.HOVERMINTIME);
});
}
var hoverCnt = 0;
var unHoverCnt = 0;
Plotly.plot(gd, [{
mode: 'markers',
x: [1, 2, 3, 4, 5, 6, 7],
y: [1, 2, 3, 2, 3, 4, 3],
marker: {
size: 16,
colors: colors0.slice()
}
}])
.then(function() {
gd.on('plotly_hover', function(eventData) {
hoverCnt++;
var pt = eventData.points[0];
Plotly.restyle(gd, 'marker.color[' + pt.pointNumber + ']', 'red');
});
gd.on('plotly_unhover', function() {
unHoverCnt++;
Plotly.restyle(gd, 'marker.color', [colors0.slice()]);
});
return assertLabelsCorrect([351, 251], [358, 272], '2');
})
.then(unhover)
.then(function() {
expect(hoverCnt).toEqual(1);
expect(unHoverCnt).toEqual(1);
return assertLabelsCorrect([400, 200], [435, 198], '3');
})
.then(unhover)
.then(function() {
expect(hoverCnt).toEqual(2);
expect(unHoverCnt).toEqual(2);
})
.then(done);
});
});