Skip to content

Commit a859464

Browse files
committed
add 'hoverinfo' to scattergeo traces
1 parent b760fe8 commit a859464

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

src/traces/scattergeo/attributes.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,23 @@ module.exports = {
5656
'If a single string, the same string appears over',
5757
'all the data points.',
5858
'If an array of string, the items are mapped in order to the',
59-
'this trace\'s (lon,lat) or `locations` coordinates.'
59+
'this trace\'s (lon,lat) or `locations` coordinates.',
60+
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
61+
'these elements will be seen in the hover labels.'
6062
].join(' ')
6163
}),
64+
hovertext: extendFlat({}, scatterAttrs.hovertext, {
65+
description: [
66+
'Sets hover text elements associated with each (lon,lat) pair',
67+
'or item in `locations`.',
68+
'If a single string, the same string appears over',
69+
'all the data points.',
70+
'If an array of string, the items are mapped in order to the',
71+
'this trace\'s (lon,lat) or `locations` coordinates.',
72+
'To be seen, trace `hoverinfo` must contain a *text* flag.'
73+
].join(' ')
74+
}),
75+
6276
textfont: scatterAttrs.textfont,
6377
textposition: scatterAttrs.textposition,
6478

src/traces/scattergeo/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3232
}
3333

3434
coerce('text');
35+
coerce('hovertext');
3536
coerce('mode');
3637

3738
if(subTypes.hasLines(traceOut)) {

src/traces/scattergeo/hover.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,13 @@ function getExtraText(trace, pt, axis) {
101101
else if(hasLat) text.push('lat: ' + format(pt.lonlat[1]));
102102

103103
if(hasText) {
104-
var tx = pt.tx || trace.text;
104+
var tx;
105+
106+
if(pt.htx) tx = pt.htx;
107+
else if(trace.hovertext) tx = trace.hovertext;
108+
else if(pt.tx) tx = pt.tx;
109+
else if(trace.text) tx = trace.text;
110+
105111
if(!Array.isArray(tx)) text.push(tx);
106112
}
107113

test/jasmine/tests/scattergeo_test.js

+69
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
var Plotly = require('@lib');
12
var Plots = require('@src/plots/plots');
23
var Lib = require('@src/lib');
34
var BADNUM = require('@src/constants/numerical').BADNUM;
45

56
var ScatterGeo = require('@src/traces/scattergeo');
67

8+
var d3 = require('d3');
9+
var createGraphDiv = require('../assets/create_graph_div');
10+
var destroyGraphDiv = require('../assets/destroy_graph_div');
11+
var customMatchers = require('../assets/custom_matchers');
12+
var mouseEvent = require('../assets/mouse_event');
713

814
describe('Test scattergeo defaults', function() {
915
var traceIn,
@@ -221,3 +227,66 @@ describe('Test scattergeo calc', function() {
221227
]);
222228
});
223229
});
230+
231+
describe('Test scattergeo hover', function() {
232+
var gd;
233+
234+
// we can't mock ScatterGeo.hoverPoints
235+
// because geo hover relies on mouse event
236+
// to set the c2p conversion functions
237+
238+
beforeAll(function() {
239+
jasmine.addMatchers(customMatchers);
240+
});
241+
242+
beforeEach(function(done) {
243+
gd = createGraphDiv();
244+
245+
Plotly.plot(gd, [{
246+
type: 'scattergeo',
247+
lon: [10, 20, 30],
248+
lat: [10, 20, 30],
249+
text: ['A', 'B', 'C']
250+
}])
251+
.then(done);
252+
});
253+
254+
afterEach(destroyGraphDiv);
255+
256+
function assertHoverLabels(expected) {
257+
var hoverText = d3.selectAll('g.hovertext').selectAll('tspan');
258+
259+
hoverText.each(function(_, i) {
260+
expect(this.innerHTML).toEqual(expected[i]);
261+
});
262+
}
263+
264+
it('should generate hover label info (base case)', function() {
265+
mouseEvent('mousemove', 381, 221);
266+
assertHoverLabels(['(10°, 10°)', 'A']);
267+
});
268+
269+
it('should generate hover label info (\'text\' single value case)', function(done) {
270+
Plotly.restyle(gd, 'text', 'text').then(function() {
271+
mouseEvent('mousemove', 381, 221);
272+
assertHoverLabels(['(10°, 10°)', 'text']);
273+
})
274+
.then(done);
275+
});
276+
277+
it('should generate hover label info (\'hovertext\' single value case)', function(done) {
278+
Plotly.restyle(gd, 'hovertext', 'hovertext').then(function() {
279+
mouseEvent('mousemove', 381, 221);
280+
assertHoverLabels(['(10°, 10°)', 'hovertext']);
281+
})
282+
.then(done);
283+
});
284+
285+
it('should generate hover label info (\'hovertext\' array case)', function(done) {
286+
Plotly.restyle(gd, 'hovertext', ['Apple', 'Banana', 'Orange']).then(function() {
287+
mouseEvent('mousemove', 381, 221);
288+
assertHoverLabels(['(10°, 10°)', 'Apple']);
289+
})
290+
.then(done);
291+
});
292+
});

0 commit comments

Comments
 (0)