Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stacked horizontal bar chart along total count with chart.js

I am unable to find the solution to add the total count for Stacked horizontal bar chart !

codepen link: code link enter image description hereenter code here

like image 607
B.V.S Bharat Kumar Avatar asked Mar 08 '23 01:03

B.V.S Bharat Kumar


1 Answers

You can use the following code to draw/add total count in your stacked horizontal bar chart :

this.data.datasets[0].data.forEach(function(data, index) {
   var total = data + this.data.datasets[1].data[index];
   var meta = chartInstance.controller.getDatasetMeta(1);
   var posX = meta.data[index]._model.x;
   var posY = meta.data[index]._model.y;
   ctx.fillStyle = 'black';
   ctx.fillText(total, posX + 4, posY + 4);
}, this);

add this inside chart­'s animation onComplete function.

ʟɪᴠᴇ ᴅᴇᴍᴏ

like image 188
ɢʀᴜɴᴛ Avatar answered Mar 24 '23 14:03

ɢʀᴜɴᴛ