Chart.js 2.0 Formatting Y Axis with Currency and Thousands Separator

前端 未结 3 1970
悲哀的现实
悲哀的现实 2021-02-14 02:40

I am having problems with formatting my chart axis and I am not able to find an example for the updated version 2.0.

How can I (for example) make 2000000 to €2.000.000?<

3条回答
  •  天命终不由人
    2021-02-14 03:02

    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;
            }
        }
    }]
    

提交回复
热议问题