Jump to content

generating array values for pie chart creations using pChart


terungwa

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");

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.