PHP Graphing LibraryPHPGraphLib

Lightweight PHP Graphing Library

Documentation Table of Contents - PHPGraphLib
Tutorial #1: Setup & Simple Example

Bullet Overview

To use PHPGraphLib:

  1. Make sure the PHP GD graphics extensionis enabled in your php.ini file.
  2. Include the source file "phpgraphlib.php" in a stand alone php script (no html here).
  3. Create a PHPGraphLib object.
  4. Add data using the addData() function.
  5. Customize graph using desired customization functions.
  6. Finalize graph using createGraph() function
  7. Include the above script as the source location of an image (<img src="my_graph_script.php">)

BulletInclude the source file "phpgraphlib.php" in a new script

To include the source PHPGraphLib class file, add this php include call at the top of your stand alone php script. This script will only include the PHP calls to create your graph. You will not include any HTML output in this script. Later, we will set the src="" element of an html image tag to the location of our new script on the desired page for our graph.

Bullet Creating the PHPGraphLib Object

Next, you have to create a PHPGraphLib 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

To add data to the graph, call the addData() function on your PHPGraphLib 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.

Bullet Customize the graph

In this example we have decided to give the graph a title, and set the y-axis values to be the color red. By default, all PHPGraphLib graphs are bar graphs, so we do not have to call anything special to display this.

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 graph you just created:

Example PHP Graph

BulletFor more advanced examples, view the Examples page