Chartjs-Chart JS version 3+. How to get a tick's label in a tick callback function?

1👍

Looks like the first two arguments will be the index of said tick.
The third argument seems to be the currently rendered ticks as an object. This is NOT helpful in getting the data.labels of the chart js object.
The getLabelForValue function will get the data.label for said tick index.

callback: function(arg1, arg2, arg3) {
    let labelOfTick = this.getLabelForValue(label)
    //Do some logic
    return something
}

0👍

Try this one:

scales: {
  x: {
    ticks: {
      callback(val:any, index:any): string {
        return index % 2 === 0 && typeof val === 'number' ? this.getLabelForValue(val) : '';
      }
    }
  }
}

Working for me on "chart.js": "^4.3.0"

Leave a comment