Zend - The PHP Company




Graphs

Add Code


Chronic - html graph generation  

Type: code fragment
Added by: joeldg
Entered: 11/10/2002
Last modified: 31/10/2001
Rating: - (fewer than 3 votes)
Views: 10124
The following is an html graph generator which I developed a while back and have made very good use of in several projects.. *update* - fixed the test array so it does not stretch the page out.


<?
###########################################################################################################
###             CHRONIC - a simple set of graphing functions
###  2002 - Joel De Gan, Tenshimedia LLC
###  This program was written as a small replacment for a more comprehensive system that might use
###  a full graphics class (i.e. jpgraph) where GD might not be available.
###  not that complex, everything is passed in as arrays.
###  I will add in more functionality as I go.
###
### expects: $curdir - current directory.
### $curdir = substr(getcwd()); 
### also expects in general the globals and admin from the phpmyads/control dir.
###########################################################################################################
// uncomment for testing..
$test "true"// set to true to view test
if ($test == "true"){
    
$curdir substr(getcwd(),0,-4); // admin
    
$host "http://localhost/phpmyads";
    if (
$go == ""){$go="index";} // set a default
    
include_once $curdir."control/global.php";
    include_once 
$curdir."control/admin.php";
}
//fi

// define our images.
$horz1 $host "/images/global/graph/horz1.gif";
$horz2 $host "/images/global/graph/horz2.gif";
$horz3 $host "/images/global/graph/horz3.gif";
$horz4 $host "/images/global/graph/horz4.gif";
$horz5 $host "/images/global/graph/horz5.gif";
$MAX_H =  5// max horizontal imgs
$vert1 $host "/images/global/graph/vert1.gif";
$vert2 $host "/images/global/graph/vert2.gif";
$vert3 $host "/images/global/graph/vert3.gif";
$vert4 $host "/images/global/graph/vert4.gif";
$MAX_V 4// max vertical imgs

// table wrapper
function table_this($this$border=1){
    return 
"n<table width=100% border=$border>n".$this."n</table>n";
}
//end function

// table row wrapper
function row_this($this){
    return 
"    <TR>n     ".$this."n    </TR>n";
}
//end function

// table cell wrapper
function cell_this($this$colspan=1$valign="bottom"$align="left"){
    return 
"        <td colspan=$colspan valign=$valign align=$align>".$this."</td>n";
}
//end function

// make a vertival graph..
###########################################################################################################
// will look something like
/*
where $arr = array("First" => 341, "Second" => 231, "Third" => 299); // etc
    +------------+----------+
    | values     | legend   |
    +------------+----------+
    |         #  | A=first  |
    |      #  #  | B=sec    |
    |   #  #  #  | C=third  |
    |#  #  #  #  | D=fourth |
    +------------+----------+
*/
function chronic_vert($arr$mult=18$title=""$type="b"$sort=""){
    global 
$vert1$vert2$vert3$vert4$MAX_V;
    
$tot count($arr);
    
$highval $tot $mult// how high can these be..
    
$colortot 4// number of vert colors to use
    
if ($sort){ arsort($arr); }
    
$keys array_keys($arr);
    
$vals array_values($arr);
    
$highest 0;
    for (
$a=0;$a<$tot;$a++){
       
$allcount += $vals[$a];
       if (
$vals[$a] > $highest){ $highest $vals[$a]; }
    }
//rof

    
$start 1// for images
    
for ($a=0;$a<$tot;$a++){
       
$curr $vals[$a];
       
$percent =  (($curr 100) / $allcount);
       
$percent_int floor($percent);
           
$percent_float number_format($percent1);
       
       
// the percentage of this to highest.
       
$testing = (($curr 100)/ $highest ); // used in next stmt
       // height compared to highest total..
       ##################
       
$size ceil(($testing 100)* $highval); // final height of bar.
       ##################
       
       
if ($start $MAX_V){$start 1; }
       
$imgout = ${"vert".$start};
       
       
$graphpart .= cell_this("<img src=$imgout height=$size width=12>");
       if (
$type=="p"){ $out "(".$percent_float ."%)"; }else if ($type == "n"){ $out "(".$vals[$a].")"; }else{$out=$vals[$a] ."(".$percent_float ."%)";}
       
$ledgpart .=  $out .$keys[$a] ."<br>n";
           
       
$start++;
    }
//rof
    
$graphpart cell_this(table_this(row_this($graphpart),0));
    
$ledgpart cell_this(table_this(row_this(cell_this($ledgpart0"top")),0),0,"top");
    
$final $graphpart $ledgpart;
    if (
$title <> ""){
      
$aprt row_this(cell_this("<b>".$title."</b>"2"middle""center"));
      return 
table_this($aprt row_this($final));
    }else{
      return 
table_this(row_this($final));
    }
//fi
}// end function chronic_vert

// make a horizontal graph
function chronic_horz($arr$mult=18$title=""$type="p"$sort=""){
    global 
$horz1$horz2$horz3$horz4$horz5$MAX_H;
    
$tot count($arr);
    
$highval $tot $mult// how high can these be..
    
$colortot 5// number of vert colors to use
    
if ($sort){ arsort($arr); }
    
$keys array_keys($arr);
    
$vals array_values($arr);
    
$highest 0;
    for (
$a=0;$a<$tot;$a++){
       
$allcount += $vals[$a];
       if (
$vals[$a] > $highest){ $highest $vals[$a]; }
    }
//rof

    
$start 1// for images
    
for ($a=0;$a<$tot;$a++){
       
$curr $vals[$a];
       
$percent =  (($curr 100) / $allcount);
       
$percent_int floor($percent);
           
$percent_float number_format($percent1);
       
       
// the percentage of this to highest.
       
$testing = (($curr 100)/ $highest ); // used in next stmt
       // height compared to highest total..
       ##################
       
$size ceil(($testing 100)* $highval); // final height of bar.
       ##################
       
       
if ($start $MAX_H){$start 1; }
       
$imgout = ${"horz".$start};
       
       if (
$type=="p"){ $out "(".$percent_float ."%)"; }else if ($type == "n"){ $out "(".$vals[$a].")"; }else{$out=$vals[$a] ."(".$percent_float ."%)";}
       
$graphpart .= "<img src=$imgout height=18 width=$size><br>n";
       
$ledgpart .= $out.$keys[$a] ."<br>n";
           
       
$start++;
    }
//rof
    
$graphpart cell_this(table_this(row_this($graphpart),0));
    
$ledgpart cell_this(table_this(row_this(cell_this($ledgpart)),0));
    
$final $ledgpart $graphpart;
    
    if (
$title <> ""){
      
$aprt row_this(cell_this("<b>".$title."</b>"2"middle""center"));
      return 
table_this($aprt row_this($final));
    }else{
      return 
table_this(row_this($final));
    }
//fi


}// end function

###TEST###
if ($test == "true"){
######################################################################################################################
$test = array("First" => rand (10012000), "Second" => rand (10015000), "Third" => rand (10014000), "Fourth" => rand (10013000));

echo 
"<table width=160><tr><td>";
echo 
chronic_vert($test20"testing""n""sort");
echo 
"</td></tr></table>";
echo 
"<table width=160><tr><td>";
echo 
chronic_horz($test20"testing""p");
echo 
"</td></tr></table>";
######################################################################################################################
}//fi
###TEST###

?>


Usage Example




Rate This Script





Search



This Category All Categories