[Chartjs]-How could I provide my own ticks array into chart.js?

3👍

You can use the afterBuildTicks callback on the scale:

new Chart('my-chart', {
  type: 'line',
  data: {
    labels: ['A', 'B', 'C', 'D', 'E', 'F', 'G'],
    datasets: [{
      label: 'My Data',
      data: [65, 59, 80, 81, 56, 55, 40],
      borderColor: '#1f77b4'
    }]
  },
  options: {
    scales: {
      y: {
        afterBuildTicks: scale => {
          scale.ticks = [{
              value: 55
            },{
              value: 60
            },
            {
              value: 65
            },
            {
              value: 70
            }
          ]
        }
      }
    }
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.8.0/chart.min.js"></script>
<canvas id="my-chart"></canvas>

Leave a comment