Home > Mobile >  Chart.js Scale Y- axis ticks text alignment center
Chart.js Scale Y- axis ticks text alignment center

Time:06-22

I have a question. I want to center the alignment of the text displayed on Yaxis in Chart.js, how can I do it? I don't think the official document has that content.

document link : enter image description here

to be

enter image description here

CodePudding user response:

You can set crossAlign to 'far' in the tick options:

const options = {
  type: 'line',
  data: {
    labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
    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: {
      y: {
        ticks: {
          crossAlign: 'far'
        }
      }
    }
  }
}

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

  • Related