Reputation: 1167
I found that you can format the x or y axis of Chart.JS to currency by a quick JavaScript callback function
callback: function(value, index, values) {
if (parseInt(value) >= 1000) {
return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else {
return '$' + value;
}
}
}
but this does not seem to be the case for the data:
In my instance this is a JavaScript 1D Array that holds numeric values, and I want to add a dollar sign, and comma where applicable. How can this be added with Charts.jS
EDIT
This is how I am populating my JSON array - and I want to format the data in the data: values
var barChartData = {
labels: labelsarr,
datasets: [{
label: 'Amount',
backgroundColor: 'rgba(200, 200, 200, 0.75)',
borderColor: 'rgb(255, 99, 132)',
borderWidth: 1,
data: values
}]
};
And the JSON array is in this format ["AAAA",100,10000,2310,24420,30,50000,400000,70000,700,823200,923200,1111]
I want to format starting at index[1]
to show $ and currency. How can this be achieved?
2nd Edit Time
I try to add title and format both data points and left axis as currency with below syntax, but this no longer displays anything on my page.
var ctx = document.getElementById('canvas').getContext('2d');
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: labelsarr,
datasets: [{
label: 'Test',
data: values,
backgroundColor: 'rgba(0, 119, 204, 0.8)',
}]
},
options: {
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
callback: function(value, index, values) {
if (parseInt(value) >= 1000) {
return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else {
return '$' + value;
}
}
}
}]
},
legend: {
position: 'top',
},
title: {
display: true,
text: '<?php echo $name ?> Test'
}
tooltips: {
callbacks: {
label: function(t, d) {
var xLabel = d.datasets[t.datasetIndex].label;
var yLabel = t.yLabel >= 1000 ?
'$' + t.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") :
'$' + t.yLabel;
return xLabel + ': ' + yLabel;
}
}
}
}
});
Upvotes: 2
Views: 11566
Reputation: 32869
This can be achieved using the following tool-tips label callback function ...
tooltips: {
callbacks: {
label: function(t, d) {
var xLabel = d.datasets[t.datasetIndex].label;
var yLabel = t.yLabel >= 1000 ? '$' + t.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : '$' + t.yLabel;
return xLabel + ': ' + yLabel;
}
}
}
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ
var labelsarr = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'];
var values = ["AAAA", 100, 10000, 2310, 24420, 30, 50000, 400000, 70000, 700, 823200, 923200, 1111];
var ctx = document.getElementById('canvas').getContext('2d');
var chart = new Chart(ctx, {
type: 'bar',
data: {
labels: labelsarr,
datasets: [{
label: 'Amount',
data: values,
backgroundColor: 'rgba(0, 119, 204, 0.8)',
}]
},
options: {
tooltips: {
callbacks: {
label: function(t, d) {
var xLabel = d.datasets[t.datasetIndex].label;
var yLabel = t.yLabel >= 1000 ? '$' + t.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") : '$' + t.yLabel;
return xLabel + ': ' + yLabel;
}
}
},
scales: {
yAxes: [{
ticks: {
callback: function(value, index, values) {
if (parseInt(value) >= 1000) {
return '$' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
} else {
return '$' + value;
}
}
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>
Upvotes: 9