PHP Graphing LibraryPHPGraphLib

Lightweight PHP Graphing Library

Documentation Table of Contents - PHPGraphLib
Tutorial #3: PHPGraphLibPie Extension

Bullet Overview

In this example, we go over usage of the PHPGraphLibPie extension, used to make pie charts. Before you read this example, make sure you are familiar with usage of the regular PHPGraphLib class. Tutorial #1 is a good place to start.

BulletInclude both source files in a new script and create the PHPGraphLibPie Object

Include both the PHPGraphLib and PHPGraphLibPie class files at the top of your stand alone php script. This script will only include the PHP calls to create your pie chart. Next, create a PHPGraphLibPie object.

Bullet Create the PHPGraphLibPie Object

Next, you have to create a PHPGraphLibPie object. You may leave the arguments blank to use default dimensions, or you may specify your own width and height.

Bullet Add data to the graph

Just like the regular PHPGraphLib class, you must call the addData() function on your PHPGraphLibPie object. You must specify an array as the argument to the addData() function as shown below. The values in the array will be used as y-axis values. If the array has keys, the keys will be used as x axis values.

In a dynamic web application environment, you could query a database and pass an array of values into your PHPGraphLibPie object.

Bullet Customize the graph

In this example we have decided to give the graph a title and change the displayed % labels from their default, which is black, to blue.

Bullet Finalize the graph

After all customizations are done, we call the createGraph() function to finalize the graph. Now, you can close the php script and include the "?>" php tag.

Here is the whole script we created above:

Bullet Include the created script as the src of an image on the desired page.

On the page you want the graph to appear, create an HTML image tag and set the src="" to the location of the script you created above.

You're done! Here is the pie chart you just created:

Example PHP Pie Chart

BulletFor other examples, view the Examples page