Create Chart.js pie chart data from PHP

0👍

You need to convert the values in the JSON to numbers, JSON encodes them as strings.

parseInt() 

in Javascript should do the trick.

I got confused with this as well, kept me occupied for weeks.

For PHP Var

var pie = [{ value: parseInt(<?PHP VARIABLE?>), color: "#F7464A" }]

👍:0

Hi I was facing the same problem and i found solution. which is working absolutly fine.

My json data
[{"week_name":"18","taskcount":"4"},{"week_name":"16","taskcount":"17"},{"week_name":"15","taskcount":"11"},{"week_name":"13","taskcount":"20"}]
so,

$obj = json_decode($jsonData);

var PieData = [<?php for($i=0;$i<count($obj);$i++){ ?>
{

    value: <?php echo $obj[$i]->taskcount;?>,

    label: 'Week '+<?php echo $obj[$i]->week_name;?>

  },
          <?php } ?>
];

👍:0

For all those still wondering how to pass an integer PHP variable as a value in Chartjs javascript here’s how:

var PieData        = [
      {
        value    : parseInt(<?php echo $variable; ?>),
        color    : '#00c0ef',
        highlight: '#00c0ef',
        label    : 'Blue'
      },

I used this with Adminlte 3 – Hope it helps!

Leave a comment