Chartjs-How to display data with percentage from database in pie chartjs?

1👍

To get everything into a percentage. You’re gonna need to compute them.

Like so:

var total = session + total_yes + total_no;
session = (session / total) * 100;
total_yes = (total_yes / total) * 100;
total_no= (total_no / total) * 100;

Then in your chartdata, just use them:

var chartdata = {
      labels: ['session', 'yes', 'no'],
      datasets: [
        {
          label: 'session',
          backgroundColor: ['#00a65a', '#FF00FF'],
          highlight: '#00c0ef',
          data: session
        },
        {
          label: 'yes',
          backgroundColor: ['#00a65a', '#FF00FF'],
          highlight: '#00c0ef',
          data:total_yes
        },
          {
          label: 'no',
          backgroundColor: ['#00a65a', '#FF00FF'],
          highlight: '#00c0ef',
          data:total_no
        }
      ]
    }

If you require to add more items just add it to the total variable them do the same to get the percentage and then add it to the chartdata. And so on.

Leave a comment