-
-
Notifications
You must be signed in to change notification settings - Fork 288
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chart: Improve yAxis label formatting for small & large numbers (#2206)
- Loading branch information
1 parent
17e8c7b
commit 4df3ab3
Showing
5 changed files
with
38 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const yAxisFormatter = value => { | ||
if (Number.isNaN(value)) { | ||
return value; | ||
} | ||
// Handle zero as a special case | ||
if (value === 0) { | ||
return '0'; | ||
} | ||
|
||
// Round to 4 significant digits | ||
const significantDigits = 4; | ||
const roundedValue = parseFloat(value.toPrecision(significantDigits)); | ||
|
||
// For very small or very large numbers, use scientific notation | ||
if (Math.abs(roundedValue) < 0.001 || Math.abs(roundedValue) >= 1e6) { | ||
return roundedValue.toExponential(2); // Scientific notation with 2 decimals | ||
} | ||
|
||
// For normal values, format with up to 2 decimal places | ||
return roundedValue.toLocaleString(undefined, { | ||
minimumFractionDigits: 0, | ||
maximumFractionDigits: 2 | ||
}); | ||
}; | ||
|
||
export { yAxisFormatter }; |