From 86c6c5b0d0eac15b74cefdfa2042acad256754e4 Mon Sep 17 00:00:00 2001 From: Ricky Reusser Date: Fri, 4 Aug 2017 16:26:22 -0700 Subject: [PATCH 1/3] Fix hover label exponents --- src/plots/cartesian/axes.js | 22 +++++++++++++++++----- test/jasmine/tests/axes_test.js | 17 +++++++++++++++++ 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/plots/cartesian/axes.js b/src/plots/cartesian/axes.js index c2009959aa3..3234fe83442 100644 --- a/src/plots/cartesian/axes.js +++ b/src/plots/cartesian/axes.js @@ -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 = '' + } 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); diff --git a/test/jasmine/tests/axes_test.js b/test/jasmine/tests/axes_test.js index 0367649a9e1..b73141a02e6 100644 --- a/test/jasmine/tests/axes_test.js +++ b/test/jasmine/tests/axes_test.js @@ -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('1e\u221221'); + expect(mockHoverText(ax, 1)).toBe('1'); + expect(mockHoverText(ax, 1e21)).toBe('1e+21'); + }); }); describe('autoBin', function() { From 87d1dcb161fb1df5aec7d037a834b2f14b257d05 Mon Sep 17 00:00:00 2001 From: Ricky Reusser Date: Fri, 4 Aug 2017 16:44:44 -0700 Subject: [PATCH 2/3] Fix lint errors --- src/plots/cartesian/axes.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/plots/cartesian/axes.js b/src/plots/cartesian/axes.js index 3234fe83442..5a2767c0d55 100644 --- a/src/plots/cartesian/axes.js +++ b/src/plots/cartesian/axes.js @@ -1207,8 +1207,8 @@ axes.tickText = function(ax, x, hover) { return showAttr !== 'all' && x !== first_or_last; } - if (hover) { - hideexp = 'never' + if(hover) { + hideexp = 'never'; } else { hideexp = ax.exponentformat !== 'none' && isHidden(ax.showexponent) ? 'hide' : ''; } @@ -1350,14 +1350,14 @@ function formatCategory(ax, out) { } function formatLinear(ax, out, hover, extraPrecision, hideexp) { - if (hideexp === 'never') { + 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 = '' + hideexp = ''; } 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 From 08cfd4ac9304ccb8f0f7cb59df4c3e142fc79761 Mon Sep 17 00:00:00 2001 From: Ricky Reusser Date: Fri, 4 Aug 2017 21:28:53 -0700 Subject: [PATCH 3/3] Fix axis test for fancy exponent format --- test/jasmine/tests/axes_test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/jasmine/tests/axes_test.js b/test/jasmine/tests/axes_test.js index b73141a02e6..d5594d4c5d6 100644 --- a/test/jasmine/tests/axes_test.js +++ b/test/jasmine/tests/axes_test.js @@ -2376,9 +2376,9 @@ describe('Test axes', function() { mockCalc(ax); - expect(mockHoverText(ax, 1e-21)).toBe('1e\u221221'); + expect(mockHoverText(ax, 1e-21)).toBe('1×10−21'); expect(mockHoverText(ax, 1)).toBe('1'); - expect(mockHoverText(ax, 1e21)).toBe('1e+21'); + expect(mockHoverText(ax, 1e21)).toBe('1×1021'); }); });