-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathscattergeo_test.js
525 lines (453 loc) · 16.4 KB
/
scattergeo_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
var Plotly = require('@lib');
var Lib = require('@src/lib');
var BADNUM = require('@src/constants/numerical').BADNUM;
var loggers = require('@src/lib/loggers');
var ScatterGeo = require('@src/traces/scattergeo');
var d3Select = require('../../strict-d3').select;
var d3SelectAll = require('../../strict-d3').selectAll;
var createGraphDiv = require('../assets/create_graph_div');
var destroyGraphDiv = require('../assets/destroy_graph_div');
var mouseEvent = require('../assets/mouse_event');
var customAssertions = require('../assets/custom_assertions');
var assertHoverLabelStyle = customAssertions.assertHoverLabelStyle;
var assertHoverLabelContent = customAssertions.assertHoverLabelContent;
var checkTextTemplate = require('../assets/check_texttemplate');
var supplyAllDefaults = require('../assets/supply_defaults');
describe('Test scattergeo defaults', function() {
var traceIn,
traceOut;
var defaultColor = '#444';
var layout = {};
beforeEach(function() {
traceOut = {};
});
it('should not slice lat if it it longer than lon', function() {
// this is handled at the calc step now via _length.
traceIn = {
lon: [-75],
lat: [45, 45, 45]
};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.lat).toEqual([45, 45, 45]);
expect(traceOut.lon).toEqual([-75]);
expect(traceOut._length).toBe(1);
});
it('should slice lon if it it longer than lat', function() {
// this is handled at the calc step now via _length.
traceIn = {
lon: [-75, -75, -75],
lat: [45]
};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.lat).toEqual([45]);
expect(traceOut.lon).toEqual([-75, -75, -75]);
expect(traceOut._length).toBe(1);
});
it('should not coerce lat and lon if locations is valid', function() {
traceIn = {
locations: ['CAN', 'USA'],
lon: [20, 40],
lat: [20, 40]
};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.lon).toBeUndefined();
expect(traceOut.lat).toBeUndefined();
});
it('should make trace invisible if lon or lat is omitted and locations not given', function() {
function testOne() {
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.visible).toBe(false);
}
traceIn = {
lat: [45, 45, 45]
};
testOne();
traceIn = {
lon: [-75, -75, -75]
};
traceOut = {};
testOne();
traceIn = {};
traceOut = {};
testOne();
});
it('should default locationmode to *geojson-id* when a valid *geojson* is provided', function() {
traceIn = {
locations: ['CAN', 'USA'],
geojson: 'url'
};
traceOut = {};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.locationmode).toBe('geojson-id', 'valid url string');
traceIn = {
locations: ['CAN', 'USA'],
geojson: {}
};
traceOut = {};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.locationmode).toBe('geojson-id', 'valid object');
traceIn = {
locations: ['CAN', 'USA'],
geojson: ''
};
traceOut = {};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.locationmode).toBe('ISO-3', 'invalid sting');
traceIn = {
locations: ['CAN', 'USA'],
geojson: []
};
traceOut = {};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.locationmode).toBe('ISO-3', 'invalid object');
traceIn = {
lon: [20, 40],
lat: [20, 40]
};
traceOut = {};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.locationmode).toBe(undefined, 'lon/lat coordinates');
});
it('should only coerce *featureidkey* when locationmode is *geojson-id', function() {
traceIn = {
locations: ['CAN', 'USA'],
geojson: 'url',
featureidkey: 'properties.name'
};
traceOut = {};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.featureidkey).toBe('properties.name', 'coerced');
traceIn = {
locations: ['CAN', 'USA'],
featureidkey: 'properties.name'
};
traceOut = {};
ScatterGeo.supplyDefaults(traceIn, traceOut, defaultColor, layout);
expect(traceOut.featureidkey).toBe(undefined, 'NOT coerced');
});
});
describe('Test scattergeo calc', function() {
function _calc(opts) {
var base = { type: 'scattergeo' };
var trace = Lib.extendFlat({}, base, opts);
var gd = { data: [trace] };
supplyAllDefaults(gd);
var fullTrace = gd._fullData[0];
return ScatterGeo.calc(gd, fullTrace).map(function(obj) {
delete obj.i;
delete obj.t;
return obj;
});
}
it('should place lon/lat data in lonlat pairs', function() {
var calcTrace = _calc({
lon: [10, 20, 30],
lat: [20, 30, 10]
});
expect(calcTrace).toEqual([
{ lonlat: [10, 20] },
{ lonlat: [20, 30] },
{ lonlat: [30, 10] }
]);
});
it('should coerce numeric strings lon/lat data into numbers', function() {
var calcTrace = _calc({
lon: [10, 20, '30', '40'],
lat: [20, '30', 10, '50']
});
expect(calcTrace).toEqual([
{ lonlat: [10, 20] },
{ lonlat: [20, 30] },
{ lonlat: [30, 10] },
{ lonlat: [40, 50] }
]);
});
it('should set non-numeric values lon/lat pairs to BADNUM', function() {
var calcTrace = _calc({
lon: [null, 10, null, null, 20, '30', null, '40', null, 10],
lat: [10, 20, '30', null, 10, '50', null, 60, null, null]
});
expect(calcTrace).toEqual([
{ lonlat: [BADNUM, BADNUM] },
{ lonlat: [10, 20] },
{ lonlat: [BADNUM, BADNUM] },
{ lonlat: [BADNUM, BADNUM] },
{ lonlat: [20, 10] },
{ lonlat: [30, 50] },
{ lonlat: [BADNUM, BADNUM] },
{ lonlat: [40, 60] },
{ lonlat: [BADNUM, BADNUM] },
{ lonlat: [BADNUM, BADNUM] }
]);
});
it('should fill array text (base case)', function() {
var calcTrace = _calc({
lon: [10, 20, 30, null, 40],
lat: [20, 30, 10, 'no-good', 50],
text: ['A', 'B', 'C', 'D', 'E']
});
expect(calcTrace).toEqual([
{ lonlat: [10, 20], tx: 'A' },
{ lonlat: [20, 30], tx: 'B' },
{ lonlat: [30, 10], tx: 'C' },
{ lonlat: [BADNUM, BADNUM], tx: 'D' },
{ lonlat: [40, 50], tx: 'E' }
]);
});
it('should fill array text (invalid entry case)', function() {
var calcTrace = _calc({
lon: [10, 20, 30, null, 40],
lat: [20, 30, 10, 'no-good', 50],
text: ['A', null, 'C', 'D', {}]
});
expect(calcTrace).toEqual([
{ lonlat: [10, 20], tx: 'A' },
{ lonlat: [20, 30], tx: null },
{ lonlat: [30, 10], tx: 'C' },
{ lonlat: [BADNUM, BADNUM], tx: 'D' },
{ lonlat: [40, 50], tx: {} }
]);
});
it('should fill array marker attributes (base case)', function() {
var calcTrace = _calc({
lon: [10, 20, null, 30],
lat: [20, 30, null, 10],
marker: {
color: ['red', 'blue', 'green', 'yellow'],
size: [10, 20, 8, 10]
}
});
expect(calcTrace).toEqual([
{ lonlat: [10, 20], mc: 'red', ms: 10 },
{ lonlat: [20, 30], mc: 'blue', ms: 20 },
{ lonlat: [BADNUM, BADNUM], mc: 'green', ms: 8 },
{ lonlat: [30, 10], mc: 'yellow', ms: 10 }
]);
});
it('should fill array marker attributes (invalid scale case)', function() {
var calcTrace = _calc({
lon: [10, 20, null, 30],
lat: [20, 30, null, 10],
marker: {
color: [0, null, 5, 10],
size: [10, NaN, 8, 10],
colorscale: [
[0, 'blue'], [0.5, 'red'], [1, 'green']
]
}
});
expect(calcTrace).toEqual([
{ lonlat: [10, 20], mc: 0, ms: 10 },
{ lonlat: [20, 30], mc: null, ms: 0 },
{ lonlat: [BADNUM, BADNUM], mc: 5, ms: 8 },
{ lonlat: [30, 10], mc: 10, ms: 10 }
]);
});
it('should fill marker attributes (symbol case)', function() {
var calcTrace = _calc({
lon: [10, 20, null, 30],
lat: [20, 30, null, 10],
marker: {
symbol: ['cross', 'square', 'diamond', null]
}
});
expect(calcTrace).toEqual([
{ lonlat: [10, 20], mx: 'cross' },
{ lonlat: [20, 30], mx: 'square' },
{ lonlat: [BADNUM, BADNUM], mx: 'diamond' },
{ lonlat: [30, 10], mx: null }
]);
});
});
describe('Test scattergeo hover', function() {
var gd;
beforeEach(function(done) {
gd = createGraphDiv();
Plotly.newPlot(gd, [{
type: 'scattergeo',
lon: [10, 20, 30],
lat: [10, 20, 30],
text: ['A', 'B', 'C']
}])
.then(done);
});
afterEach(destroyGraphDiv);
function check(pos, content, style) {
mouseEvent('mousemove', pos[0], pos[1]);
style = style || {
bgcolor: 'rgb(31, 119, 180)',
bordercolor: 'rgb(255, 255, 255)',
fontColor: 'rgb(255, 255, 255)',
fontSize: 13,
fontFamily: 'Arial'
};
assertHoverLabelContent({
nums: content[0],
name: content[1]
});
assertHoverLabelStyle(
d3Select('g.hovertext'),
style
);
}
it('should generate hover label info (base case)', function() {
check([381, 221], ['(10°, 10°)\nA', null]);
});
it('should generate hover label info (with trace name)', function(done) {
Plotly.restyle(gd, 'hoverinfo', 'lon+lat+text+name').then(function() {
check([381, 221], ['(10°, 10°)\nA', 'trace 0']);
})
.then(done, done.fail);
});
it('should use the hovertemplate', function(done) {
Plotly.restyle(gd, 'hovertemplate', 'tpl %{lat}<extra>x</extra>').then(function() {
check([381, 221], ['tpl 10', 'x']);
})
.then(done, done.fail);
});
it('should not hide hover label when hovertemplate', function(done) {
Plotly.restyle(gd, {
name: '',
hovertemplate: 'tpl %{lat}<extra>x</extra>'
}).then(function() {
check([381, 221], ['tpl 10', 'x']);
})
.then(done, done.fail);
});
it('should generate hover label info (\'text\' single value case)', function(done) {
Plotly.restyle(gd, 'text', 'text').then(function() {
check([381, 221], ['(10°, 10°)\ntext', null]);
})
.then(done, done.fail);
});
it('should generate hover label info (\'hovertext\' single value case)', function(done) {
Plotly.restyle(gd, 'hovertext', 'hovertext').then(function() {
check([381, 221], ['(10°, 10°)\nhovertext', null]);
})
.then(done, done.fail);
});
it('should generate hover label info (\'hovertext\' array case)', function(done) {
Plotly.restyle(gd, 'hovertext', ['Apple', 'Banana', 'Orange']).then(function() {
check([381, 221], ['(10°, 10°)\nApple', null]);
})
.then(done, done.fail);
});
it('should generate hover label with custom styling', function(done) {
Plotly.restyle(gd, {
'hoverlabel.bgcolor': 'red',
'hoverlabel.bordercolor': [['blue', 'black', 'green']]
})
.then(function() {
check([381, 221], ['(10°, 10°)\nA', null], {
bgcolor: 'rgb(255, 0, 0)',
bordercolor: 'rgb(0, 0, 255)',
fontColor: 'rgb(0, 0, 255)',
fontSize: 13,
fontFamily: 'Arial'
});
})
.then(done, done.fail);
});
it('should generate hover label with arrayOk \'hoverinfo\' settings', function(done) {
Plotly.restyle(gd, 'hoverinfo', [['lon', null, 'lat+name']]).then(function() {
check([381, 221], ['lon: 10°', null]);
})
.then(done, done.fail);
});
describe('should preserve lon/lat formatting hovetemplate equivalence', function() {
var pos = [381, 221];
var exp = ['(10.00088°, 10.00012°)\nA'];
it('- base case (truncate z decimals)', function(done) {
Plotly.restyle(gd, {
lon: [[10.0001221321]],
lat: [[10.00087683]]
})
.then(function() { check(pos, exp); })
.then(done, done.fail);
});
it('- hovertemplate case (same lat/lon truncation)', function(done) {
Plotly.restyle(gd, {
lon: [[10.0001221321]],
lat: [[10.00087683]],
hovertemplate: '(%{lat}°, %{lon}°)<br>%{text}<extra></extra>'
})
.then(function() { check(pos, exp); })
.then(done, done.fail);
});
});
it('should include *properties* from input custom geojson', function(done) {
var fig = Lib.extendDeep({}, require('@mocks/geo_custom-geojson.json'));
fig.data = [fig.data[3]];
fig.data[0].geo = 'geo';
fig.data[0].marker = {size: 40};
fig.data[0].hovertemplate = '%{properties.name}<extra>LOOK</extra>';
fig.layout.geo.projection = {scale: 30};
Plotly.react(gd, fig)
.then(function() {
check([275, 255], ['New York', 'LOOK']);
})
.then(done, done.fail);
});
});
describe('scattergeo drawing', function() {
var gd;
beforeEach(function() {
gd = createGraphDiv();
});
afterEach(destroyGraphDiv);
it('should not throw an error with bad locations', function(done) {
spyOn(loggers, 'log');
Plotly.newPlot(gd, [{
locations: ['canada', 0, null, '', 'utopia'],
locationmode: 'country names',
type: 'scattergeo'
}])
.then(function() {
// only utopia logs - others are silently ignored
expect(loggers.log).toHaveBeenCalledTimes(1);
})
.then(done, done.fail);
});
it('preserves order after hide/show', function(done) {
function getIndices() {
var out = [];
d3SelectAll('.scattergeo').each(function(d) { out.push(d[0].trace.index); });
return out;
}
Plotly.newPlot(gd, [
{type: 'scattergeo', lon: [10, 20], lat: [10, 20]},
{type: 'scattergeo', lon: [10, 20], lat: [10, 20]}
])
.then(function() {
expect(getIndices()).toEqual([0, 1]);
return Plotly.restyle(gd, 'visible', false, [0]);
})
.then(function() {
expect(getIndices()).toEqual([1]);
return Plotly.restyle(gd, 'visible', true, [0]);
})
.then(function() {
expect(getIndices()).toEqual([0, 1]);
})
.then(done, done.fail);
});
});
describe('Test scattergeo texttemplate:', function() {
checkTextTemplate([{
'type': 'scattergeo',
'mode': 'markers+text',
'lon': [-73.57, -79.24, -123.06],
'lat': [45.5, 43.4, 49.13],
'text': ['Montreal', 'Toronto', 'Vancouver']
}], '.scattergeo text', [
['%{text}: %{lon}, %{lat}', ['Montreal: −73.57, 45.5', 'Toronto: −79.24, 43.4', 'Vancouver: −123.06, 49.13']]
]);
checkTextTemplate([{
'type': 'scattergeo',
'mode': 'markers+text',
'locations': ['Canada'],
'locationmode': 'country names'
}], '.scattergeo text', [
['%{location}', ['Canada']]
]);
});