terungwa Posted September 23, 2014 Share Posted September 23, 2014 I am using the pChart (http://www.pchart.net/) PHP class framework to create a pie chart using database values which I have extracted using this select query: $query = "SELECT Labels, COUNT(quantity) as quantity FROM inventory GROUP BY Labels"; $result = $mysqli->query($query); /* associative array */ while($row = $result->fetch_assoc()) { $titles = (array_values($row)); $count = $row['quantity']; $Labels = $row['Labels']; print_r($count); print_r($Labels); } The implementation procedure for the pChart framework requires two array data sources as shown in code sample below: The absissa series, and ordinate series /* Create and populate the pData object */ $MyData = new pData(); $MyData->addPoints(array(40,60,15,10,6,4,80),"quantity"); $MyData->setSerieDescription("quantity","Application A"); /* Define the absissa series */ $MyData->addPoints(array("Table","Chair","Bed","Stove","Lamp","Rug", "Others"),"Labels"); $MyData->setAbscissa("Labels"); Within this context, how may I get the absissa and ordinate values as arrays into the pChart for creations of the pie chart? Thanks Quote Link to comment Share on other sites More sharing options...
terungwa Posted September 23, 2014 Author Share Posted September 23, 2014 (edited) oops I think I found my solution after all. $count = array(); $Labels = array(); $mysqli = new mysqli("localhost", "user", "password", "dbname"); $query = "SELECT Labels, COUNT(quantity) as quantity FROM inventory GROUP BY Labels"; $result = $mysqli->query($query); /* associative array */ while($row = $result->fetch_assoc()) { $count[] = $row['quantity']; $Labels[] = $row['Labels']; } I defined two empty arrays: $count = array() as abssisa; and $Labels = array() as ordinate; and on each iteration of the while loop, i added to these arrays. the pChart code implementation now looks like this: /* Create and populate the pData object */ $MyData = new pData(); $MyData->addPoints($count,"ScoreA"); $MyData->setSerieDescription("ScoreA","Application A"); /* Define the absissa serie */ $MyData->addPoints($labels,"Labels"); $MyData->setAbscissa("Labels"); Edited September 23, 2014 by terungwa Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.