Location: PHPKode > projects > ZorbIPTraffic > zorbiptraffic/zorbiptraffic/zorbipyeargraph.php
<?php
include "zorbconf.php";
include("zorblib.php");
include("zorblang$lang.php");
$imgpath="images/";
$get=$_GET;
$getkeys=array_keys($get);
$getcount=count($get) -1;
while($getcount>=0) 
 {
     $key=$getkeys[$getcount];
     $variable=$get[$key];
     $$key=$variable;
     $getcount=$getcount-1;
 }
$width = 600;  
$height = 150;  
$lentb=580;
$image = imagecreatetruecolor($width, $height);  
$zorbblue = imagecreatefrompng($imgpath.'zorbblue.png'); 
$zorbtopblue = imagecreatefrompng($imgpath.'zorbtopblue.png'); 
$zorbred = imagecreatefrompng($imgpath.'zorbred.png'); 
$zorbtopred = imagecreatefrompng($imgpath.'zorbtopred.png'); 

if (!ereg("^(sum|([[:digit:]]{1,3}\.){3}[[:digit:]]{1,3})$",$ip)) $ip="sum";
$year=sprintf("%02d",$year+0);if ($year==0) $year=strftime("%Y",time());

// colors  
$white = imagecolorallocate($image, $bkcolor1, $bkcolor2, $bkcolor3);  
$white1 = imagecolorallocate($image, 0xFF, 0xFF, 0xFF);  
$navy = imagecolorallocate($image, 0x00, 0x00, 0x80);  
$black = imagecolorallocate($image, 0x00, 0x00, 0x00);  
$gray = imagecolorallocate($image, 0xC0, 0xC0, 0xC0);  

// draw something here  

mysql_connect("$dbhost","$dbuname","$dbpass");
$tab=mysql_list_tables("$dbname");

$ret=mysql_query("select hostname from zhost where ip='$ip'"); 
$row = mysql_fetch_array($ret);                                                                                               
$hostname=$row["hostname"];

$totalin=0;$totalout=0;

if ($ip=="sum")
  $ret=mysql_query("select sum(totin) totin,sum(totout) totout,monthid from zmonth where monthid  like '$year%' group by monthid order by monthid"); 
else

  $ret=mysql_query("select totin ,totout totout,monthid from zmonth where monthid  like '$year%' and ip like'$ip' order by monthid"); 
$i=1;
while ($row = mysql_fetch_array($ret))                                                                                               
{
$monthid=substr($row["monthid"],4,2);
if (substr($monthid,0,1)==0) $monthid=substr($monthid,1,1);
while ($i < $monthid)
{
$datain[$i] = 0;
$dataout[$i] = 0;
$i++;
}
$datain[$monthid] = $row["totin"];
$totalin=$totalin+$row["totin"];
$dataout[$monthid] = $row["totout"];
$totalout=$totalout+$row["totout"];
$i++;
}
$totalin=fncScale($totalin,2,0);
$totalout=fncScale($totalout,2,0);
while ($i < 13)
{
$datain[$i] = 0;
$dataout[$i] = 0;
$i++;
}

$maxval = max($datain);  
$nval = sizeof($datain);  

if (max($dataout) > $maxval) 
  {
   $maxval = max($dataout);  
   $nval = sizeof($dataout);  
  }

$vmargin = 20; // top (bottom) vertical margin for title (x-labels)  
$hmargin = 50; // left horizontal margin for y-labels  

$base = floor(($width - $hmargin) / $nval); // distance between columns  

$ysize = $height - 2 * $vmargin; // y-size of plot  
$xsize = $nval * $base; // x-size of plot  

// plot frame  
imagefill($image, 1, 1, $white);
imagerectangle($image, $hmargin, $vmargin,  $lentb, $vmargin + $ysize, $black);
imagefill($image, $hmargin+2, $vmargin+2, $white1);
imagerectangle($image, $hmargin, $ypos,  $lentb, $ypos + 20, $black);
imagefill($image, $hmargin+2, $ypos+2, white);
// title  
$titlefont = 3;  

if ($ip=="sum") $hostname=zorbtrans("Total");
$title = zorbtrans("Year")." $year ($hostname) ".zorbtrans("Dwld").": ".$totalin."   ".zorbtrans("Upld").": ".$totalout;  

// pixel-width of title 
$txtsz = imagefontwidth($titlefont) * strlen($title); 

$xpos = (int)($hmargin + ($xsize - $txtsz)/2); // center the title  
$xpos = max(1, $xpos); // force positive coordinates  
$ypos = 3; // distance from top  

imagestring($image, $titlefont, $xpos, $ypos, $title , $white1);      

// y labels and grid lines  
$labelfont = 2;  
$ngrid = 3; // number of grid lines  

$dydat = $maxval / $ngrid; // data units between grid lines  
$dypix = $ysize / ($ngrid + 1); // pixels between grid lines  

for ($i = 0; $i <= ($ngrid + 1); $i++) 
 {  
    // iterate over y ticks  

    $ydat = fncScale(($i * $dydat),0,0); // height of grid line in units of data  
    $ypos = $vmargin + $ysize - (int)($i*$dypix); // height of grid line in pixels  

    $txtsz = imagefontwidth($labelfont) * strlen($ydat); // pixel-width of label  
    $txtht = imagefontheight($labelfont); // pixel-height of label  
    
    $xpos = (int)(($hmargin - $txtsz) / 2);  
    $xpos = max(1, $xpos);  
	    
    imagestring($image, $labelfont, $xpos-5, $ypos - (int)($txtht/2), $ydat, $black);  
			
    if (!($i == 0) && !($i > $ngrid))  

    imageline($image, $hmargin - 3, $ypos, $lentb, $ypos, $gray);  
    // don't draw at Y=0 and top  
 }  

// columns and x labels  
$padding = 3; // half of spacing between columns  
if ($dydat==0) $dydat=1;
$yscale = $ysize / (($ngrid+1) * $dydat); // pixels per data unit  

												    
for ($i = 0; list($xval, $yval1) = each($datain); $i++) 
{  
    list($xval2, $yval2) = each($dataout);
    
    // vertical columns  
    $ymax = $vmargin + $ysize;  
    $ymin1 = $ymax - (int)($yval1*$yscale);  
    $ymin2 = $ymax - (int)($yval2*$yscale);  
    $xmax = $hmargin + ($i+1)*$base - $padding;  
    $xmin = $hmargin + $i*$base + $padding +10;  
    		    
    $ymin=$ymin1;
    if ($ymin>0)
      {
       if ($ymax-$ymin>0) 
         {
	   imagecopyresized($image, $zorbtopblue,$xmin,$ymin-4 , 0, 0, 16,4, 8, 4);
	   imagecopyresized($image, $zorbblue,$xmin,$ymin , 0, 0, 16,$ymax-$ymin, 8, 1);
	 }
      }
    $ymin=$ymin2;
    if ($ymin>0)
      {
       if ($ymax-$ymin>0) 
         {
	   imagecopyresized($image, $zorbred,$xmin+4,$ymin , 0, 0, 16,$ymax-$ymin, 8, 1);
           imagecopyresized($image, $zorbtopred,$xmin+4,$ymin-4 , 0, 0, 16,4, 8, 4);
	 }                  
      }
    

    // x labels  
    $txtsz = imagefontwidth($labelfont) * strlen($xval);  
			
    $xpos = $xmin + (int)(($base - $txtsz) / 2);  
    $xpos = max($xmin, $xpos);  
    $ypos = $ymax + 3; // distance from x axis  
					    
if ($xval==1)  {$title=substr(zorbtrans("January"),0,3);  }
if ($xval==2)  {$title=substr(zorbtrans("February"),0,3); }
if ($xval==3)  {$title=substr(zorbtrans("March"),0,3);    }
if ($xval==4)  {$title=substr(zorbtrans("April"),0,3);    }
if ($xval==5)  {$title=substr(zorbtrans("May"),0,3);      }
if ($xval==6)  {$title=substr(zorbtrans("June"),0,3);     }
if ($xval==7)  {$title=substr(zorbtrans("July"),0,3);     }
if ($xval==8)  {$title=substr(zorbtrans("August"),0,3);   }
if ($xval==9)  {$title=substr(zorbtrans("September"),0,3);}
if ($xval==10) {$title=substr(zorbtrans("October"),0,3);  }
if ($xval==11) {$title=substr(zorbtrans("November"),0,3); }
if ($xval==12) {$title=substr(zorbtrans("December"),0,3); }
    imagestring($image, $labelfont, $xpos-15, $ypos, $title, $black);  
}  

// flush image  
header("Content-type: image/png"); // or "Content-type: image/png"  
imagepng($image); // or imagepng($image)  
imagedestroy($image);  


?> 
Return current item: ZorbIPTraffic