Skip to content

Commit

Permalink
Check for isNaN before building number formatter options
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
defunctzombie committed Apr 19, 2023
1 parent b34e273 commit 38e1c18
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/core/core.ticks.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,15 @@ 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);
Expand Down

0 comments on commit 38e1c18

Please sign in to comment.