Chart.js Add Commas to Tooltip and Y-Axis
For your yAxes ticks options, this will add commas at the thousands marks:
ticks: {
beginAtZero:true,
userCallback: function(value, index, values) {
value = value.toString();
value = value.split(/(?=(?:...)*$)/);
value = value.join(',');
return value;
}
}
Similar function can be added in a tooltip callback.
Full example in this FIDDLE
I see a lot of overly complicated answers for this.
This functionality is already built into javascript with .toLocaleString()
, we don't have to reinvent to wheel! This accounts for decimals as well.
yAxes: [
{
ticks: {
beginAtZero: true,
userCallback: function(value, index, values) {
return value.toLocaleString(); // this is all we need
}
}
}
]