3

I'm using Chart.js plugin to show a Bar Chart and I'm getting output as below:

enter image description here My question is about, how to add a custom text after rendering a value to bar? For example, In Jan, value is showing 56. I want to add % increased/decreased information next to it (i.e. 56 [115 %]) How to do this?

Here's my code

    window.chartHeadcount = new Chart(document.getElementById("barChartHeadcount"), {
        type: 'bar',
        data: {
            labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            datasets: [{
                label: 'Billed',
                backgroundColor: 'rgb(0, 197, 106)',
                data: billedHeadCount
            }, {
                label: 'Unbilled',
                backgroundColor: 'rgb(255, 114, 107)',
                data: unBilledHeadCount
            }]
        },
        options: {
            title: {
                display: true,
                text: 'Community Headcount - ' + Options.Globals.Year
            },
            tooltips: {
                mode: 'index',
                intersect: false
            },
            responsive: true,
            scales: {
                xAxes: [{
                    stacked: false
                }],
                yAxes: [{
                    stacked: false
                }]
            }
        }
    });

1 Answer 1

7

You can use the plugin chartjs-datalabels and set the formatter property to set your custom labels.

Created a fiddle for your reference -> http://jsfiddle.net/upmth2cq/1/

Hope it helps!

new Chart(document.getElementById("barChartHeadcount"), {
  type: 'bar',
  data: {
    labels: ['Jan', 'Feb', 'Mar'],
    datasets: [{
      label: 'Billed',
      backgroundColor: 'rgb(0, 197, 106)',
      data: [56, 63, 67]
    }, {
      label: 'Unbilled',
      backgroundColor: 'rgb(255, 114, 107)',
      data: [1, 2, 3]
    }]
  },
  options: {
    title: {
      display: true,
      text: 'Community Headcount'
    },
    tooltips: {
      mode: 'index',
      intersect: false
    },
    responsive: true,
    scales: {
      xAxes: [{
        stacked: false
      }],
      yAxes: [{
        stacked: false
      }]
    },
    plugins: {
      datalabels: {
        align: 'end',
        anchor: 'end',
        backgroundColor: function(context) {
          return context.dataset.backgroundColor;
        },
        borderRadius: 4,
        color: 'white',
        formatter: function(value){
            return value + ' (100%) ';
        }
      }
    }
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

I'm using it. But, is it possible to get this from another array? instead of hard-code value
Yes, you need to have your own logic inside the function and replace the hardcoded value with the computed one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.