Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c303560

Browse files
authored
Merge pull request plotly#1283 from plotly/fix-map-extratext
Fix scattermapbox and scattergeo handling of '' text items
2 parents 1ef50b9 + 9df444d commit c303560

File tree

4 files changed

+71
-8
lines changed

4 files changed

+71
-8
lines changed

src/traces/scattergeo/hover.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@ function getExtraText(trace, pt, axis) {
102102
else if(hasLon) text.push('lon: ' + format(pt.lonlat[0]));
103103
else if(hasLat) text.push('lat: ' + format(pt.lonlat[1]));
104104

105-
if(hasText) text.push(pt.tx || trace.text);
105+
if(hasText) {
106+
var tx = pt.tx || trace.text;
107+
if(!Array.isArray(tx)) text.push(tx);
108+
}
106109

107110
return text.join('<br>');
108111
}

src/traces/scattermapbox/hover.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ function getExtraText(trace, di) {
8686
else if(hasLat) text.push('lat: ' + format(lonlat[1]));
8787

8888
if(isAll || hoverinfo.indexOf('text') !== -1) {
89-
text.push(di.tx || trace.text);
89+
var tx = di.tx || trace.text;
90+
if(!Array.isArray(tx)) text.push(tx);
9091
}
9192

9293
return text.join('<br>');

test/jasmine/tests/geo_test.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -420,25 +420,54 @@ describe('Test geo interactions', function() {
420420
});
421421

422422
describe('scattergeo hover labels', function() {
423-
beforeEach(function() {
424-
mouseEventScatterGeo('mousemove');
425-
});
426-
427423
it('should show one hover text group', function() {
424+
mouseEventScatterGeo('mousemove');
428425
expect(d3.selectAll('g.hovertext').size()).toEqual(1);
429426
});
430427

431428
it('should show longitude and latitude values', function() {
432-
var node = d3.selectAll('g.hovertext').selectAll('tspan')[0][0];
429+
mouseEventScatterGeo('mousemove');
433430

431+
var node = d3.selectAll('g.hovertext').selectAll('tspan')[0][0];
434432
expect(node.innerHTML).toEqual('(0°, 0°)');
435433
});
436434

437435
it('should show the trace name', function() {
438-
var node = d3.selectAll('g.hovertext').selectAll('text')[0][0];
436+
mouseEventScatterGeo('mousemove');
439437

438+
var node = d3.selectAll('g.hovertext').selectAll('text')[0][0];
440439
expect(node.innerHTML).toEqual('trace 0');
441440
});
441+
442+
it('should show *text* (case 1)', function(done) {
443+
Plotly.restyle(gd, 'text', [['A', 'B']]).then(function() {
444+
mouseEventScatterGeo('mousemove');
445+
446+
var node = d3.selectAll('g.hovertext').selectAll('tspan')[0][1];
447+
expect(node.innerHTML).toEqual('A');
448+
})
449+
.then(done);
450+
});
451+
452+
it('should show *text* (case 2)', function(done) {
453+
Plotly.restyle(gd, 'text', [[null, 'B']]).then(function() {
454+
mouseEventScatterGeo('mousemove');
455+
456+
var node = d3.selectAll('g.hovertext').selectAll('tspan')[0][1];
457+
expect(node).toBeUndefined();
458+
})
459+
.then(done);
460+
});
461+
462+
it('should show *text* (case 3)', function(done) {
463+
Plotly.restyle(gd, 'text', [['', 'B']]).then(function() {
464+
mouseEventScatterGeo('mousemove');
465+
466+
var node = d3.selectAll('g.hovertext').selectAll('tspan')[0][1];
467+
expect(node).toBeUndefined();
468+
})
469+
.then(done);
470+
});
442471
});
443472

444473
describe('scattergeo hover events', function() {

test/jasmine/tests/scattermapbox_test.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,36 @@ describe('scattermapbox hover', function() {
494494
expect(out.color).toEqual('#1f77b4');
495495
});
496496

497+
it('should skip over blank and non-string text items', function(done) {
498+
var xval = 11,
499+
yval = 11,
500+
out;
501+
502+
Plotly.restyle(gd, 'text', [['', 'B', 'C']]).then(function() {
503+
out = hoverPoints(getPointData(gd), xval, yval)[0];
504+
expect(out.extraText).toEqual('(10°, 10°)');
505+
506+
return Plotly.restyle(gd, 'text', [[null, 'B', 'C']]);
507+
})
508+
.then(function() {
509+
out = hoverPoints(getPointData(gd), xval, yval)[0];
510+
expect(out.extraText).toEqual('(10°, 10°)');
511+
512+
return Plotly.restyle(gd, 'text', [[false, 'B', 'C']]);
513+
})
514+
.then(function() {
515+
out = hoverPoints(getPointData(gd), xval, yval)[0];
516+
expect(out.extraText).toEqual('(10°, 10°)');
517+
518+
return Plotly.restyle(gd, 'text', [['A', 'B', 'C']]);
519+
})
520+
.then(function() {
521+
out = hoverPoints(getPointData(gd), xval, yval)[0];
522+
expect(out.extraText).toEqual('(10°, 10°)<br>A');
523+
})
524+
.then(done);
525+
});
526+
497527
it('should generate hover label info (positive winding case)', function() {
498528
var xval = 11 + 720,
499529
yval = 11;

0 commit comments

Comments
 (0)