Wordpress & Web Development

Add Graphs to Your PHP Web App With pChart

James Bruce 19-02-2012

php graphspChart is a remarkably advanced graphing toolkit for PHP. It’s free under GPL licence, highly customisable, fully object oriented, and more than capable of handling any data you throw at it. Let me show you how to get started with using it for your web app.


This tutorial assumes a basic knowledge of PHP.

Features – At a Glance

  • Easy to get started with tons of example code.
  • Anti-Aliasing for beautiful graphs.
  • Every kind of graph you could possibly imagine, as well as native drawing routines to customise the display even further. (3D graphs are limited to pie charts though)
  • Best-fit line calculation – just give it the data points and let it do the work.
  • Can also create Barcodes, as if graphing wasn’t enough. (Not QR Codes though, only standard 1-dimensional ones)
  • Conditional formatting, to create really visually appealing graphs.Add Graphs to Your PHP Web App With pChart conditional
  • Comprehensive caching class to speed up your graphing in a production environment.

Getting Started

Download the latest pChart package and upload it to the root of your web server. Rename the direct to pChart. You can test it out right away by navigating to this directory which will load the example graphs.
php graphs

Try it Yourself

To learn the basic method of drawing a graph, create a new PHP file in the root of your server called test.php. Add these lines to the top (assuming the directory you upload pChart to named just pChart):

/* Include all the classes */

The next step is to create a dataset, and use the addPoints method.

/* Create your dataset object */
$myData = new pData();
/* Add data in your dataset */

Notice that you can use a VOID keyword if data is missing. You could also connect to a MySQL data source and pull an array of data from there, or load a CSV file from somewhere. We’re going to be drawing a very simplistic graph ofcourse, but you can also add multiple datasets, adjust ticks etc at this point.


Next you need to create the image object, set the graphing area, and choose a font.

$myPicture = new pImage(700,230,$myData); // width, height, dataset
$myPicture->setGraphArea(60,40,670,190); // x,y,width,height

The scale must then be computed before output – but this can be done automatically – then draw the graph, like this:


In this case, we’re drawing a spline chart – basically a curved line chart – but there’s a huge list of other charts you can draw just by changing this function. The very last step is to output the resulting PNG file to the browser. Use the Stroke() function to do this:


You’d use this in cases where you’re either displaying directly to the user, or embedding the PHP as file as an image, like:

<img src="test.php">

If you load the test.php in your browser now, you should see something similar to this:

php graphs

Another option is to render the graph to a file if you’re generating them through some kind of automated CRON job, for instance. In that case, use (where the path of the image is relative to the test.php and in a writeable folder):



Though pChart is the most comprehensive charting toolbox for PHP by far, there are some alternatives:

  • SparkLine PHP is an implementation of the distinctive tiny graphs so named by Edward Tuffte.
  • JqPlot is a jQuery based graphing solution, which would shift the graphing computation to the users browser rather than your server, ideal if you’re trying to plot mathematical functions.
  • PHPMyGraph isn’t as comprehensive, customisable, or attractive as pChart, but it’s also a lot simpler.
  • EasyChart Builder is a simple WordPress plugin, but this requires your dataset to be added manually.
  • Google Image Chart creator has a wide selection of graphs and can be easily embedded, but also requires you to manually enter your dataset.
That’s it from me this time, I do hope you have a play with pChart in web apps you’re making. I’ve just started working it into the web side of my Egg Counter iPhone application, so I’m certainly no expert on it yet. I’ll try to point you in the right direction if you have any questions, but there’s also extensive documentation available. Do you have a better way of graphing data in your web apps? Let us know!

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. Amit
    May 4, 2016 at 5:52 pm

    You are Hero ..Really a big thanks.
    I am working on some project and this resolves my issue

  2. Phil
    June 20, 2012 at 8:43 pm

    How about phpChart? It generates web-based charts using PHP.

    • muotechguy
      June 26, 2012 at 11:58 am

      Looks nice, though it appears to need Javascript too?

      • Phil
        July 6, 2012 at 1:34 am

        It generates require javascript in PHP. Save time.

  3. Abc
    February 20, 2012 at 3:13 am

    I suggest Open Flash Chart 2 too

    • James Bruce
      February 20, 2012 at 9:58 pm

      I'm going to assume thats Adobe Flash based...