Web Design
Web Design Surrey Web Design Services in Surrey

Tutorial Categories

Links & Services

Mailing List

For special offers and annoucements, please enter your email address below.

Dynamically Generated PHP Bar Graphs

In this tutorial I'm going to show you how to make a php file that can take a series of values and turn them into a horizontal bar graph. This function is often useful when making polls - but the function can be used for all manner of bar charts. The end result should look like this:

Dynamic PHP Graph

We are going to create two files. First our page (page.php), and secondly the file that contains the bar chart function - which outputs itself as an image (graph.php).

STEP 1:

First we create the html file. In this file we define the three values to be converted into the bars of the chart. We also create an image with a src value of our graph.php file.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Page.php</title> 
</head> 
<body> 
<?php 
//define 3 values and their titles 
$a = '12'; 
$aTitle= 'oranges'; 
$b = '123'; 
$bTitle = 'apples'; 
$c = '40'; 
$cTitle = 'bananas'; 
?> 
<img src="graph.php?<?="a=$a&b=$b&c=$c&aTitle=$aTitle&bTitle=$bTitle&cTitle$cTitle";?>" /> 
</body> 
</html>

 

STEP 2:

Now we create the php file which will output itself as a PNG image.

<?php 
if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) {  
ob_start("ob_gzhandler");  
} else {  
ob_start();  
} 
//header to store the file
header("Expires: ".date("D, j M Y h:i:s", mktime(0, 0, 0, date("n")+1, date("j")+1, 2020))." GMT"); 
header("Content-Type: image/png");

##################################################### 
//bar graph function file 
##################################################### 

function barGraph($value1,$value2,$value3) { 
     
    //Create a new image 
    $image = imagecreate(400,82); 
     
    //Set the colours of the image parts 
    $background = ImageColorAllocate($image,255,255,255); 
    $border = ImageColorAllocate($image,0,0,0); 
    $fill = ImageColorAllocate($image,43,82,0); 
     
    //create the background of the bars 
    ImageFilledRectangle($image,0,0,302,20,$background); 
    ImageFilledRectangle($image,0,30,302,50,$background); 
    ImageFilledRectangle($image,0,60,302,80,$background); 
     
    //create the bars themselves 
    ImageFilledRectangle($image,1,1,$value1,19,$fill); 
    ImageFilledRectangle($image,1,31,$value2,49,$fill); 
    ImageFilledRectangle($image,1,61,$value3,79,$fill); 
     
    //Create the Borders 
    ImageRectangle($image,0,0,300,20,$border); 
    ImageRectangle($image,0,30,300,50,$border); 
    ImageRectangle($image,0,60,300,80,$border); 
     
    //add labels to the items 
    ImageString($image,5 , 310, 0 ,$_GET['aTitle'],$border); 
    ImageString($image,5 , 310, 30,$_GET['bTitle'], $border); 
    ImageString($image,5 , 310, 60,$_GET['cTitle'], $border); 
     
    //convert this page into a PNG image 
    imagePNG($image); 
     
    //Delete the image from menory 
    imagedestroy($image); 
    } 

//call the function and pass it it's values 
barGraph($_GET['a'],$_GET['b'],$_GET['c']); 
?> 

 

THE END RESULT:

Dynamic PHP Graph


More PHP Tutorials

Recursively Remove Files in Linux

View More...

Output a list of images using PHP

View More...

PHP Dynamically Generated Year Dropdown

View More...

Output All PHP Session Variables

View More...