Search code examples
javascriptchart.jsreact-chartjs-2

How to hide labels by two ticks? chartjs


How can we customize labels and hide it 2 ticks/line (see the expected output below)? I already tried the code below but it did nothing. Is there any chance we can hide it specifically?

legend: {
   labels: {
      filter: function(label) {
         if (label.text === '10' || label.text === 10) return false;
      }
   }
},

Here is my working code like click link here


Current Output

enter image description here

Expected Output

enter image description here


Solution

  • 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;
                }
              }
            }]
          }
        };
    }