Contents
Login Register Wishes Forum About
Language Reference » Basic Syntax
Help topics
Latest updates
1998d  replaceImageMapTitle
1998d  replaceImageMapValues
1998d  createFunctionSerie
1998d  setAbsicssaPosition
1998d  negateValues
Toolbox
  Download as PDF
  Print this page
  Share this page
  Create an account
  Feed the updates!
Google AdSense
Basic Syntax

This page summarize some of the basic concepts of pCharts graphics.

Include the library

pChart is composed of classes, to add the objects definition in you codes, you must add some includes in your projects headers. In PHP, adding external files is pretty easy using the include() function. Sometimes, you may prefer the require_once() function.

All your scripts will start by the following lines :
 /* Include all the classes */
 include("class/pDraw.class.php");
 include("class/pImage.class.php");
 include("class/pData.class.php");

Note that if you need to use sessions, they must be initiated before this line to avoid messages like :
Cannot send session cookie - headers already sent by [..]

Prepare your dataset

There are many ways to populate your dataset. The easiest way is to use the addPoints method of the pData class. With this method you can add values one by one or directly provide an array containing the data that you want to chart.
 /* Create your dataset object */
 $myData = new pData();

 /* Add data in your dataset */
 $myData->addPoints(array(VOID,3,4,3,5));

In this example we are providing 5 values, the 4th one is missing and replaced by the VOID keyword.

Create your pChart object

Next step is to create your drawing object. The following will create a 700x230px picture :
 /* Create a pChart object and associate your dataset */
 $myPicture = new pImage(700,230,$myData);

Define your chart area

Before calling any scaling or charting function, you must define the chart area boundaries.
 /* Define the boundaries of the graph area */
 $myPicture->setGraphArea(60,40,670,190);

Choose a nice font

The default font may look a bit "agressive", hopefully the web is full of free nice-looking TTF files. You can set a different font, all the function that are writting text will use it.
 /* Choose a nice font */
 $myPicture->setFontProperties(array("FontName"=>"fonts/Forgotte.ttf","FontSize"=>11));

Compute the scale

Scale computing is mandatory before calling the charting functions. You can define which scaling mode to use (classical, add values) and the way you want to display and rotate the axis.
 /* Draw the scale, keep everything automatic */
 $myPicture->drawScale();

Draw your chart

It‘s now time to choose which kind of chart you‘ll draw. We‘ll use a spline here :
 /* Draw the scale, keep everything automatic */
 $myPicture->drawSplineChart();

All done! Let‘s render to the browser

You can now finalize your graph and send it to the user browser :
 /* Build the PNG file and send it to the web browser */
 $myPicture->Stroke();

Put it all together

As you‘ll see, this is a very basic example of what pChart can do for you. When you‘ll make your own scripts, you‘ll see that the most difficult thing is to figure out how you want to represent your data. pChart can do a lot of things for you, we invite you to walk in this documentation to use the full potential of this library.


 /* Include all the classes */ 
 include("class/pDraw.class.php"); 
 include("class/pImage.class.php"); 
 include("class/pData.class.php");

 /* Create your dataset object */ 
 $myData = new pData(); 
 
 /* Add data in your dataset */ 
 $myData->addPoints(array(VOID,3,4,3,5));

 /* Create a pChart object and associate your dataset */ 
 $myPicture = new pImage(700,230,$myData);

 /* Choose a nice font */
 $myPicture->setFontProperties(array("FontName"=>"fonts/Forgotte.ttf","FontSize"=>11));

 /* Define the boundaries of the graph area */
 $myPicture->setGraphArea(60,40,670,190);

 /* Draw the scale, keep everything automatic */ 
 $myPicture->drawScale();

 /* Draw the scale, keep everything automatic */ 
 $myPicture->drawSplineChart();

 /* Build the PNG file and send it to the web browser */ 
 $myPicture->Render("basic.png");
Last updated on 12/06/2010 
by Jean-Damien 
Linked resources
  There is no linked topic yet.
Community comments
Message from Boaz R on 15/06/2011
typo on page: see first code section, where it ‘include()"s pImage. There‘s typo there in the form of unwanted characters.
© Copyrights
Components used on this web site : Famfamfam icons has been made by Mark James, Rounded corners lite has been coded by Cameron Cooke and Tim Hutchison, SyntaxHighlighter has been written by Alex Gorbatchev. pChart and this web site have been created by Jean-Damien POGOLOTTI. This documentation contains 185 pages and 56 comments. 409 users have registered. This page has been rendered in 0,01 seconds. Wiki revision 1.37.