How to represent MySQL data in the form of charts

๐Ÿ‘:0

I hope you do assign $result somewhere. There does not seem to be any query.

Then there is a โ€œ;โ€ missing after your echo statement in the while structure.

Also, your whole data setup does not make any sense: why would you overwrite $set1 and $set2 again and again, just to end up using just the last results.

I assume (by your naming) you want the sets to hold multiple values. That would be achieved by initializing them as arrays first and then pushing each rows values to them:

$set1[] = $row['standName'];
$set2[] = $row['currentQty'];

At least this way you would actually end up with sets of data.

Finally, have a look at how Chart.js works for Pie charts: http://www.chartjs.org/docs/#doughnut-pie-chart-example-usage

Handing over something like $row['standName'] (presumably a string) as a data point value (numeric) does not mak any sense.

Since it is not at all clear, what you are trying to do here, I will go out on a limb:

<?php

...

$data = array();
$colors = array("#F38630","#E0E4CC");
$i=0;
while ($row = mysql_fetch_assoc($result)) {
    echo $row['standName'] . ' | ' . $row['currentQty'] . '  %' . "\n";
    $data[] = array(
        'value' => $row['currentQty'],
        'label' => $row['standName'],
        'color' => $colors[$i%2], // just alternating colors
    );
    $i++;
}

$pieData = jseon_encode($data);

?>
<script type="text/javascript" src="js/Chart.min.js"></script>
<script>
  var myPie = new  Chart(document.getElementById("canvas").getContext("2d")).Pie(<?php echo $pieData; ?>);
</script>

Leave a comment