SourceForge.net  |  Sunyday.net
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 www.pchart.net.
Overview  |  Screenshots  |  Download  |  Add-ons  |  Demonstration  |  Support  |  :: Documentation ::
Documentation Contents
Overview
Class definition
FAQ
Digging pChart
Data Structure
Running from CLI
Basic Examples
Advanced Examples
Data Structure

It is important for you to understand how to interface your data with the pChart library. To help you dealing with points and serie, I've made the pData class. You're free to use it or not! Sometimes it can be easier to directly create the Data & DataDescription arrays. This is a short summary on how the data are structured.

Data array

This array contains all series and associated data. The fisrt key should be kept as numeric and lineary incremented.
Array
(
    [0] => Array
        (
            [Name] => January
            [Serie1] => 0
            [Serie2] => 1
        )

    [1] => Array
        (
            [Name] => February
            [Serie1] => 1
            [Serie2] => 4
        )

    [2] => Array
        (
            [Name] => March
            [Serie1] => 4
            [Serie2] => 9
        )
) 
To rawly generate this array you can proceed this way :
  $Data[] = array("Name"=>"January","Serie1"=>0,"Serie2"=>1);
  $Data[] = array("Name"=>"February","Serie1"=>1,"Serie2"=>4);
  $Data[] = array("Name"=>"March","Serie1"=>4,"Serie2"=>9);
 
You can also do this way :
  $Data[0]["Name"]   = "January";
  $Data[0]["Serie1"] = 0;
  $Data[0]["Serie2"] = 1;
  $Data[1]["Name"]   = "February";

  /* ... */
 
Data Description array

This array contains informations about the series : which ones will be displayed on graphs, their name, which serie to use as abscissa data.
Array
(
    [Position] => Name
    [Values] => Array
        (
            [0] => Serie1
            [1] => Serie2
        )

    [Description] => Array
        (
            [Serie1] => Year 2007
            [Serie2] => Year 2008
        )

) 
$DataDescription["Position"] contains the name of the serie to use as abscissa data. $DataDescription["Values"] contains the list of the series that will be displayed when calling a graph function of the pChart library. $DataDescription["Description"][] contains the description of the serie that will be displayed when calling the drawLegend function.
  // Set the serie Name as abscissa data
  $DataDescription["Position"] = "Name";

  // Display Serie1 and Serie2 when calling graphs functions
  $DataDescription["Values"] = array("Serie1","Serie2");

  // Set the description of Serie1 to "Year 2007"
  $DataDescription["Description"]["Serie1"] = "Year 2007";
 
Going deeper
Since release 1.27 it is now possible to set special attributes to the X and Y axis. This allow you to name an axis or set its format. All this informations are stored in the DataDescription array.
  // Set the X and Y axis name
  $DataSet->SetXAxisName("Hour");
  $DataSet->SetYAxisName("Call duration");

  // Set the X and Y axis format
  $DataSet->SetYAxisFormat("time");
  $DataSet->SetXAxisFormat("date");

  // Set the X and Y axis units
  $DataSet->SetYAxisUnit("C")
  $DataSet->SetXAxisUnit("h")
 
This infos are stored in the following way in the DataDescription array:
Array
(
    [Format] => Array
        (
            [X] => date
            [Y] => time
        )

    [Unit] => Array
        (
            [X] => C
            [Y] => h
        )

    [Axis] => Array
        (
            [Y] => Call duration
        )

) 
Units and axis names are free. The following formats can be used : date (should contains an unix time stamp), time (should contains a number of seconds), number (default format), metric (can contains everything)

Conclusion

You can build manualy this data structures depending of the source of your data. If you feel not comfortable dealing with arrays, the pData class is here to assist you. One of the most simple graph can be done this way :
  // Create some data
  $Data[] = array("Name"=>"A","Serie1"=>0);
  $Data[] = array("Name"=>"B","Serie1"=>4);
  $Data[] = array("Name"=>"C","Serie1"=>2);
  $Data[] = array("Name"=>"D","Serie1"=>7);
  $Data[] = array("Name"=>"E","Serie1"=>3);
  $Data[] = array("Name"=>"F","Serie1"=>5);

  // Set the serie Name as abscissa data
  $DataDescription["Position"] = "Name";

  // Display Serie1 when calling graphs functions
  $DataDescription["Values"][] = "Serie1";

  // Set the description of Serie1 to "Year 2007"
  $DataDescription["Description"]["Serie1"] = "Year 2007";
 
Will produce something like :


Good luck with your data transformation!

Last updated on 05/20/2008