Chartjs-Have text displayed instead of numerical values on y axis

2👍

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>

Leave a comment