Jump to content

Recommended Posts

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:

  1. The absissa series,
  2. 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

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 by terungwa
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.