Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bdec7be

Browse files
committed
exponent bases fixed for linear
1 parent a81c92f commit bdec7be

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

src/plots/cartesian/axes.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,12 @@ axes.handleTickDefaults = function(containerIn, containerOut, coerce, axType, op
253253
var expBase = coerce('exponentbase'),
254254
expFmtDflt = coerce('exponentformat');
255255

256-
if (expBase !== 2 && expBase !== 10) expFmtDflt = 'power';
257-
if (expBase === 2 && expFmtDflt !== 'SI') expFmtDflt = 'power';
256+
if (+expBase !== 2 && +expBase !== 10) expFmtDflt = 'power';
257+
if (+expBase === 2 && expFmtDflt !== 'SI') expFmtDflt = 'power';
258+
if (expBase === 'e'){
259+
containerOut.exponentbase = Math.E;
260+
containerOut.type = 'log';
261+
}
258262

259263
containerOut.exponentformat = expFmtDflt;
260264
}
@@ -1377,9 +1381,9 @@ axes.autoTicks = function(ax, roughDTick){
13771381
var nt = 1.5 * Math.abs((ax.range[1] - ax.range[0]) / roughDTick);
13781382

13791383
// ticks on a linear scale, labeled fully
1380-
roughDTick = Math.abs(Math.pow(10, ax.range[1]) -
1381-
Math.pow(10, ax.range[0])) / nt;
1382-
base = Math.pow(10, Math.floor(Math.log(roughDTick) / Math.LN10));
1384+
roughDTick = Math.abs(Math.pow(ax.exponentbase, ax.range[1]) -
1385+
Math.pow(ax.exponentbase, ax.range[0])) / nt;
1386+
base = Math.pow(ax.exponentbase, Math.floor(Math.log(roughDTick) / Math.log(ax.exponentbase)));
13831387
ax.dtick = 'L' + roundDTick(roughDTick, base, roundBase10);
13841388
}
13851389
else {
@@ -1440,7 +1444,7 @@ function autoTickRound(ax) {
14401444
ax._tickround = 2 - Math.floor(Math.log(dtick) / Math.log(ax.exponentbase) + 0.01);
14411445

14421446
if(ax.type === 'log') {
1443-
maxend = Math.pow(10, Math.max(ax.range[0], ax.range[1]));
1447+
maxend = Math.pow(ax.exponentbase, Math.max(ax.range[0], ax.range[1]));
14441448
}
14451449
else maxend = Math.max(Math.abs(ax.range[0]), Math.abs(ax.range[1]));
14461450

@@ -1765,7 +1769,7 @@ function numFormat(v, ax, fmtoverride, hover) {
17651769
tickRound = ax._tickround,
17661770
exponentFormat = fmtoverride || ax.exponentformat || 'B',
17671771
exponent = ax._tickexponent,
1768-
base = ax.exponentbase || 10,
1772+
base = +ax.exponentbase || 10,
17691773
isBase10 = (base === 10),
17701774
tickformat = ax.tickformat;
17711775

@@ -1791,7 +1795,8 @@ function numFormat(v, ax, fmtoverride, hover) {
17911795
if(tickformat) return d3.format(tickformat)(v).replace(/-/g,'\u2212');
17921796

17931797
// 'epsilon' - rounding increment
1794-
var e = Math.pow(base, -tickRound) / 2;
1798+
var e = isBase10 ? Math.pow(base, -tickRound) / 2 : 0;
1799+
// var e = 0;
17951800

17961801
// exponentFormat codes:
17971802
// 'e' (1.2e+6, default)
@@ -1807,6 +1812,7 @@ function numFormat(v, ax, fmtoverride, hover) {
18071812
// take the sign out, put it back manually at the end
18081813
// - makes cases easier
18091814
v = Math.abs(v);
1815+
18101816
if(v < e) {
18111817
// 0 is just 0, but may get exponent if it's the last tick
18121818
v = '0';
@@ -1815,16 +1821,11 @@ function numFormat(v, ax, fmtoverride, hover) {
18151821
v += e;
18161822
// take out a common exponent, if any
18171823
// Special case for base 2 to follow "SI"
1818-
if(exponent && base === 2 && exponentFormat === 'SI') {
1819-
v = v / 1024;
1820-
1821-
// To make 1024 -> 1k
1822-
exponent -= 9;
1823-
tickRound += exponent;
1824-
} else {
1824+
if(exponent){
18251825
v *= Math.pow(base, -exponent);
18261826
tickRound += exponent;
18271827
}
1828+
18281829
// round the mantissa
18291830
if(tickRound === 0) v = String(Math.floor(v));
18301831
else if(tickRound < 0) {
@@ -1857,6 +1858,13 @@ function numFormat(v, ax, fmtoverride, hover) {
18571858
} else if(exponentFormat === 'B' && exponent === 9) {
18581859
v += 'B';
18591860
} else if(exponentFormat === 'SI' || exponentFormat === 'B') {
1861+
if(base === 2 && exponent >= 10){
1862+
v = v * Math.pow(base, (exponent - 10));
1863+
exponent -= 9;
1864+
} else if(base === 2 && exponent < 10){
1865+
v = v * Math.pow(base, (exponent));
1866+
exponent = 0;
1867+
}
18601868
v += SIPREFIXES[exponent / 3 + 5];
18611869
} else {
18621870
v += '&times;' + base + '<sup>' + signedExponent + '</sup>';

src/plots/cartesian/layout_attributes.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,8 @@ module.exports = {
290290
].join(' ')
291291
},
292292
exponentbase: {
293-
valType: 'number',
294-
min: 2,
293+
valType: 'enumerated',
294+
values: [2, '2', 'e', 8, '8', 10, '10', 16, '16'],
295295
dflt: 10,
296296
role: 'style',
297297
description: [

0 commit comments

Comments
 (0)