Caroline D Portgas
Caroline D Portgas

Reputation: 13

Chart.js plugin datalabels - formatter- Add space between thousands

Hello I am trying to put a space between thousands in my datalabels. I did something like that.. but that doesn't work. I can't find any solution on the internet. Here is the documentation I don't understand : https://chartjs-plugin-datalabels.netlify.app/guide/formatting.html#custom-labels

plugins: {
            datalabels: {
                anchor: 'end',
                align: 'right',
                color: '#EA7B30',
                formatter: function(value, context) {
                    return ',' + Math.floor(value*1);
                },              
            }
        }

Upvotes: 0

Views: 2051

Answers (1)

Juha Kangas
Juha Kangas

Reputation: 788

You can use value.toLocaleString() which puts comma between thousands and then replaceAll to replace all commas with space.

        plugins: {
            datalabels: {
                anchor: 'end',
                align: 'right',
                color: '#EA7B30',
                formatter: function(value, context) {
                    return value.toLocaleString().replaceAll(',',' ')
                },              
            }
        }

Upvotes: 3

Related Questions