Skip to content

Commit 6a670ff

Browse files
committed
fix #3961 - fill in zLabel during choropleth hover
... so that hover templates with `%{z}` can render the z value using the same formatting as by default.
1 parent 0690162 commit 6a670ff

File tree

3 files changed

+37
-12
lines changed

3 files changed

+37
-12
lines changed

src/components/fx/hover.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,11 +896,15 @@ function createHoverText(hoverData, opts, gd) {
896896
if(d.zLabel !== undefined) {
897897
if(d.xLabel !== undefined) text += 'x: ' + d.xLabel + '<br>';
898898
if(d.yLabel !== undefined) text += 'y: ' + d.yLabel + '<br>';
899-
text += (text ? 'z: ' : '') + d.zLabel;
899+
if(d.trace.type !== 'choropleth') {
900+
text += (text ? 'z: ' : '') + d.zLabel;
901+
}
900902
} else if(showCommonLabel && d[hovermode + 'Label'] === t0) {
901903
text = d[(hovermode === 'x' ? 'y' : 'x') + 'Label'] || '';
902904
} else if(d.xLabel === undefined) {
903-
if(d.yLabel !== undefined && d.trace.type !== 'scattercarpet') text = d.yLabel;
905+
if(d.yLabel !== undefined && d.trace.type !== 'scattercarpet') {
906+
text = d.yLabel;
907+
}
904908
} else if(d.yLabel === undefined) text = d.xLabel;
905909
else text = '(' + d.xLabel + ', ' + d.yLabel + ')';
906910

src/traces/choropleth/hover.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
* LICENSE file in the root directory of this source tree.
77
*/
88

9-
109
'use strict';
1110

1211
var Axes = require('../../plots/cartesian/axes');
@@ -47,17 +46,16 @@ module.exports = function hoverPoints(pointData, xval, yval) {
4746
pointData.index = pt.index;
4847
pointData.location = pt.loc;
4948
pointData.z = pt.z;
49+
pointData.zLabel = Axes.tickText(geo.mockAxis, geo.mockAxis.c2l(pt.z), 'hover').text;
5050
pointData.hovertemplate = pt.hovertemplate;
5151

5252
makeHoverInfo(pointData, trace, pt, geo.mockAxis);
5353

5454
return [pointData];
5555
};
5656

57-
function makeHoverInfo(pointData, trace, pt, axis) {
58-
if(trace.hovertemplate) {
59-
return;
60-
}
57+
function makeHoverInfo(pointData, trace, pt) {
58+
if(trace.hovertemplate) return;
6159

6260
var hoverinfo = pt.hi || trace.hoverinfo;
6361

@@ -73,18 +71,16 @@ function makeHoverInfo(pointData, trace, pt, axis) {
7371

7472
var text = [];
7573

76-
function formatter(val) {
77-
return Axes.tickText(axis, axis.c2l(val), 'hover').text;
78-
}
79-
8074
if(hasIdAsNameLabel) {
8175
pointData.nameOverride = pt.loc;
8276
} else {
8377
if(hasName) pointData.nameOverride = trace.name;
8478
if(hasLocation) text.push(pt.loc);
8579
}
8680

87-
if(hasZ) text.push(formatter(pt.z));
81+
if(hasZ) {
82+
text.push(pointData.zLabel);
83+
}
8884
if(hasText) {
8985
fillText(pt, trace, text);
9086
}

test/jasmine/tests/choropleth_test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,31 @@ describe('Test choropleth hover:', function() {
200200
)
201201
.then(done);
202202
});
203+
204+
describe('should preserve z formatting hovetemplate equivalence', function() {
205+
var base = function() {
206+
return {
207+
data: [{
208+
type: 'choropleth',
209+
locations: ['RUS'],
210+
z: [10.02132132143214321]
211+
}]
212+
};
213+
};
214+
215+
var pos = [400, 160];
216+
var exp = ['10.02132', 'RUS'];
217+
218+
it('- base case (truncate z decimals)', function(done) {
219+
run(pos, base(), exp).then(done);
220+
});
221+
222+
it('- hovertemplate case (same z truncation)', function(done) {
223+
var fig = base();
224+
fig.hovertemplate = '%{z}<extra>%{location}</extra>';
225+
run(pos, fig, exp).then(done);
226+
});
227+
});
203228
});
204229

205230
describe('choropleth drawing', function() {

0 commit comments

Comments
 (0)