diff --git a/src/core/core.ticks.js b/src/core/core.ticks.js index c0e34b11eda..ce8905b3a5f 100644 --- a/src/core/core.ticks.js +++ b/src/core/core.ticks.js @@ -26,11 +26,15 @@ const formatters = { * @return {string} string representation of the tickValue parameter */ numeric(tickValue, index, ticks) { + const locale = this.chart.options.locale; if (tickValue === 0) { - return '0'; // never show decimal places for 0 + return formatNumber(0, locale, { + minimumFractionDigits: 0, + maximumFractionDigits: 0, + ...this.options.ticks.format + }); } - const locale = this.chart.options.locale; let notation; let delta = tickValue; // This is used when there are less than 2 ticks as the tick interval. diff --git a/test/specs/core.ticks.tests.js b/test/specs/core.ticks.tests.js index 01db0cdce30..7b93c9ee854 100644 --- a/test/specs/core.ticks.tests.js +++ b/test/specs/core.ticks.tests.js @@ -100,6 +100,33 @@ describe('Test tick generators', function() { }); describe('formatters.numeric', function() { + it('should apply number format options to zero ticks', function() { + for (const test of [ + {format: {style: 'percent'}, expected: '0%'}, + {format: {style: 'currency', currency: 'USD'}, expected: '$0'}, + {format: {style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 2}, expected: '$0.00'} + ]) { + const chart = window.acquireChart({ + type: 'line', + data: {datasets: [{data: [0, 1]}]}, + options: { + locale: 'en-US', + scales: { + y: {min: 0, max: 1, ticks: {format: test.format}} + } + } + }); + expect(chart.scales.y.ticks[0].label).toBe(test.expected); + } + }); + + it('should keep zero ticks free of automatic precision and notation', function() { + const scale = {chart: {options: {locale: 'en-US'}}, options: {ticks: {}}}; + for (const ticks of [[], [{value: 0}], [{value: 0}, {value: 0.01}], [{value: 0}, {value: 1e16}]]) { + expect(Chart.Ticks.formatters.numeric.call(scale, 0, 0, ticks)).toBe('0'); + } + }); + it('should not fail on empty or 1 item array', function() { const scale = {chart: {options: {locale: 'en'}}, options: {ticks: {format: {}}}}; expect(Chart.Ticks.formatters.numeric.apply(scale, [1, 0, []])).toEqual('1');