Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Chart.js - The X axis labels are not all showing

I'm using Chart.js for my pie/bar charts and on the line and the bar chart all of the 'x' axis labels are not all showing for some reason, however in the pie chart they are:

**Image of all 3 graphs along side**

Code below has been used and the 'type' has just been altered for the type of graph I wished to use:

var ctx = document.getElementById("my3Chart");

var mylineChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Time Management", "Career Coach", "Stress & Wellbeing", "Note Taking", "Exam Prep", "Presentations"],
        datasets: [{
            label: 'Module Tracker',
            data: [6, 4, 2, 0, 3, 1],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});
like image 493
K.Haydock Avatar asked Feb 21 '19 19:02

K.Haydock


1 Answers

I've tested your code in a fiddle with Chart.js 2.0 for both bar/line and it looks fine. Chart takes in x-axis labels with:

labels: ["Time Management", "Career Coach", "Stress & Wellbeing", "Note Taking", "Exam Prep", "Presentations"]

The reason you can't see all your x-axis labels is that the width's are too thin. You can fix this by either expanding your width, or rotating your labels to be vertical instead.

  • 400px width example: https://jsfiddle.net/swcy2opv/
  • Rotated labels example: https://jsfiddle.net/swcy2opv/1/
like image 155
Joseph Cho Avatar answered Oct 21 '22 09:10

Joseph Cho