1

Chart

I created a chart using chart.js and it works as expected. The problem is that I don't need the x axis labels (Speed). I looked at questions similar to this but none of them worked. How do you hide the x axis labels without removing or changing anything else in chart.js v3?

LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
Jellyfish
  • 304
  • 3
  • 13

2 Answers2

4

You can do 2 things, either supply an labels array with empty strings or use the tick callback to provide empty labels

 scales: {
            x: {
                ticks: {
                    // Include a dollar sign in the ticks
                    callback: function(value, index, values) {
                        return '';
                    }
                }
            }
        }

Example:

var options = {
  type: 'line',
  data: {
    // option 1, provide empty strings for labels array
    labels: ["", "", "", "", "", ""],
    datasets: [{
        label: '# of Votes',
        data: [12, 19, 3, 5, 2, 3],
        borderWidth: 1
      },
      {
        label: '# of Points',
        data: [7, 11, 5, 8, 3, 7],
        borderWidth: 1
      }
    ]
  },
  options: {
    scales: {
      x: {
        ticks: {
          //option 2, use callback to change labels to empty string
          callback: () => ('')
        }
      }
    }
  }
}

var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
<body>
  <canvas id="chartJSContainer" width="600" height="400"></canvas>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.1.1/chart.js" integrity="sha512-aWx9jeVTj8X49UzUnUHGIlo6rNne1xNsCny/lL0QwUTQK2eilrHXpSk9xbRm4FJ4eLi2XBmnFlRkWPoChSx8bA==" crossorigin="anonymous"></script>
</body>
LeeLenalee
  • 27,463
  • 6
  • 45
  • 69
2

Simply disable the x axis labels by one flag like this:

 options: {
    scales: {
      x: {
        ticks: {
          display: false
        }
      }
    }
 }
Charlie
  • 22,886
  • 11
  • 59
  • 90