-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add "SI extended" formatting rule for the tick exponents #7249
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
base: master
Are you sure you want to change the base?
Changes from all commits
8d6861f
c0ec891
d78cfc1
8f361a2
15fa0ba
13921b3
6806e55
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -1557,7 +1557,8 @@ function autoTickRound(ax) { | |||||||||||||||||||
var rangeexp = Math.floor(Math.log(maxend) / Math.LN10 + 0.01); | ||||||||||||||||||||
var minexponent = ax.minexponent === undefined ? 3 : ax.minexponent; | ||||||||||||||||||||
if(Math.abs(rangeexp) > minexponent) { | ||||||||||||||||||||
if(isSIFormat(ax.exponentformat) && !beyondSI(rangeexp)) { | ||||||||||||||||||||
if((isSIFormat(ax.exponentformat) && ax.exponentformat !== 'SI extended' && !beyondSI(rangeexp)) || | ||||||||||||||||||||
(isSIFormat(ax.exponentformat) && ax.exponentformat === 'SI extended' && !beyondSIExtended(rangeexp))) { | ||||||||||||||||||||
ax._tickexponent = 3 * Math.round((rangeexp - 1) / 3); | ||||||||||||||||||||
} else ax._tickexponent = rangeexp; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
@@ -1914,7 +1915,8 @@ function formatLog(ax, out, hover, extraPrecision, hideexp) { | |||||||||||||||||||
var p = +parts[1]; | ||||||||||||||||||||
var absP = Math.abs(p); | ||||||||||||||||||||
var exponentFormat = ax.exponentformat; | ||||||||||||||||||||
if(exponentFormat === 'power' || (isSIFormat(exponentFormat) && beyondSI(p))) { | ||||||||||||||||||||
if(exponentFormat === 'power' || (isSIFormat(exponentFormat) && exponentFormat !== 'SI extended' && beyondSI(p)) || | ||||||||||||||||||||
(isSIFormat(exponentFormat) && exponentFormat === 'SI extended' && beyondSIExtended(p))) { | ||||||||||||||||||||
out.text = parts[0]; | ||||||||||||||||||||
if(absP > 0) out.text += 'x10'; | ||||||||||||||||||||
if(out.text === '1x10') out.text = '10'; | ||||||||||||||||||||
|
@@ -2063,8 +2065,11 @@ function num2frac(num) { | |||||||||||||||||||
// also automatically switch to sci. notation | ||||||||||||||||||||
var SIPREFIXES = ['f', 'p', 'n', 'μ', 'm', '', 'k', 'M', 'G', 'T']; | ||||||||||||||||||||
|
||||||||||||||||||||
// extending SI prefixes | ||||||||||||||||||||
var SIPREFIXES_EXTENDED = ['q', 'r', 'y', 'z', 'a', 'f', 'p', 'n', 'μ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y', 'R', 'Q']; | ||||||||||||||||||||
|
||||||||||||||||||||
function isSIFormat(exponentFormat) { | ||||||||||||||||||||
return exponentFormat === 'SI' || exponentFormat === 'B'; | ||||||||||||||||||||
return exponentFormat === 'SI' || exponentFormat === 'SI extended' || exponentFormat === 'B'; | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
2071
to
2073
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could turn this into a one-liner:
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
// are we beyond the range of common SI prefixes? | ||||||||||||||||||||
|
@@ -2078,6 +2083,10 @@ function beyondSI(exponent) { | |||||||||||||||||||
return exponent > 14 || exponent < -15; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
function beyondSIExtended(exponent) { | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a comment here for clarity, to match the above comment
Suggested change
|
||||||||||||||||||||
return exponent > 32 || exponent < -30; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
function numFormat(v, ax, fmtoverride, hover) { | ||||||||||||||||||||
var isNeg = v < 0; | ||||||||||||||||||||
// max number of digits past decimal point to show | ||||||||||||||||||||
|
@@ -2153,7 +2162,8 @@ function numFormat(v, ax, fmtoverride, hover) { | |||||||||||||||||||
|
||||||||||||||||||||
// add exponent | ||||||||||||||||||||
if(exponent && exponentFormat !== 'hide') { | ||||||||||||||||||||
if(isSIFormat(exponentFormat) && beyondSI(exponent)) exponentFormat = 'power'; | ||||||||||||||||||||
if((isSIFormat(exponentFormat) && exponentFormat !== 'SI extended' && beyondSI(exponent)) || | ||||||||||||||||||||
(isSIFormat(exponentFormat) && exponentFormat === 'SI extended' && beyondSIExtended(exponent))) exponentFormat = 'power'; | ||||||||||||||||||||
Comment on lines
+2165
to
+2166
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This Maybe encapsulate this logic inside a function? function shouldSwitchSIToPowerFormat(exponent, exponentFormat) {
if (!isSIFormat(exponentFormat)) return false;
if (exponentFormat === 'SI extended' && beyondSIExtended(exponent)) return true;
if (exponentFormat !== 'SI extended' && beyondSI(exponent)) return true;
return false;
} Then these lines here just become if(shouldSwitchSIToPowerFormat(exponent, exponentFormat)) exponentFormat = 'power'; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hard agree with Emily on this one. |
||||||||||||||||||||
|
||||||||||||||||||||
var signedExponent; | ||||||||||||||||||||
if(exponent < 0) signedExponent = MINUS_SIGN + -exponent; | ||||||||||||||||||||
|
@@ -2167,7 +2177,11 @@ function numFormat(v, ax, fmtoverride, hover) { | |||||||||||||||||||
} else if(exponentFormat === 'B' && exponent === 9) { | ||||||||||||||||||||
v += 'B'; | ||||||||||||||||||||
} else if(isSIFormat(exponentFormat)) { | ||||||||||||||||||||
v += SIPREFIXES[exponent / 3 + 5]; | ||||||||||||||||||||
if(exponentFormat !== 'SI extended') { | ||||||||||||||||||||
v += SIPREFIXES[exponent / 3 + 5]; | ||||||||||||||||||||
} else if(exponentFormat === 'SI extended') { | ||||||||||||||||||||
v += SIPREFIXES_EXTENDED[exponent / 3 + 10]; | ||||||||||||||||||||
} | ||||||||||||||||||||
Comment on lines
+2180
to
+2184
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could switch to a ternary since there are only two options for this operation:
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it make sense to use
SIPREFIXES
in the new array? Then you wouldn't have to update the new array ifSIPREFIXES
ever changes. Normally I'd put the destructured array at the start of the enclosing array, but it appears that the index is important in this case.