Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix hover label exponents #1932

Merged
merged 3 commits into from
Aug 5, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/plots/cartesian/axes.js
Original file line number Diff line number Diff line change
@@ -1207,7 +1207,11 @@ axes.tickText = function(ax, x, hover) {
return showAttr !== 'all' && x !== first_or_last;
}

hideexp = ax.exponentformat !== 'none' && isHidden(ax.showexponent) ? 'hide' : '';
if(hover) {
hideexp = 'never';
} else {
hideexp = ax.exponentformat !== 'none' && isHidden(ax.showexponent) ? 'hide' : '';
}

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

function formatLinear(ax, out, hover, extraPrecision, hideexp) {
// don't add an exponent to zero if we're showing all exponents
// so the only reason you'd show an exponent on zero is if it's the
// ONLY tick to get an exponent (first or last)
if(ax.showexponent === 'all' && Math.abs(out.x / ax.dtick) < 1e-6) {
if(hideexp === 'never') {
// If this is a hover label, then we must *never* hide the exponent
// for the sake of display, which could give the wrong value by
// potentially many orders of magnitude. If hideexp was 'never', then
// it's now succeeded by preventing the other condition from automating
// this choice. Thus we can unset it so that the axis formatting takes
// precedence.
hideexp = '';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the 📚 @rreusser

} else if(ax.showexponent === 'all' && Math.abs(out.x / ax.dtick) < 1e-6) {
// don't add an exponent to zero if we're showing all exponents
// so the only reason you'd show an exponent on zero is if it's the
// ONLY tick to get an exponent (first or last)
hideexp = 'hide';
}
out.text = numFormat(out.x, ax, hideexp, extraPrecision);
17 changes: 17 additions & 0 deletions test/jasmine/tests/axes_test.js
Original file line number Diff line number Diff line change
@@ -2363,6 +2363,23 @@ describe('Test axes', function() {

expect(mockCalc(ax).length).toBe(10001);
});

it('never hides the exponent when in hover mode', function() {
var ax = {
type: 'linear',
tickmode: 'linear',
tick0: 0,
dtick: 2e20,
range: [0, 1.0732484076433121e21],
_length: 270
};

mockCalc(ax);

expect(mockHoverText(ax, 1e-21)).toBe('1×10<sup>−21</sup>');
expect(mockHoverText(ax, 1)).toBe('1');
expect(mockHoverText(ax, 1e21)).toBe('1×10<sup>21</sup>');
});
});

describe('autoBin', function() {