1
votes

I create a pie chart using google services. the problem is that i want to show credit and debit in pie chart, but output comes only in one color. Here is my query.

$data = mysqli_query($link, "select SUM(pay_payable) as debit, SUM(pay_paid) as credit from purchasers_payment where p_id = '$pur_id'");

and here is my chart setting.

var data = google.visualization.arrayToDataTable([
          ['Debit', 'Credit'],
          <?php
            while($row = mysqli_fetch_array($data))
            {
                echo "['".$row['debit']."',".$row['credit']."],";
            }
            ?>
        ]);

        var options = {
          is3D: true,
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart'));

here is mine code output enter image description here

and i want output like this: enter image description here

1

1 Answers

0
votes

the data format for a PieChart uses rows for each slice
to get two slices, you need two rows of data...

['Label', 'Value'],
['Debit', 10000],
['Credit', 2000]

see following working snippet...

google.charts.load('current', {
    packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Label', 'Value'],
    ['Debit', 10000],
    ['Credit', 2000]
  ]);

  var options = {
    is3D: true,
    height: 300
  };

  var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="piechart"></div>

try the following php...

var data = google.visualization.arrayToDataTable([
  ['Label', 'Value'],
  <?php
    while($row = mysqli_fetch_array($data))
    {
        echo "['Debit',".$row['debit']."],['Credit',".$row['credit']."],";
    }
  ?>
]);