Zend - The PHP Company




Graphs

Add Code


phpGraphIt  

Type: class
Added by: ruddyjs
Entered: 13/09/2001
Last modified: 31/10/2000
Rating: - (fewer than 3 votes)
Views: 12728
This the easy way to make a line chart. This app. containts 2 files : 1. graph.php -- as a class library 2. image.php -- as image creator


-- graph.php ---
<?php
/*
===================================
 File         : image.php
 Atuhor        : Ruddy J.Suhatril
 email        : ruddy_js@yahoo.com
        http://ruddy.powernet.or.id
 Written      : 11 September 2001
 Last Updated : 12 September 2001
===================================
This app. contains 2 files :
1. graph.php (this file) as class file
2. image.php as image creator

This app. is absolutely free.
If you have any suggestion or question please contact me !!! :)
*/
class row {
    var 
$datanya,$head;
    function 
row(){
    
$this->datanya=array();
    }

    function 
cell($legend,$dat){
    
$this->datanya[$legend]=$dat;
    }
    function 
xp(){
     return 
$this->datanya;
    }
 
}
class 
phpGraphIt {
    var 
$tabledata,$rowdata,$confdata;
    var 
$series,$imagefile,$rwdata,$version;
    function 
phpGraphIt(){
    
$this->tabledata=array();
    
$this->series=array();
    
$this->imagefile="image.php";
    
$this->graphtype=0;
    
$this->confdata=array("x"=>600,
                  
"y"=>400,
                   
"showlegend"=>0,
                  
"showgrid"=>1,    
                  
"gridy"=>10);
    
$this->version="1.0.0";
    }
    
    function 
setgridy($shw){
    
$this->confdata["gridy"]=$shw;
    }

    function 
showgrid($shw){
    
$this->confdata["showgrid"]=$shw;
    }
 
    function 
showlegend($shw){
        
$this->confdata["showlegend"]=$shw;
    }
 
    function 
setarea($x,$y){
        
$this->confdata["x"]=$x;
    
$this->confdata["y"]=$y;
    }

    function 
setType($type){
    
$this->graphType=$type;
    }
   
    function 
series($data="",$mean){
    
$this->series[strtolower($data)]=$mean;
    }

    function 
xseries($rwdata=""){
    
$this->rwdata=$rwdata;
    
$this->tabledata[$rwdata]=array();
    
$this->rowdata=new row;
    }
  
    function 
cell($legend,$data){
    
$this->rowdata->cell(strtolower($legend),$data);
    
$this->tabledata[$this->rwdata]=$this->rowdata->xp();
    }

    function 
loadGraph(){
    switch(
$this->graphtype){
        default :
           
$imagefile="image.php";
        break;
        }
    
$serdata=base64_encode(serialize($this->series));
    
$graphdata=base64_encode(serialize($this->tabledata));
    
$confdata=base64_encode(serialize($this->confdata));
    return 
$imagefile."?graphdata=".$graphdata."&serdata=".$serdata."&confdata=".$confdata;
    }
}
?>


-- image.php ---
<?php
/*
===================================
 Class        : phpGraphIt
 Atuhor        : Ruddy J.Suhatril
 email        : ruddy_js@yahoo.com
                http://ruddy.powernet.or.id
 Written      : 11 September 2001
 Last Updated : 12 September 2001
===================================
This app. contains 2 files :
1. graph.php as class file
2. image.php (this file) as image creator

This app. absolutely free.
If you have any suggestion or question please contact me,
*/
if((!empty($graphdata))&&(!empty($serdata))){
  
$dataser=unserialize(base64_decode($serdata));
  
$datagraph=unserialize(base64_decode($graphdata));
  
$dataconf=unserialize(base64_decode($confdata));
  foreach(
$dataconf as $key=>$val){
    $
$key=$val;
  }
  
$minx=50;
  
$maxy=30;
  
$maxx=$x-50;
  
$miny=$y-50;
  
$fonty=1;
  
$fontx=1;
  
$lnglegend=4;
  
header("Content-type: image/jpeg");
  
$im=imagecreate($x,$y);
  
$color[1]=imagecolorallocate($im,0,0,124);
  
$color[3]=imagecolorallocate($im,255,0,255);
  
$color[2]=imagecolorallocate($im,0,255,255);
  
$color[4]=imagecolorallocate($im,150,0,255);
  
$color[5]=imagecolorallocate($im,150,55,55);
  
$white=imagecolorallocate($im,255,255,255);
  
$black=imagecolorallocate($im,0,0,0);
  
imagefill($im,0,0,$white);

  
//Serach criteria for maxseries and maksimum value
  
$maxdata=0;
  
$maxseries=0;
  
$maxrowdata=1;
  foreach(
$datagraph as $dat){
    
$mindata=min($dat);
         
ksort($dat);
    
$i=0;
    foreach(
$dat as $key=>$val){
        
$i++;    
          if(
$maxdata<=$val$maxdata=$val;
        }
    if(
$maxseries<=$i$maxseries=$i;
    
$maxrowdata++;
  }


  
//Process to create grid y
  
$vy=$miny;
  
$vx=$minx;
  
$vx0=$minx-1;
  
$vx1=$minx+1;
  
$maxyxis=$miny-$maxy;
  
$county=round($maxyxis/$gridy);
  
$grd=$maxdata/$gridy;
  
imageline($im,$minx,$miny,$maxx,$miny,$black);
  for(
$i=0;$i<=$gridy;$i++){
     if(
$showgrid){
       
imagedashedline($im,$minx,$vy,$maxx,$vy,$black);
     }
     
$vy0=$vy-1;
     
$vy1=$vy+1;
     
$strgridy=floor($i*$grd);
     
$stlength=strlen($strgridy);
     
$strposx=($minx-$stlength)-($stlength*5)-1;
     
$strposy=$vy-($fonty*4);
     
imagefilledrectangle($im,$vx0,$vy0,$vx1,$vy1,$black);
     
imagestring($im,$fonty,$strposx,$strposy,$strgridy,$black);
     
$vy-=$county;
  }
  
  
//Process to create x series
  
$maxxis=$maxx-$minx;
  
$countx=floor($maxxis/$maxseries);
  
$vx=$minx;
  
$vy=$miny;
  
$vy0=$vy-1;
  
$vy1=$vy+1;
  
imageline($im,$minx,$miny,$minx,$maxy,$black);
  for(
$i=1;$i<=$maxseries;$i++){
     
$place[$i]=$vx;
     
$vx0=$vx-1;
     
$vx1=$vx+1;
     
$strposy=$vy+10;
     
$strposx=$vx;
     
imagefilledrectangle($im,$vx0,$vy0,$vx1,$vy1,$black);
     
imagestring($im,$fontx,$strposx,$strposy,$dataser[$i],$black);
     
$vx+=$countx;
  }

  
//Create line graph
  
reset($datagraph);
  
$h=1;
  
$strposx=$minx;
  
$strposy=$y-20;
  while(list(
$key,$val)=each($datagraph)){
    if(
$showlegend){
      
$strposx1=$strposx+$lnglegend*5;
      
imageline($im,$strposx,$strposy,$strposx1,$strposy,$color[$h]);
      
$strposx=$strposx1+10;
      
$strposy1=$strposy-3;
      
imagestring($im,$fontx,$strposx,$strposy1,$key,$black);
      
$strposx=$strposx1+(strlen($key)*$fontx*10);
        }
    for(
$i=1;$i<=$maxseries;$i++){
       
$j=$i+1;
       if(isset(
$val[$j])&&($val[$j]!=0)){
         
$yy=round($miny-(($val[$i]/$maxdata)*$maxyxis));
             
$yy1=round($miny-(($val[$j]/$maxdata)*$maxyxis));
             
imageline($im,$place[$i],$yy,$place[$j],$yy1,$color[$h]);
           }
        }
        
$h++;
  }
  
imagejpeg($im);
  
imagedestroy($im);
} else {
 
// No image
}


?>



Usage Example


See the example


Rate This Script





Search



This Category All Categories