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

前端 未结 5 1619
死守一世寂寞
死守一世寂寞 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:40

    Just extend the line chart and replace the xLabels you don't want after your initialization

    Chart.types.Line.extend({
        name: "LineAlt",
        initialize: function (data) {
            Chart.types.Line.prototype.initialize.apply(this, arguments);
            var xLabels = this.scale.xLabels
            xLabels.forEach(function (label, i) {
                if (i % 2 == 1)
                    xLabels[i] = '';
            })
        }
    });
    
    
    var lineChartData = {
        labels: ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
        datasets: [
            {
                fillColor: "#79D1CF",
                strokeColor: "#79D1CF",
                data: [59, 80, 81, 56, 55, 40, 34, 43, 43, 12, 65, 65]
            }
        ]
    };
    
    var ctx = document.getElementById("myChart").getContext("2d");
    var myLine = new Chart(ctx).LineAlt(lineChartData);
    

    Fiddle - http://jsfiddle.net/ttz5t3dx/


提交回复
热议问题