条形图显示要打印的值

时间:2019-03-14 12:29:25

标签: chart.js

我有以下显示信息值的条形图和工具提示

var chartData = {
    labels: ["Chevrolet", "Volkswagen", "Fiat", "Ford", "Renault", "Toyota"],
    datasets: [
        {
            fillColor: "#FFB6C1",
            strokeColor: "#FF1493",
            data: [87, 80, 56, 50, 18, 78]
        }
    ]
};


var ctx = document.getElementById("myChart").getContext("2d");
var myBar = new Chart(ctx).Bar(chartData, {    
});

Fiddle

我想打印该图形,但不会打印工具提示值。那么如何在条形图的顶部显示值?

1 个答案:

答案 0 :(得分:1)

您可以遍历onAnimationComplete函数的各个条并显示值

showTooltips: false,
    onAnimationComplete: function () {

        var ctx = this.chart.ctx;
        ctx.font = this.scale.font;
        ctx.fillStyle = this.scale.textColor
        ctx.textAlign = "center";
        ctx.textBaseline = "bottom";

        this.datasets.forEach(function (dataset) {
            dataset.bars.forEach(function (bar) {
                ctx.fillText(bar.value, bar.x, bar.y - 5);
            });
        })
    }

看看我的完整Fiddle