Change the Y-axis values from numbers to strings in Chart.js - Javascript Chart.js

Javascript examples for Chart.js:Axis

Description

Change the Y-axis values from numbers to strings in Chart.js

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.2.1/Chart.min.js"></script> 
      <script type="text/javascript">
    window.onload=function(){/*from  w w w . j  av a  2s.c  o m*/
var yLabels = {
    10 : 'Test',
    2 : 'Java',
    14 : 'Javascript',
    6 : 'bootcamp',
    18 : 'Oracle',
    10 : 'mid-level',
    12 : 'senior-dev',
    4 : 'full-stack-dev',
    16 : 'famous-speaker',
    8 : 'SQL',
    20 : 'harambe'
}
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["HTML", "CSS", "SCSS", "JavaScript"],
        datasets: [{
            data: [12, 19, 3, 10],
            backgroundColor: [
                'rgba(255, 159, 64, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(255, 206, 86, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        legend: {
            display: false
        },
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero: true,
                    callback: function(value, index, values) {
                        return yLabels[value];
                    }
                }
            }]
        },
        title: {
            display: true,
            text: 'Bar Graph to show proficency'
        }
    }
});
    }

      </script> 
   </head> 
   <body> 
      <canvas id="myChart" width="400" height="400"></canvas>  
   </body>
</html>

Related Tutorials