Graphing Classes (v0.5)
This zip file contains a class file for drawing four different types of graphs using PHP and the GD graphics libraries. Currently supported graphs are for frequency distribution, bar chart, scatter graph, and line graph.
Graphs can be generated very simply and many aspects of the graphs can be changed. The base Graph class allows for extensions to draw many different types of graph.
The following code snippet demonstrates how a frequency distribution graph can be created:
1 //include the graphing classes 2 require_once("class_graphs.php"); 3 //generate fake data set (groups = seperators for each band, values = what we're plotting) 4 $data = array( 5 "groups" => array(10,20,30,40,50,60,70,80,90,100), 6 "values" => array()); 7 for($i=0;$i<100;$i++) { 8 array_push($data["values"],(rand(0,10)*rand(0,10))); 9 } 10 //create graph object 11 $g = new Graph_FD(400,200); 12 $g->load_data($data); 13 //set up the title 14 $g->set_title("Example Frequency Distribution Graph"); 15 $g->titlefont = "verdanab.ttf"; 16 $g->titlesize = 10; 17 //set up the axis labels 18 $g->labelfont = "verdana.ttf"; 19 $g->labelsize = 8; 20 //set up the small text 21 $g->smallfont = "verdana.ttf"; 22 $g->smallsize = 7; 23 //set labels 24 $g->xtitle = "Percentage rating of some thing that we've been measuring"; 25 $g->ytitle = "Frequency"; 26 //draw the graph 27 $g->draw(); 28 //output to browser 29 $g->output(); 30 //destroy the image 31 $g->destroy();
The outputted image would look something like this:
The zip file contains example of how to set up each type of graph, but please note they currently only support positive data values (a limitation that I will remedy if I have need of negative graphs in any of my projects). The images below show other example graphs that can be generated using the classes:
Bar chart:
Scatter graph:
Line graph:
Multi-bar graph:
I have recently been requested to develop a bar chart capable of supporting multiple bars, as shown below, this addon class is not distributed with the free script here, however, if you would like to licence this or other add ons please contact me
NOTE: the font files used in the example are not distributed with the script, they must be referenced relative to the script that is calling the graphing class. Use TTF fonts and refer to the PHP Documentation for more information.
Details of the zip file are given below:
File size: 70.04KB
File count: 11
Contents of archive:
» graphing/class_graphs.php 27.38KB
» graphing/graph-bar.png 15.46KB
» graphing/graph-fd.png 9.53KB
» graphing/graph-line.png 28.63KB
» graphing/graph-scatter.png 9.67KB
» graphing/graph_bar.php 1.77KB
» graphing/graph_fd.php 1.62KB
» graphing/graph_line.php 1.68KB
» graphing/graph_scatter.php 1.54KB
» graphing/ 0B