* 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)
| 26 | * @return {string} string representation of the tickValue parameter |
| 27 | */ |
| 28 | numeric(tickValue, index, ticks) { |
| 29 | if (tickValue === 0) { |
| 30 | return '0'; // never show decimal places for 0 |
| 31 | } |
| 32 | |
| 33 | const locale = this.chart.options.locale; |
| 34 | let notation; |
| 35 | let delta = tickValue; // This is used when there are less than 2 ticks as the tick interval. |
| 36 | |
| 37 | if (ticks.length > 1) { |
| 38 | // all ticks are small or there huge numbers; use scientific notation |
| 39 | const maxTick = Math.max(Math.abs(ticks[0].value), Math.abs(ticks[ticks.length - 1].value)); |
| 40 | if (maxTick < 1e-4 || maxTick > 1e+15) { |
| 41 | notation = 'scientific'; |
| 42 | } |
| 43 | |
| 44 | delta = calculateDelta(tickValue, ticks); |
| 45 | } |
| 46 | |
| 47 | const logDelta = log10(Math.abs(delta)); |
| 48 | |
| 49 | // When datasets have values approaching Number.MAX_VALUE, the tick calculations might result in |
| 50 | // infinity and eventually NaN. Passing NaN for minimumFractionDigits or maximumFractionDigits |
| 51 | // will make the number formatter throw. So instead we check for isNaN and use a fallback value. |
| 52 | // |
| 53 | // toFixed has a max of 20 decimal places |
| 54 | const numDecimal = isNaN(logDelta) ? 1 : Math.max(Math.min(-1 * Math.floor(logDelta), 20), 0); |
| 55 | |
| 56 | const options = {notation, minimumFractionDigits: numDecimal, maximumFractionDigits: numDecimal}; |
| 57 | Object.assign(options, this.options.ticks.format); |
| 58 | |
| 59 | return formatNumber(tickValue, locale, options); |
| 60 | }, |
| 61 | |
| 62 | |
| 63 | /** |
nothing calls this directly
no test coverage detected