Zend - The PHP Company




Graphs

Add Code


Dynamic Bar Graph  

Type: code fragment
Added by: Arakis
Entered: 25/05/2000
Last modified: 08/12/1999
Rating: ***** (10 votes)
Views: 22996
Draws a dynamic PNG bar graph image from data in parameters


  <? 
//   Welcome to "Club of the developers PHP" 
//  http://phpclub.unet.ru/index_e.php3 
?>
<?php   

/**************************************************************************/   
/* you must supply to this script 3 parameters :  title,width and data.   */   
/* - title is the title of the graph                                      */   
/* - width is the width of the generated graph                            */   
/* - data is used for the values to be displayed                          */   
/*     to split the data you have to use separators "^" and "^^"          */   
/*     -> item_title^value^^                                              */    
/* ---------------------------------------------------------------------- */   
/* call this script directly in the browser or in your html page with the */   
/* img tag.                                                               */   
/* example 1 :                                                            */   
/* graph.php3?title=foo&width=500&data=a^15^^b^20^^                       */   
/* example 2 :                                                            */   
/* <html>                                                                 */   
/*  ...                                                                   */   
/*  <img src="graph.php3?title=foo&width=500&data=a^15^^b^20^^">          */   
/*  ...                                                                   */   
/* </html>                                                                */   
/* ---------------------------------------------------------------------- */   
/* you can use as many data items as you want                             */   
/* you need the GD library in your php module                             */   
/* don't forget to encode you parameters (unicode)                        */   
/* Steve Maranda steve_maranda@teluq.uquebec.ca                           */   
/* Have fun !                                                             */   


//title  
$title urldecode($title);  

//data  
$data urldecode($data);  

//start positions of graph  
$x 40;  
$y 30;  

//right margin  
$right_margin20;  

$bar_width 10;  
$total 0;  
$max 0;  

$unit = (($width-$x)-$right_margin) / 100;  

$itemsexplode"^^",$data);  

//calculate total  
while (list($key,$item) = each($items))  
      {  
      if (
$item)  
         {  
         
$pos strpos($item"^");  
         
$value substr($item,$pos+1,strlen($item));  
         
$total $total $value;  
         }  
      }  

reset($items);  

//calculate height of graph  
$height sizeof($items) * ($bar_width 20);  

Header"Content-type:  image/png");  

$im imagecreate($width,$height);  
    
// Allocate colors  
$white=ImageColorAllocate($im,255,255,255);  
$yellow=ImageColorAllocate($im,240,240,70);  
$blue=ImageColorAllocate($im,0,64,128);  
$bar=ImageColorAllocate($im,64,100,168);  

//background  
ImageFilledRectangle($im,0,0,$width,$height,$blue);  

//title  
$title_x = (imagesx($im)-7.5*strlen($title))/2;  
ImageString($im,3,$title_x,4,$title,$white);  

//date  
//ImageString($im,1,$width-150,17,date("D M jS Y h:i:s A"),$white);  
   
//line  
Imageline($im,$x,$y-5,$x,$height-15,$bar);  

//draw data  
while (list($key,$item) = each($items))  
      {  
      if (
$item)  
         {  
         
$pos strpos($item"^");  
         
$item_title substr($item,0,$pos);  
         
$value substr($item,$pos+1,strlen($item));  

          
//display percent  
         
ImageString($im,3,$x-25,$y-2,intval(round(($value/$total)*100)). "%",$yellow);  

          
//value right side rectangle  
         
$px $x + ( intval(round(($value/$total)*100)) * $unit);  

          
//draw rectangle value  
         
ImageFilledRectangle($im,$x,$y-2,$px,$y+$bar_width,$bar);  

          
//draw item title  
         
ImageString($im,2,$x+5,$y+9,$item_title,$white);  

          
//draw empty rectangle  
         
ImageRectangle($im,$px+1,$y-2,($x+(100*$unit)),$y+$bar_width,$bar);  

          
//display numbers  
         
ImageString($im,1,($x+(100*$unit))-40,$y+12,$value"/".$total,$white);  

         }  
      
$y=$y+($bar_width+20);  
      }  

// Display modified image  
ImagePng($im);  

// Release allocated ressources  
ImageDestroy($im);  
?>  




Usage Example


See the example


Rate This Script





Search



This Category All Categories