* Formatter for numeric ticks * @method Chart.Ticks.formatters.numeric * @param tickValue {number} the value to be formatted * @param index {number} the position of the tickValue parameter in the ticks array * @param ticks {object[]} the list of ticks being converted * @return {string
(tickValue, index, ticks)
| 47 | * @return {string} string representation of the tickValue parameter |
| 48 | */ |
| 49 | numeric(tickValue, index, ticks) { |
| 50 | const locale = this.chart.options.locale; |
| 51 | if (tickValue === 0) { |
| 52 | return formatNumber(0, locale, applyNumberFormat({ |
| 53 | minimumFractionDigits: 0, |
| 54 | maximumFractionDigits: 0 |
| 55 | }, this.options.ticks.format)); |
| 56 | } |
| 57 | |
| 58 | let notation; |
| 59 | let delta = tickValue; // This is used when there are less than 2 ticks as the tick interval. |
| 60 | |
| 61 | if (ticks.length > 1) { |
| 62 | // all ticks are small or there huge numbers; use scientific notation |
| 63 | const maxTick = Math.max(Math.abs(ticks[0].value), Math.abs(ticks[ticks.length - 1].value)); |
| 64 | if (maxTick < 1e-4 || maxTick > 1e+15) { |
| 65 | notation = 'scientific'; |
| 66 | } |
| 67 | |
| 68 | delta = calculateDelta(tickValue, ticks); |
| 69 | } |
| 70 | |
| 71 | const logDelta = log10(Math.abs(delta)); |
| 72 | |
| 73 | // When datasets have values approaching Number.MAX_VALUE, the tick calculations might result in |
| 74 | // infinity and eventually NaN. Passing NaN for minimumFractionDigits or maximumFractionDigits |
| 75 | // will make the number formatter throw. So instead we check for isNaN and use a fallback value. |
| 76 | // |
| 77 | // toFixed has a max of 20 decimal places |
| 78 | const numDecimal = isNaN(logDelta) ? 1 : Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0); |
| 79 | |
| 80 | const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal}; |
| 81 | |
| 82 | return formatNumber(tickValue, locale, applyNumberFormat(options, this.options.ticks.format)); |
| 83 | }, |
| 84 | |
| 85 | |
| 86 | /** |
nothing calls this directly
no test coverage detected