Matt Hackmann

phpGraphs - A PHP Graph Drawing Library

Posted On
web dev


Over the past few days, I've been developing a library to help in drawing graphs for various stuff on the music page. I've built to to be a fairly complete solution that is both robust and simple to use. Today, I offer phpGraphs to the masses to do with as they please, free of charge. Follow the break for a quick tutorial on how to use it.

phpGraphs requires PHP5 and the GD2 extension to be enabled. To install, simply copy the phpGraph.php to a place accessible to your script.

Here's a small script that sets up the graph object an adds data:

Code: php
  1. <?php
  2. include ("");
  3. $graph = new Graph ("My Graph", 640, 480);
  4. $dataSet = $graph->createDataSet ("Data Set 1");
  5. for ($i = 0; $i < 50; $i++)
  6. $dataSet->add (rand () % 100);
  7. ?>

Line 2 - Here, we include the phpGraph library. Pretty self-explanitory.

Line 4 - We create our graph object in the variable $graph. The argument passed is the title that will be drawn at the top of the graph. The second two parameters are the width and height of the image, respectively.

Line 5 - This creates a new dataset with the name "Data Set 1". It returns a pointer to the dataset object within the graph object. You can create an infinite amount of data sets, though there is a visual limitation I will discuss later.

Line 6 - Calling the add function of the object returned on line 5 will add data to the set, in this case a random number from 0 to 100.

phpGraphs has the ability to draw four different types of graph. They are: line, vertical bar, horizontal bar and pie. Following is an example of each graph type along with the function prototype.

Line Graph

Code: prototype
  1. image drawLineGraph (xLabel, yLabel, [drawLegend = true], [divisions = 5]);

Vertical Bar Graph

Code: prototype
  1. image drawVertBarGraph (yLabel, [drawLegend = true], [divisions = 5]);

Horizontal Bar Graph

Code: prototype
  1. image drawHorzBarGraph (yLabel, [drawLegend = true], [divisions = 5]);

Pie Graph

Code: prototype
  1. image $graph-&gt;drawPieGraph ([drawLegend = true]);

All of these are functions of the Graph object and aach returns a handle to a GD image that you can do with as you please. Some notes on the parameters:

xLabel - Label to be drawn on the X axis

yLabel - Label to be drawn on the Y axis

showLegend - Defines whether to show the legend or not. This is turned on by default. If the legend is disabled, the graph will enlarge to fill the image.

divisions - This says how many times to divide up the Y axis (X for horizontal bar graphs). The default is 5, which should work well in most cases.

By default, phpGraphs has colors set for text, image background, data background, grid, and twelve colors for data sets. Now, as I mentioned above, you can have an infinite amount of data sets, but after the twelfth the color will be black as no colors are set beyond that. phpGraphs, however, gives you complete control over the colors you wish to use. The functions are as follows:

Code: prototype
  1. Graph-&gt;setBgColor (r, g, b, [a = 0x00]);
  2. Graph-&gt;setDataBgColor (r, g, b, [a = 0x00]);
  3. Graph-&gt;setTextColor (r, g, b, [a = 0x00]);
  4. Graph-&gt;setGridColor (r, g, b, [a = 0x00]);
  5. Graph-&gt;setDataColor (index, r, g, b, [a = 0x00]);

r, g, b - The red, green and blue values of the color. Valid value range is 0-255.

a - Alpha of the color. Valid values are 0-127, with 0 being completely opaque and 127 being completely transparent.

index - setDataColor allows you to change the color of the lines, bars and slices used for data. The index value specifies the data set number. Valid valus are 1-infinity.

Below is an example of a graph that has a custom color scheme:

A graph using custom colors

Expounding upon the example given above, here is how a complete script would look.

Code: php
  1. <?php
  2. include ("");
  3. // Create the graph and add some data
  4. $graph = new Graph ("My Graph", 640, 480);
  5. $dataSet = $graph->createDataSet ("Data Set 1");
  6. for ($i = 0; $i < 50; $i++)
  7. $dataSet->add (rand () % 100);
  8. // Draw the graph
  9. $img = $graph->drawLineGraph ("Iteration", "Value", false);
  10. // Add the appropriate headers so the browser recognizes this as an image
  11. header ("Content-type: image/png");
  12. imagepng ($img);
  13. imagedestroy ($img);
  14. ?>

That's about all there is to it. If you find any bugs, have any feature requests, or are just having issues, feel free to leave a comment below and I'll be glad to help. Enjoy!

[ Download ]



I copied the same code and executed it is showing the error

Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in c:\apache\htdocs\phpgraphs_1.0\phpgraphs.php on line 24

how can we solve this problem

Leave A Comment