[Chartjs]-React + Chart.js 2.0: How to put a label inside of a doughnut chart?

5👍

In react-chartjs-2 you can access the datasets through chart.config.data.datasets.

So for:

data = {
    labels: [
        'Red',
        'Green',
        'Yellow'
    ],
    datasets: [{
        data: [300, 50, 100],
    ...

Inside the draw callback in Chart.helpers.extend you can use:

var sum = 0;
for (var i = 0; i < this.chart.config.data.datasets[0].data.length; i++) {
   sum += this.chart.config.data.datasets[0].data[i];
}

Here’s an example Codepen: http://codepen.io/anon/pen/xqMQQB?editors=1010

Leave a comment