John
John

Reputation: 83

How to change font size of labeled scales in Chart.js?

In chart.js how can I set the set the font size of the axis labels? I tried lots of codes, but none of them worked. I use a simple bar chart. I importing:

<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>

The code:

  options: {
       scales: {
           x: {
               display: true,
               size: 30
           }
       }
    }

On chart.js I have read, that the namespace is : options.scales[scaleId].title but it also didnt worked, when I modify my code. https://www.chartjs.org/docs/3.3.2/axes/labelling.html

Upvotes: 3

Views: 6176

Answers (1)

LeeLenalee
LeeLenalee

Reputation: 31351

You have to configure it in the font options for the label like so:

var options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    datasets: [{
      label: '# of Votes',
      data: [12, 19, 3, 5, 2, 3],
      borderColor: 'pink'
    }]
  },
  options: {
    scales: {
      x: {
        ticks: {
          font: {
            size: 20
          }
        },
        title: {
          display: true,
          text: 'titleX',
          font: {
            size: 25
          }
        }
      },
      y: {
        ticks: {
          font: {
            size: 20
          }
        },
        title: {
          display: true,
          text: 'titleY',
          font: {
            size: 20
          }
        }
      }
    }
  }
}

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.5.0/chart.js"></script>
</body>

EDIT: Since you want to make the ticks bigger you still need to do that with the font object but than in the ticks sub category

Upvotes: 4

Related Questions