Skip to content

Commit 437cb2b

Browse files
authored
Merge pull request #7768 from Hasnaathussain/fix-tick-formatting-exponential
fix(axes): format tick labels correctly for small numbers in exponential notation
2 parents a677f28 + 278319f commit 437cb2b

3 files changed

Lines changed: 53 additions & 11 deletions

File tree

draftlogs/7768_fix.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Format tick labels correctly for small numbers in exponential notation [[#7768](https://github.com/plotly/plotly.js/pull/7768)], with thanks to @Hasnaathussain for the contribution!

src/plots/cartesian/axes.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,9 +2214,6 @@ function numFormat(v, ax, fmtoverride, hover) {
22142214

22152215
if(tickformat) return ax._numFormat(tickformat)(v).replace(/-/g, MINUS_SIGN);
22162216

2217-
// 'epsilon' - rounding increment
2218-
var e = Math.pow(10, -tickRound) / 2;
2219-
22202217
// exponentFormat codes:
22212218
// 'e' (1.2e+6, default)
22222219
// 'E' (1.2E+6)
@@ -2231,27 +2228,27 @@ function numFormat(v, ax, fmtoverride, hover) {
22312228
// take the sign out, put it back manually at the end
22322229
// - makes cases easier
22332230
v = Math.abs(v);
2231+
2232+
// 'epsilon' - rounding increment
2233+
const e = Math.pow(10, -tickRound) / 2;
22342234
if(v < e) {
22352235
// 0 is just 0, but may get exponent if it's the last tick
22362236
v = '0';
22372237
isNeg = false;
22382238
} else {
2239-
v += e;
22402239
// take out a common exponent, if any
22412240
if(exponent) {
22422241
v *= Math.pow(10, -exponent);
22432242
tickRound += exponent;
22442243
}
22452244
// round the mantissa
2246-
if(tickRound === 0) v = String(Math.floor(v));
2247-
else if(tickRound < 0) {
2245+
if(tickRound === 0) {
22482246
v = String(Math.round(v));
2249-
v = v.slice(0, Math.max(0, v.length + tickRound));
2250-
for(var i = tickRound; i < 0; i++) v += '0';
2247+
} else if(tickRound < 0) {
2248+
const roundingMagnitude = Math.pow(10, -tickRound);
2249+
v = String(Math.round(v / roundingMagnitude) * roundingMagnitude);
22512250
} else {
2252-
v = String(v);
2253-
var dp = v.indexOf('.') + 1;
2254-
if(dp) v = v.slice(0, dp + tickRound).replace(/\.?0+$/, '');
2251+
v = v.toFixed(Math.min(20, tickRound)).replace(/\.?0+$/, '');
22552252
}
22562253
// insert appropriate decimal point and thousands separator
22572254
v = Lib.numSeparate(v, ax._separators, separatethousands);

test/jasmine/tests/axes_test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3668,6 +3668,50 @@ describe('Test axes', function() {
36683668
});
36693669
});
36703670

3671+
it('formats tick labels correctly for small numbers in exponential notation', function() {
3672+
var textOut = mockCalc({
3673+
type: 'linear',
3674+
tickmode: 'linear',
3675+
exponentformat: 'none',
3676+
showexponent: 'all',
3677+
tick0: 0,
3678+
dtick: 1e-9,
3679+
range: [8.5e-9, 11.5e-9]
3680+
});
3681+
3682+
expect(textOut).toEqual([
3683+
'0.000000009', '0.00000001', '0.000000011'
3684+
]);
3685+
3686+
textOut = mockCalc({
3687+
type: 'linear',
3688+
tickmode: 'linear',
3689+
exponentformat: 'none',
3690+
showexponent: 'all',
3691+
tick0: 0,
3692+
dtick: 1e-15,
3693+
range: [8.5e-15, 11.5e-15]
3694+
});
3695+
3696+
expect(textOut).toEqual([
3697+
'0.000000000000009', '0.00000000000001', '0.000000000000011'
3698+
]);
3699+
3700+
textOut = mockCalc({
3701+
type: 'linear',
3702+
tickmode: 'linear',
3703+
exponentformat: 'e',
3704+
showexponent: 'all',
3705+
tick0: 0,
3706+
dtick: 1e-15,
3707+
range: [8.5e-15, 11.5e-15]
3708+
});
3709+
3710+
expect(textOut).toEqual([
3711+
'0.9e\u221214', '1e\u221214', '1.1e\u221214'
3712+
]);
3713+
});
3714+
36713715
it('provides a new date suffix whenever the suffix changes', function() {
36723716
var ax = {
36733717
type: 'date',

0 commit comments

Comments
 (0)