Skip to content

Commit aa6eadf

Browse files
committed
add 'hoverinfo' to scattermapbox traces
1 parent 55a51f8 commit aa6eadf

File tree

5 files changed

+41
-4
lines changed

5 files changed

+41
-4
lines changed

src/traces/scattermapbox/attributes.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ module.exports = {
4343
'If a single string, the same string appears over',
4444
'all the data points.',
4545
'If an array of string, the items are mapped in order to the',
46-
'this trace\'s (lon,lat) coordinates.'
46+
'this trace\'s (lon,lat) coordinates.',
47+
'If trace `hoverinfo` contains a *text* flag and *hovertext* is not set,',
48+
'these elements will be seen in the hover labels.'
49+
].join(' ')
50+
}),
51+
hovertext: extendFlat({}, scatterAttrs.hovertext, {
52+
description: [
53+
'Sets hover text elements associated with each (lon,lat) pair',
54+
'If a single string, the same string appears over',
55+
'all the data points.',
56+
'If an array of string, the items are mapped in order to the',
57+
'this trace\'s (lon,lat) coordinates.',
58+
'To be seen, trace `hoverinfo` must contain a *text* flag.'
4759
].join(' ')
4860
}),
4961

src/traces/scattermapbox/calc.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ module.exports = function calc(gd, trace) {
2727
hasColorArray = (hasMarkers && Array.isArray(marker.color)),
2828
hasSizeArray = (hasMarkers && Array.isArray(marker.size)),
2929
hasSymbolArray = (hasMarkers && Array.isArray(marker.symbol)),
30-
hasTextArray = Array.isArray(trace.text);
30+
hasTextArray = Array.isArray(trace.text),
31+
hasHoverTextArray = Array.isArray(trace.hovertext);
3132

3233
calcMarkerColorscale(trace);
3334

@@ -94,6 +95,11 @@ module.exports = function calc(gd, trace) {
9495
calcPt.tx = (typeof tx === 'string') ? tx : '';
9596
}
9697

98+
if(hasHoverTextArray) {
99+
var htx = trace.hovertext[i];
100+
calcPt.htx = (typeof htx === 'string') ? htx : '';
101+
}
102+
97103
calcTrace.push(calcPt);
98104
}
99105

src/traces/scattermapbox/defaults.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
4242
}
4343

4444
coerce('text');
45+
coerce('hovertext');
4546
coerce('mode');
4647

4748
if(subTypes.hasLines(traceOut)) {

src/traces/scattermapbox/hover.js

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

8888
if(isAll || hoverinfo.indexOf('text') !== -1) {
89-
var tx = di.tx || trace.text;
89+
var tx;
90+
91+
if(di.htx) tx = di.htx;
92+
else if(trace.hovertext) tx = trace.hovertext;
93+
else if(di.tx) tx = di.tx;
94+
else if(trace.text) tx = trace.text;
95+
9096
if(!Array.isArray(tx)) text.push(tx);
9197
}
9298

test/jasmine/tests/scattermapbox_test.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ describe('@noCI scattermapbox hover', function() {
588588
});
589589
});
590590

591-
it('should generate hover label info (hoverinfo: \'text\' case)', function(done) {
591+
it('should generate hover label info (hoverinfo: \'text\' + \'text\' array case)', function(done) {
592592
Plotly.restyle(gd, 'hoverinfo', 'text').then(function() {
593593
var xval = 11,
594594
yval = 11;
@@ -599,4 +599,16 @@ describe('@noCI scattermapbox hover', function() {
599599
done();
600600
});
601601
});
602+
603+
it('should generate hover label info (hoverinfo: \'text\' + \'hovertext\' array case)', function(done) {
604+
Plotly.restyle(gd, 'hovertext', ['Apple', 'Banana', 'Orange']).then(function() {
605+
var xval = 11,
606+
yval = 11;
607+
608+
var out = hoverPoints(getPointData(gd), xval, yval)[0];
609+
610+
expect(out.extraText).toEqual('Apple');
611+
done();
612+
});
613+
});
602614
});

0 commit comments

Comments
 (0)