Skip to content

Commit 3991552

Browse files
Merge pull request #3436 from plotly/map_hovertemplates
add hovertemplate to choropleth, scattergeo and scattermapbox
2 parents 60ffe4f + 2dedf00 commit 3991552

File tree

12 files changed

+59
-3
lines changed

12 files changed

+59
-3
lines changed

src/traces/choropleth/attributes.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
'use strict';
1010

11+
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
1112
var scatterGeoAttrs = require('../scattergeo/attributes');
1213
var colorscaleAttrs = require('../../components/colorscale/attributes');
1314
var colorbarAttrs = require('../../components/colorbar/attributes');
@@ -72,7 +73,8 @@ module.exports = extendFlat({
7273
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
7374
editType: 'calc',
7475
flags: ['location', 'z', 'text', 'name']
75-
})
76+
}),
77+
hovertemplate: hovertemplateAttrs(),
7678
},
7779

7880
colorscaleAttrs('', {

src/traces/choropleth/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3030
coerce('locationmode');
3131

3232
coerce('text');
33+
coerce('hovertemplate');
3334

3435
coerce('marker.line.color');
3536
coerce('marker.line.width');

src/traces/choropleth/hover.js

+5
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,18 @@ module.exports = function hoverPoints(pointData, xval, yval) {
4747
pointData.index = pt.index;
4848
pointData.location = pt.loc;
4949
pointData.z = pt.z;
50+
pointData.hovertemplate = pt.hovertemplate;
5051

5152
makeHoverInfo(pointData, trace, pt, geo.mockAxis);
5253

5354
return [pointData];
5455
};
5556

5657
function makeHoverInfo(pointData, trace, pt, axis) {
58+
if(trace.hovertemplate) {
59+
return;
60+
}
61+
5762
var hoverinfo = pt.hi || trace.hoverinfo;
5863

5964
var parts = (hoverinfo === 'all') ?

src/traces/scattergeo/attributes.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
'use strict';
1010

11+
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
1112
var scatterAttrs = require('../scatter/attributes');
1213
var plotAttrs = require('../../plots/attributes');
1314
var colorAttributes = require('../../components/colorscale/attributes');
@@ -122,5 +123,6 @@ module.exports = overrideAll({
122123

123124
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
124125
flags: ['lon', 'lat', 'location', 'text', 'name']
125-
})
126+
}),
127+
hovertemplate: hovertemplateAttrs(),
126128
}, 'calc', 'nested');

src/traces/scattergeo/defaults.js

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

3434
coerce('text');
3535
coerce('hovertext');
36+
coerce('hovertemplate');
3637
coerce('mode');
3738

3839
if(subTypes.hasLines(traceOut)) {

src/traces/scattergeo/hover.js

+5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ module.exports = function hoverPoints(pointData, xval, yval) {
6666

6767
pointData.color = getTraceColor(trace, di);
6868
pointData.extraText = getExtraText(trace, di, geo.mockAxis, cd[0].t.labels);
69+
pointData.hovertemplate = trace.hovertemplate;
6970

7071
return [pointData];
7172
};
7273

7374
function getExtraText(trace, pt, axis, labels) {
75+
if(trace.hovertemplate) {
76+
return;
77+
}
78+
7479
var hoverinfo = pt.hi || trace.hoverinfo;
7580

7681
var parts = hoverinfo === 'all' ?

src/traces/scattermapbox/attributes.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
'use strict';
1010

11+
var hovertemplateAttrs = require('../../components/fx/hovertemplate_attributes');
1112
var scatterGeoAttrs = require('../scattergeo/attributes');
1213
var scatterAttrs = require('../scatter/attributes');
1314
var mapboxAttrs = require('../../plots/mapbox/layout_attributes');
@@ -115,5 +116,6 @@ module.exports = overrideAll({
115116

116117
hoverinfo: extendFlat({}, plotAttrs.hoverinfo, {
117118
flags: ['lon', 'lat', 'text', 'name']
118-
})
119+
}),
120+
hovertemplate: hovertemplateAttrs(),
119121
}, 'calc', 'nested');

src/traces/scattermapbox/defaults.js

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module.exports = function supplyDefaults(traceIn, traceOut, defaultColor, layout
3030

3131
coerce('text');
3232
coerce('hovertext');
33+
coerce('hovertemplate');
3334
coerce('mode');
3435

3536
if(subTypes.hasLines(traceOut)) {

src/traces/scattermapbox/hover.js

+5
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,16 @@ module.exports = function hoverPoints(pointData, xval, yval) {
6666

6767
pointData.color = getTraceColor(trace, di);
6868
pointData.extraText = getExtraText(trace, di, cd[0].t.labels);
69+
pointData.hovertemplate = trace.hovertemplate;
6970

7071
return [pointData];
7172
};
7273

7374
function getExtraText(trace, di, labels) {
75+
if(trace.hovertemplate) {
76+
return;
77+
}
78+
7479
var hoverinfo = di.hi || trace.hoverinfo;
7580
var parts = hoverinfo.split('+');
7681
var isAll = parts.indexOf('all') !== -1;

test/jasmine/tests/choropleth_test.js

+12
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ describe('Test choropleth hover:', function() {
114114
.then(done);
115115
});
116116

117+
it('should use the hovertemplate', function(done) {
118+
var fig = Lib.extendDeep({}, require('@mocks/geo_first.json'));
119+
fig.data[1].hovertemplate = 'tpl %{z}<extra>x</extra>';
120+
121+
run(
122+
[400, 160],
123+
fig,
124+
['tpl 10', 'x']
125+
)
126+
.then(done);
127+
});
128+
117129
it('should generate hover label info (\'text\' single value case)', function(done) {
118130
var fig = Lib.extendDeep({}, require('@mocks/geo_first.json'));
119131
fig.data[1].text = 'tExT';

test/jasmine/tests/scattergeo_test.js

+8
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,14 @@ describe('Test scattergeo hover', function() {
291291
.then(done);
292292
});
293293

294+
it('should use the hovertemplate', function(done) {
295+
Plotly.restyle(gd, 'hovertemplate', 'tpl %{lat}<extra>x</extra>').then(function() {
296+
check([381, 221], ['tpl 10', 'x']);
297+
})
298+
.catch(failTest)
299+
.then(done);
300+
});
301+
294302
it('should generate hover label info (\'text\' single value case)', function(done) {
295303
Plotly.restyle(gd, 'text', 'text').then(function() {
296304
check([381, 221], ['(10°, 10°)\ntext', null]);

test/jasmine/tests/scattermapbox_test.js

+12
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,18 @@ describe('@noCI scattermapbox hover', function() {
776776
.catch(failTest)
777777
.then(done);
778778
});
779+
780+
it('should pass along hovertemplate', function(done) {
781+
Plotly.restyle(gd, 'hovertemplate', 'tpl').then(function() {
782+
var xval = 11;
783+
var yval = 11;
784+
785+
var out = hoverPoints(getPointData(gd), xval, yval)[0];
786+
787+
expect(out.hovertemplate).toEqual('tpl');
788+
done();
789+
});
790+
});
779791
});
780792

781793
describe('@noCI Test plotly events on a scattermapbox plot:', function() {

0 commit comments

Comments
 (0)