Chartjs-How to hide labels by two ticks? chartjs

1๐Ÿ‘

โœ…

You need to use options.scales.yAxes[0].ticks.callback function

render() {
    const options = {
      responsive: true,
      legend: {
        display: false
      },
      type: "bar",
      scales: {
        yAxes: [{
          ticks: {
            callback: function(value, index, values) {
              if (index % 2 === 0) {
                return value;
              }
              return null;
            }
          }
        }]
      }
    };
}

1๐Ÿ‘

Note: @Catalin โ€˜s answer is problematic. Problem: He/She is returning "" in case the label should be hidden. However, you should return null.

Generalized answer

You need to use the options.scales.yAxes.ticks.callback function

const options = {
  scales:{
    yAxes: {
      ticks: {
        callback: function(value, index, values) {
          if (index % 2 === 0) {
            return value;
          }
          return null; // <- NOT ""
        }
      }
    } 
  }
}

Why is null so important?

The impact becomes clear on the xAxes:
Returning null tells chartjs that there is no label.
example when null is returned

Returning "" tells chartjs that there is a label. That means, it reserves space. In this case the labels are slanted:
example when an empty string is returned

Leave a comment