我有以下显示信息值的条形图和工具提示
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, {
});
我想打印该图形,但不会打印工具提示值。那么如何在条形图的顶部显示值?
答案 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