Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/core/core.ticks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
27 changes: 27 additions & 0 deletions test/specs/core.ticks.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Loading