Skip to content

Commit 86c6c5b

Browse files
committed
Fix hover label exponents
1 parent 6b54152 commit 86c6c5b

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

src/plots/cartesian/axes.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,11 @@ axes.tickText = function(ax, x, hover) {
12071207
return showAttr !== 'all' && x !== first_or_last;
12081208
}
12091209

1210-
hideexp = ax.exponentformat !== 'none' && isHidden(ax.showexponent) ? 'hide' : '';
1210+
if (hover) {
1211+
hideexp = 'never'
1212+
} else {
1213+
hideexp = ax.exponentformat !== 'none' && isHidden(ax.showexponent) ? 'hide' : '';
1214+
}
12111215

12121216
if(ax.type === 'date') formatDate(ax, out, hover, extraPrecision);
12131217
else if(ax.type === 'log') formatLog(ax, out, hover, extraPrecision, hideexp);
@@ -1346,10 +1350,18 @@ function formatCategory(ax, out) {
13461350
}
13471351

13481352
function formatLinear(ax, out, hover, extraPrecision, hideexp) {
1349-
// don't add an exponent to zero if we're showing all exponents
1350-
// so the only reason you'd show an exponent on zero is if it's the
1351-
// ONLY tick to get an exponent (first or last)
1352-
if(ax.showexponent === 'all' && Math.abs(out.x / ax.dtick) < 1e-6) {
1353+
if (hideexp === 'never') {
1354+
// If this is a hover label, then we must *never* hide the exponent
1355+
// for the sake of display, which could give the wrong value by
1356+
// potentially many orders of magnitude. If hideexp was 'never', then
1357+
// it's now succeeded by preventing the other condition from automating
1358+
// this choice. Thus we can unset it so that the axis formatting takes
1359+
// precedence.
1360+
hideexp = ''
1361+
} else if(ax.showexponent === 'all' && Math.abs(out.x / ax.dtick) < 1e-6) {
1362+
// don't add an exponent to zero if we're showing all exponents
1363+
// so the only reason you'd show an exponent on zero is if it's the
1364+
// ONLY tick to get an exponent (first or last)
13531365
hideexp = 'hide';
13541366
}
13551367
out.text = numFormat(out.x, ax, hideexp, extraPrecision);

test/jasmine/tests/axes_test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,6 +2363,23 @@ describe('Test axes', function() {
23632363

23642364
expect(mockCalc(ax).length).toBe(10001);
23652365
});
2366+
2367+
it('never hides the exponent when in hover mode', function() {
2368+
var ax = {
2369+
type: 'linear',
2370+
tickmode: 'linear',
2371+
tick0: 0,
2372+
dtick: 2e20,
2373+
range: [0, 1.0732484076433121e21],
2374+
_length: 270
2375+
};
2376+
2377+
mockCalc(ax);
2378+
2379+
expect(mockHoverText(ax, 1e-21)).toBe('1e\u221221');
2380+
expect(mockHoverText(ax, 1)).toBe('1');
2381+
expect(mockHoverText(ax, 1e21)).toBe('1e+21');
2382+
});
23662383
});
23672384

23682385
describe('autoBin', function() {

0 commit comments

Comments
 (0)