Chartjs-How to create a chart-js pie chart with PHP data variables?

1👍

You can use PHP echo function for this. You can concatenate your PHP variables into the echo statement. Note: If you want to send an array to the JS, just use json_encode() like shown below.

PHP section:

<?php
echo '
<script type="text/javascript">
var v1=40;
var c1="green";
var v2=40;
var c2="red";
var jsArray =' . json_encode($phpArray) . '; // Remove this if not needed...
</script>';
?>

Javascript section:

var pieData = [
    {
        value: v1,
        color: c1
    },
    {
        value : v2,
        color : c2
    }
];

You can wrap everything in a function for cleaner code. Hope it helps.

Leave a comment