Last release: 1.27d
a PHP Class to build Charts
pChart 2.x is born, you can start moving your script to this new version. The new website is at
Overview  |  Screenshots  |  Download  |  Add-ons  |  Demonstration  |  Support  |  :: Documentation ::
Documentation Contents
Class definition
Using pChart with MySQL
Playing with colors
Setting X labels
Series are drawn black?
Debugging charts
Call to undefined function
Digging with pChart
Basic Examples
Advanced Examples
Using pChart with MySQL

Most of the time the pChart library will be linked to a database in order to retrieve dynamic data. This tutorial will show you how to use the pChart and the pData classes to adapt SQL results into a graph. What is done here with MySQL can be easily reused for any kind of database engine like MS SQL server or Oracle.

First of all, connect to your MySQL database:
 $db = mysql_connect("localhost", "username", "password");
 if ( $db == "" ) { echo " DB Connection error...\r\n"; exit(); }

This code will connect to the MySQL service running on the same server than the PHP script using username / password credential. We're then setting the current database to mydatabase.

Retrieving graph data

The graph data will be first retrieved using an SQL query that will depends on your SQL scheme. This simple example assume that the data is located in the column 'value' of the 'data' table. We're also assuming that you are already connected with your database $db.
$Requete = "SELECT `value` FROM `data`";
$result  = mysql_query($Requete,$db);
while ($row = mysql_fetch_array($result))
 { $DataSet->AddPoint($row["value"],"Serie1"); }
Of course it will be better to order your data if you have an index column (named 'id' here) :
$Requete = "SELECT `value` FROM `data` ORDER BY `id`";
$result  = mysql_query($Requete,$db);
while ($row = mysql_fetch_array($result))
 { $DataSet->AddPoint($row["value"],"Serie1"); }
You can retrieve many series in a single query :
$Requete = "SELECT `value1`,`value2`,`value3` FROM `data` ORDER BY `id`";
$result  = mysql_query($Requete,$db);
while ($row = mysql_fetch_array($result))
When you've retrieved your data, don't forget to mark all series as graphable :
Other things to do...

Don't forget to set the series descriptions! This descriptions can come from an SQL query or be statically set depending of your database content.

Statically :
A simple way to deal with SQL should be something like :
$Requete = "SELECT `description` FROM `datadescription` WHERE `serie`='Serie1'";
$result  = mysql_query($Requete,$db);
$row     = mysql_fetch_array($result);
or a more interesting way :
$Requete = "SELECT `description`,`serie` FROM `datadescription`";
$result  = mysql_query($Requete,$db);
while ($row = mysql_fetch_array($result))
 { $DataSet->SetSerieName($row["description"],$row["serie"]); }
..assuming you have a table named 'datadescription' containing two columns 'serie' and 'description'. The 'serie' column containing the ID of the serie we've used while retrieving data : Serie1, Serie2, Serie3 and the description containing the legend text associated.

Last updated on 07/18/2008