]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Check for isNaN before building number formatter options (#11238)
authorRoman Shtylman <shtylman@gmail.com>
Wed, 19 Apr 2023 22:22:25 +0000 (15:22 -0700)
committerGitHub <noreply@github.com>
Wed, 19 Apr 2023 22:22:25 +0000 (18:22 -0400)
* Check for isNaN before building number formatter options

When datasets have values approaching Number.MAX_VALUE, the tick calculations might result in infinity and eventually NaN. Passing NaN for minimumFractionDigits or maximumFractionDigits will make the number formatter throw. Instead we check for isNaN and use a fallback value so the formatter does not throw.

* Update src/core/core.ticks.js

Co-authored-by: Jacco van den Berg <jaccoberg2281@gmail.com>
---------

Co-authored-by: Jacco van den Berg <jaccoberg2281@gmail.com>
src/core/core.ticks.js

index eac44444af5e2f9d3ee1ee411f649a2f843171f9..c0e34b11eda12af9cda5ff12e3d6960a05f10b51 100644 (file)
@@ -45,7 +45,13 @@ const formatters = {
     }
 
     const logDelta = log10(Math.abs(delta));
-    const numDecimal = Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0); // toFixed has a max of 20 decimal places
+
+    // When datasets have values approaching Number.MAX_VALUE, the tick calculations might result in
+    // infinity and eventually NaN. Passing NaN for minimumFractionDigits or maximumFractionDigits
+    // will make the number formatter throw. So instead we check for isNaN and use a fallback value.
+    //
+    // toFixed has a max of 20 decimal places
+    const numDecimal = isNaN(logDelta) ? 1 : Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0);
 
     const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal};
     Object.assign(options, this.options.ticks.format);