Reputation: 12838
Is it possible to display text instead of numerical values in the y axis of a line graph with Chart.JS?
Say that we have, for example, the following text-to-value mapping
.
0 = "sedentary"
1 = "under-active"
2 = "active"
....
How can we display the words "sedentary", "under-active", etc instead of the numerical values in the following graph:
Upvotes: 0
Views: 99
Reputation: 32879
Yes, it is possible, and you can achieve that using the userCallback
function for y-axis ticks.
var mapText = ['sedentary', 'under-active', 'active', 'inactive'];
var chart = new Chart(canvas, {
type: 'line',
data: {
labels: ['January', 'February', 'March', 'April'],
datasets: [{
label: 'Statistics',
data: [3, 1, 2, 0],
backgroundColor: 'rgba(0, 119, 204, 0.1)',
borderColor: 'rgba(0, 119, 204, 0.8)',
borderWidth: 1,
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true,
maxTicksLimit: 4, //set as the 'mapText' array length
stepSize: 1,
userCallback: function(t, i) {
return mapText[mapText.length - (i + 1)];
}
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="canvas"></canvas>
Upvotes: 2