Skip to content

Commit 4160081

Browse files
committed
support 'e' and 'E' exponentformat on log axes
1 parent 18b12f2 commit 4160081

File tree

2 files changed

+46
-16
lines changed

2 files changed

+46
-16
lines changed

src/plots/cartesian/axes.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,9 +1079,10 @@ function formatDate(ax, out, hover, extraPrecision) {
10791079
}
10801080

10811081
function formatLog(ax, out, hover, extraPrecision, hideexp) {
1082-
var dtick = ax.dtick,
1083-
x = out.x,
1084-
tickformat = ax.tickformat;
1082+
var dtick = ax.dtick;
1083+
var x = out.x;
1084+
var tickformat = ax.tickformat;
1085+
var dtChar0 = typeof dtick === 'string' && dtick.charAt(0);
10851086

10861087
if(hideexp === 'never') {
10871088
// If this is a hover label, then we must *never* hide the exponent
@@ -1093,30 +1094,33 @@ function formatLog(ax, out, hover, extraPrecision, hideexp) {
10931094
hideexp = '';
10941095
}
10951096

1096-
if(extraPrecision && ((typeof dtick !== 'string') || dtick.charAt(0) !== 'L')) dtick = 'L3';
1097+
if(extraPrecision && (dtChar0 !== 'L')) dtick = 'L3';
10971098

1098-
if(tickformat || (typeof dtick === 'string' && dtick.charAt(0) === 'L')) {
1099+
if(tickformat || (dtChar0 === 'L')) {
10991100
out.text = numFormat(Math.pow(10, x), ax, hideexp, extraPrecision);
11001101
}
1101-
else if(isNumeric(dtick) || ((dtick.charAt(0) === 'D') && (Lib.mod(x + 0.01, 1) < 0.1))) {
1102+
else if(isNumeric(dtick) || ((dtChar0 === 'D') && (Lib.mod(x + 0.01, 1) < 0.1))) {
11021103
var p = Math.round(x);
1103-
if(['e', 'E', 'power'].indexOf(ax.exponentformat) !== -1 ||
1104-
(isSIFormat(ax.exponentformat) && beyondSI(p))) {
1104+
var absP = Math.abs(p);
1105+
var exponentFormat = ax.exponentformat;
1106+
if(exponentFormat === 'power' || (isSIFormat(exponentFormat) && beyondSI(p))) {
11051107
if(p === 0) out.text = 1;
11061108
else if(p === 1) out.text = '10';
1107-
else if(p > 1) out.text = '10<sup>' + p + '</sup>';
1108-
else out.text = '10<sup>' + MINUS_SIGN + -p + '</sup>';
1109+
out.text = '10<sup>' + (p > 1 ? '' : MINUS_SIGN) + absP + '</sup>';
11091110

11101111
out.fontSize *= 1.25;
11111112
}
1113+
else if((exponentFormat === 'e' || exponentFormat === 'E') && absP > 2) {
1114+
out.text = '1' + exponentFormat + (p > 0 ? '+' : MINUS_SIGN) + absP;
1115+
}
11121116
else {
11131117
out.text = numFormat(Math.pow(10, x), ax, '', 'fakehover');
11141118
if(dtick === 'D1' && ax._id.charAt(0) === 'y') {
11151119
out.dy -= out.fontSize / 6;
11161120
}
11171121
}
11181122
}
1119-
else if(dtick.charAt(0) === 'D') {
1123+
else if(dtChar0 === 'D') {
11201124
out.text = String(Math.round(Math.pow(10, Lib.mod(x, 1))));
11211125
out.fontSize *= 0.75;
11221126
}
@@ -1332,11 +1336,8 @@ function numFormat(v, ax, fmtoverride, hover) {
13321336
else if(exponentFormat !== 'power') signedExponent = '+' + exponent;
13331337
else signedExponent = String(exponent);
13341338

1335-
if(exponentFormat === 'e') {
1336-
v += 'e' + signedExponent;
1337-
}
1338-
else if(exponentFormat === 'E') {
1339-
v += 'E' + signedExponent;
1339+
if(exponentFormat === 'e' || exponentFormat === 'E') {
1340+
v += exponentFormat + signedExponent;
13401341
}
13411342
else if(exponentFormat === 'power') {
13421343
v += '×10<sup>' + signedExponent + '</sup>';

test/jasmine/tests/axes_test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1941,6 +1941,35 @@ describe('Test axes', function() {
19411941
]);
19421942
});
19431943

1944+
it('supports e/E format on log axes', function() {
1945+
['e', 'E'].forEach(function(e) {
1946+
var textOut = mockCalc({
1947+
type: 'log',
1948+
tickmode: 'linear',
1949+
exponentformat: e,
1950+
showexponent: 'all',
1951+
tick0: 0,
1952+
dtick: 'D2',
1953+
range: [-4.1, 4.1]
1954+
});
1955+
1956+
var oep = '1' + e + '+';
1957+
var oem = '1' + e + '\u2212';
1958+
1959+
expect(textOut).toEqual([
1960+
oem + '4', '2', '5',
1961+
oem + '3', '2', '5',
1962+
'0.01', '2', '5',
1963+
'0.1', '2', '5',
1964+
'1', '2', '5',
1965+
'10', '2', '5',
1966+
'100', '2', '5',
1967+
oep + '3', '2', '5',
1968+
oep + '4'
1969+
]);
1970+
});
1971+
});
1972+
19441973
it('provides a new date suffix whenever the suffix changes', function() {
19451974
var ax = {
19461975
type: 'date',

0 commit comments

Comments
 (0)