最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to display value labels above graph bars using chart.js - Stack Overflow

programmeradmin1浏览0评论

I am using chart.js,i want to show the label value above the bars, so how do it?

var barChartData = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    fillColor: "rgba(220,220,220,0.5)",
    strokeColor: "rgba(220,220,220,1)",
    data: [65, 59, 90, 81, 56, 55, 40],
  }]
}

I am using chart.js,i want to show the label value above the bars, so how do it?

var barChartData = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    fillColor: "rgba(220,220,220,0.5)",
    strokeColor: "rgba(220,220,220,1)",
    data: [65, 59, 90, 81, 56, 55, 40],
  }]
}
Share Improve this question edited Apr 4, 2014 at 7:31 Andreas Lyngstad 4,9272 gold badges38 silver badges70 bronze badges asked Apr 4, 2014 at 7:23 user3496929user3496929 511 gold badge1 silver badge3 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

I had the same problem and decided to write it. Please get my version of chart.js from my forked version on github: Chart.js

You can pass the following options:

var barChartData = {
  labels: ["January", "February", "March", "April", "May", "June", "July"],
  datasets: [{
    fillColor: "rgba(220,220,220,0.5)",
    strokeColor: "rgba(220,220,220,1)",
    data: [65, 59, 90, 81, 56, 55, 40],
  }]
};
var opts = {
    scaleShowValues: true, 
    scaleValuePaddingX: 13,
    scaleValuePaddingY: 13
};
var chrt = document.getElementById('chrtDemo').getContext('2d');
new Chart(chrt).Bar(barChartData, opts);

scaleValuePaddingX and scaleValuePaddingY will shift the value position so you can fine-tune it.

You can also pass a dataset of colors to color each bar individually:

var barChartData = {
  labels: ["January", "February", "March"],
  datasets: [{
    fillColor: ["rgba(220,220,220,0.5)", "rgba(220,220,220,0.5)", "rgba(220,220,220,0.5)"],
    strokeColor: ["rgba(220,220,220,1)", "rgba(220,220,220,1)", "rgba(220,220,220,1)"],
    data: [65, 59, 90],
  }]
};
发布评论

评论列表(0)

  1. 暂无评论