Chartjs-Chart.js hover over slice get NaN percentage

0👍

Try this:

var options = {
        responsive: true,
        // show percentage of slice when hover
        tooltips: {
        callbacks: {
            label: function(tooltipItem, data) {
                var allData = data.datasets[tooltipItem.datasetIndex].data;
                var tooltipLabel = data.labels[tooltipItem.index];
                var tooltipData = allData[tooltipItem.index];
                var total = 0;
                for (var i=0; i<allData.length; i++) {
                    total += allData[i];
                }
                var tooltipPercentage = Math.round((tooltipData / total) * 100);
                return tooltipLabel + ': $ ' + tooltipData + ' (' + tooltipPercentage + '%)';
            }
        }
        }
    };

Leave a comment