score:29

Accepted answer

the fix

assign a function into usercallback when creating the config object. this gets called when creating the tick marks. you can find documentation at chart.js at the bottom of scales documentation

example fiddle with the fix

yaxes: [{
    ticks: {
        beginatzero: true,
        stepsize: 500000,

        // return an empty string to draw the tick line but hide the tick label
        // return `null` or `undefined` to hide the tick line entirely
        usercallback: function(value, index, values) {
            // convert the number to a string and splite the string every 3 charaters from the end
            value = value.tostring();
            value = value.split(/(?=(?:...)*$)/);

            // convert the array to a string and format the output
            value = value.join('.');
            return '€' + value;
        }
    }
}]

score:3

instead a new function you can use a native javascript numberformat

        yaxes: [{
      ticks: {
        beginatzero: true,
        callback: function(value, index, values) {
                return '€ ' + intl.numberformat().format((value/1000)) + 'k';
            }
      }
    }]
        //2000000 => € 2,000k

score:6

for those using version: 2.5.0, here is an enhancement. with this, you can also format the amounts in tooltips of the chart, not only the 'ticks' in the 'yaxes'

...
options: {
    scales: {
        yaxes: [{
            ticks: {
                beginatzero:true,
                callback: function(value, index, values) {
                    return '$ ' + number_format(value);
                }
            }
        }]
    },
    tooltips: {
        callbacks: {
            label: function(tooltipitem, chart){
                var datasetlabel = chart.datasets[tooltipitem.datasetindex].label || '';
                return datasetlabel + ': $ ' + number_format(tooltipitem.ylabel, 2);
            }
        }
    }
}

here is the number_format() function what i am using:

function number_format(number, decimals, dec_point, thousands_sep) {
// *     example: number_format(1234.56, 2, ',', ' ');
// *     return: '1 234,56'
    number = (number + '').replace(',', '').replace(' ', '');
    var n = !isfinite(+number) ? 0 : +number,
            prec = !isfinite(+decimals) ? 0 : math.abs(decimals),
            sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
            dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
            s = '',
            tofixedfix = function (n, prec) {
                var k = math.pow(10, prec);
                return '' + math.round(n * k) / k;
            };
    // fix for ie parsefloat(0.55).tofixed(0) = 0;
    s = (prec ? tofixedfix(n, prec) : '' + math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\b(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}

Related Query

More Query from same tag