fix(axes): format tick labels correctly for small numbers in exponential notation#7768
Open
Hasnaathussain wants to merge 1 commit intoplotly:masterfrom
Open
fix(axes): format tick labels correctly for small numbers in exponential notation#7768Hasnaathussain wants to merge 1 commit intoplotly:masterfrom
Hasnaathussain wants to merge 1 commit intoplotly:masterfrom
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes tick label formatting in numFormat for very small numbers where String(v) returns exponential notation, preventing invalid/truncated tick strings.
Changes:
- Detects exponential notation from
String(v)and splits mantissa/exponent before applying rounding/truncation. - Re-attaches the exponent after formatting the mantissa to avoid slicing into the exponent.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+2220
to
+2226
| var adjustedTickRound = tickRound + exponentVal; | ||
| if(dp) { | ||
| if(adjustedTickRound < 0) { | ||
| mantissa = mantissa.slice(0, dp - 1); | ||
| } else { | ||
| mantissa = mantissa.slice(0, dp + adjustedTickRound).replace(/\.?0+$/, ''); | ||
| } |
| } else { | ||
| mantissa = mantissa.slice(0, dp + adjustedTickRound).replace(/\.?0+$/, ''); | ||
| } | ||
| } |
Comment on lines
+2213
to
+2233
| var vStr = String(v); | ||
| var ep = vStr.indexOf('e'); | ||
| if(ep >= 0) { | ||
| var mantissa = vStr.slice(0, ep); | ||
| var exponentStr = vStr.slice(ep); | ||
| var dp = mantissa.indexOf('.') + 1; | ||
| var exponentVal = parseInt(exponentStr.slice(1), 10); | ||
| var adjustedTickRound = tickRound + exponentVal; | ||
| if(dp) { | ||
| if(adjustedTickRound < 0) { | ||
| mantissa = mantissa.slice(0, dp - 1); | ||
| } else { | ||
| mantissa = mantissa.slice(0, dp + adjustedTickRound).replace(/\.?0+$/, ''); | ||
| } | ||
| } | ||
| v = mantissa + exponentStr; | ||
| } else { | ||
| v = vStr; | ||
| var dp = v.indexOf('.') + 1; | ||
| if(dp) v = v.slice(0, dp + tickRound).replace(/\.?0+$/, ''); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this PR does
This PR fixes a bug in
src/plots/cartesian/axes.jswhere formatting very small numbers could result in invalid tick strings (e.g."-9.381779999999999e-") whenString(v)produces exponential notation but thenumFormatlogic slices the string indiscriminately.Fix details
When exponential notation is present, the logic now separates the mantissa from the exponent, applies precision formatting to the mantissa, and re-attaches the exponent string, avoiding string truncation that removes the exponential component.
Tested against locally failing reproductions and verified using
test-jasmineto ensure no existing suites are broken.