Thread: [Openfirst-cvscommit] logger graph.php,NONE,1.1
Brought to you by:
xtimg
From: <i-...@us...> - 2003-09-30 23:30:37
|
Update of /cvsroot/openfirst/logger In directory sc8-pr-cvs1:/tmp/cvs-serv27522/logger Added Files: graph.php Log Message: New file. Creates the usage statistics graph. --- NEW FILE: graph.php --- <?php /* * openFIRST.logger - graph.php * * Copyright (C) 2003, * openFIRST Project * Original Author: Greg Inozemtsev <gr...@si...> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ //include database functions include("../config/globals.php"); //set header header("Content-type: image/png"); //create image resource $im=load_png("./backgrounds/back.png"); //image width and height $height=imagesy($im); $width=imagesx($im); //colours $text_color = imagecolorallocate($im, 233, 14, 91); //graph: RGB, modify if necessary $axis_color = imagecolorallocate($im, 0, 0, 0); //axis, scale: RGB, modify if necessary //axis labels imagestring($im, 1, $width-150, $height-8, "Generated by openFIRST Logger", $axis_color); imagestring($im, 2, $width/2-10, $height-11, "Date", $axis_color); imagestringup($im, 2, 2, $height/2+10, "Hits", $axis_color); //y-axis imageline($im, 15,5,15,$height-25,$axis_color); //x-axis imageline($im, 15,$height-25,$width-5,$height-25,$axis_color); //Get graph type if(isset($_GET["graph"])){ $graphtype=$_GET["graph"]; } else { //default to weekly graph $graphtype=1; } //number of labels on y-axis (hits) $ylabels=5; //set variables according to type //(Note: set $labelskip=0 to remove all labels) switch($graphtype){ //monthly case 2: { $graphtitle="Monthly Usage"; $labelskip=6; $labelformat="%a %b %d"; //total days to graph (counting from 0) $days=30; break; } //yearly case 3: { $graphtitle="Yearly Usage"; $labelskip=49; $labelformat="%b %Y"; //total days to graph (counting from 0) $days=366; break; } //weekly default: { $graphtitle="Weekly Usage"; //print every label $labelskip=1; $labelformat="%a %b %d"; //total days to graph (counting from 0) $days=6; break; } } //create the graph //x scale $dx=($width-20)/($days+1); //print graph title imagestring($im, 4, 50, 2, $graphtitle, $axis_color); //this section gets data from Logger DB table $usedata=array(); for($i=0;$i<=$days;$i++) { //get date (86400 seconds in a day) $unixtime=time()-86400*($days-$i); //get month $date=strftime("%B",$unixtime); //fix for PHP running on Win32 (%e not supported by strftime) $day=strftime("%d",$unixtime); //check for single-digit day if (substr($day,0,1)=="0"){ //remove leading 0 $day=substr($day,1,1); } //complete date in "Logger" format, except the time $date.=" ".$day.", ".strftime("%Y",$unixtime); //Uses INSTR for partial comparison. May not work with all databases. $query = ofirst_dbquery("SELECT * FROM ofirst_logger WHERE (INSTR(Date,'".$date."')!=0)"); //number of hits that day $usedata[$i] = ofirst_dbnum_rows($query); //see how many labels to skip if((($i % $labelskip)==0)&&($labelskip!=0)){ //label on the axis imagestring ($im, 1, 10+$dx*$i, ($height-20), strftime($labelformat,$unixtime), $axis_color); } } //find maximum value $max=max($usedata); //find the y scale $step=round($max/$ylabels); $scale=0; while($scale<=$step){ $scale+=50; } //draw the y scale for($i=0;$i<=$max+$dx;$i+=$scale){ imagestring ($im, 1, 17, ($height-33)-(($height-50)*($i/$max)), $i, $axis_color); } //plot the data $i=0; //initial position $px=16; $py=$height-26-$usedata["0"]; foreach ($usedata as $hits) { $x=20+$dx*$i; //y-coordinate of point (scale up to max hits) $y=($height-26)-(($height-50)*($hits/$max)); //draw the graph segment imageline($im, $px,$py, $x, $y,$text_color); if((($i % $labelskip)==0)&&($labelskip!=0)) { //print point label imagestring ($im, 1, $x-5, $y-10, $hits, $text_color); } //save previous x and y $px=$x; $py=$y; //next point $i++; } //unset the array unset($usedata); //send image to browser imagepng ($im); //destroy image resource imagedestroy ($im); ?> <?php //This function loads PNG file into an image resource. If the file does not //exist, creates a blank image. Based on php.net example code. function load_png($imgname) { $image = @imagecreatefrompng ($imgname); // Attempt to open // See if it failed if (!$image) { // Create a blank image $image = imagecreate (500, 300); //fill with white imagecolorallocate ($image, 255, 255, 255); } return $image; } ?> |