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 Link to comment https://forums.phpfreaks.com/topic/291227-generating-array-values-for-pie-chart-creations-using-pchart/ Share on other sites More sharing options...
terungwa Posted September 23, 2014 Author Share Posted September 23, 2014 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"); Link to comment https://forums.phpfreaks.com/topic/291227-generating-array-values-for-pie-chart-creations-using-pchart/#findComment-1491841 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.