show label in tooltip but not in x axis for chartjs line chart

前端 未结 5 1647
死守一世寂寞
死守一世寂寞 2020-12-08 23:34

I currently am using a line chart with chart.js, and have a label set that looks like this [\"January 2015\", \"February 2015\", \"March 2015\", \"April 2015\", \"May

5条回答
  •  长情又很酷
    2020-12-09 00:29

    For anyone looking to achieve this on Chart JS V2 the following will work:

     var options =  {  
             scales: {
                xAxes: [{
                    afterTickToLabelConversion: function(data){
    
    
                    var xLabels = data.ticks;
    
                    xLabels.forEach(function (labels, i) {
                        if (i % 2 == 1){
                            xLabels[i] = '';
                        }
                    });
                } 
            }]   
        }
    }
    

    Then pass the options variable as usual into a:

    myLineChart = new Chart(ctx, {
        type: 'line',
        data: data,
        options: options
    });`
    

提交回复
热议问题