Login   Register  
PHP Classes
elePHPant
Icontem

File: class_html_chart.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Mario Herrmann  >  HTML Chart  >  class_html_chart.php  >  Download  
File: class_html_chart.php
Role: Class source
Content type: text/plain
Description: HTML Chart Class
Class: HTML Chart
Drawing a Chart in HTML
Author: By
Last change: .
Date: 11 years ago
Size: 7,494 bytes
 

Contents

Class file image Download
<?
/**
    Author: Mario Herrmann <mario at maxx-web dot com>
    


    # English
        
        This is a simple class to paint some statistics  
        just by using simple HTML code
        U don´t need any gdlib extensions to be installed
        
        Maybe the Stylesheet definitions may not fit ur needs
        so just change it in the Construktor or just delete the constructor function
        and make the settings for "td.dia_normal","td.dia_small" and "td.dia_very_small"
        global
        
        Some pictures are needed to paint the Chart axis
        ecke.gif,right_dia.gif,right_spitze.gif,up_dia.gif,up_spitze.gif
        and pixel.gif
        
        if u don´t want to keep them in the same directory as the script u can use
        $object->set_imagefolder("ur/image/folder/");
        
        Usage:        
        $chart =new THtml_Chart();
        $values=array(    "Peter"=>array(10),"Ulf"=>array(10.7),"Jan"=>array(17),"Stefan"=>array(9),"Mario"=>array(40));
        $chart->show_diagramm($values,"Name","Easter Eggs","Simple Easter Egg Chart",array("Number of founded eggs"));
        
        for more examples view the example.php
        
    # Deutsch
        
        Einfache Klasse zur Darstellung von Daten (Statistiken)
        ohne auf Erweiterungen wie die gdlib zurückgreifen zu müssen
        
        Falls die Stylesheetangaben die Im Construktor der Klasse 
        ausgegeben werden nicht zum Benutzten Layout passen
        so können diese leicht geändert werden
        
        Die Bilder ecke.gif,right_dia.gif,right_spitze.gif,up_dia.gif,up_spitze.gif und    pixel.gif
        werden benötigt um die Achsen des Diagramms zu zeichnen
        sollen die Bilder nicht im gleichen Pfad wie das Script liegen
        kann mit $object->set_imagefolder("dein/bilder/ordner/"); ein eigener Ordner
        angegeben werden
        
        Beispiele sind in der example.php
*/

class THtml_Chart{
    
    var 
$bar_colors=array("#FF8000","#004080","#FF0000","#008040","#808080");    
    var 
$bgcolor="#FFFFFF";
    var 
$headcolor="#CCCCCC";
    var 
$bordercolor="#000000";
    var 
$maxstring="maximal value";
    var 
$show_x_values=false;
    var 
$measure="";
    var 
$imagefolder="";
    var 
$show_avg=false;
    var 
$show_cap_avg=false;
    
    function 
THtml_Chart(){        
        echo <<<css
        <style type="text/css">
        <!--
        table       { font-family: Verdana, Arial,Helvetica;font-size: 10pt; }
        td.dia_normal          { font-family: Verdana, Arial,Helvetica;font-size: 10pt; }
        td.dia_small      { font-size: 8pt;color: #000000; }    
        td.dia_very_small      { font-size: 7pt;color: #000000; }        
        -->
        </style>

css;
        
    }
    
    function 
_reset(){
        
$this->bar_colors=array("#FF8000","#004080","#FF0000","#008040","#808080");    
        
$this->bgcolor="#FFFFFF";
        
$this->headcolor="#CCCCCC";
        
$this->bordercolor="#000000";
        
$this->maxstring="maximal value";
        
$this->show_x_values=false;
        
$this->measure="";
        
$this->imagefolder="";
        
$this->show_avg=false;
    }
    function 
set_imagefolder($f){
        
$this->imagefolder=$f;    
    }
    function 
set_measure($unit){
        
$this->measure=$unit;    
    }
    function 
set_maxstring($v){
        
$this->maxstring=$v;    
    }
    function 
htmlcolor($color){
        return 
eregi("^#[0-9A-Fa-f]{6}$",$color);
    }
    
    function 
set_bgcolor($c){
        if(
$this->htmlcolor($c))$this->bgcolor=$c;
    }
    function 
set_bordercolor($c){
        if(
$this->htmlcolor($c))$this->bordercolor=$c;
    }
    function 
set_headcolor($c){
        if(
$this->htmlcolor($c))$this->headcolor=$c;
    }
    function 
set_bar_colors($c){
        if(
is_array($c)){
            foreach(
$c as $v){                
                if(!
$this->htmlcolor($v))return false;
            }    
            
$this->bar_colors=$c;
        }        
    }
    function 
set_show_x_values($c=false){
        if((
$c))
            
$this->show_x_values=true;
        else
            
$this->show_x_values=false;
    }
    function 
set_show_avg($a=false){
        if((
$a))
            
$this->show_avg=true;
        else
            
$this->show_avg=false;
    }
    function 
set_show_cap_avg($a=false){
        if((
$a))
            
$this->show_cap_avg=true;
        else
            
$this->show_cap_avg=false;
    }
    
    function 
show_diagramm($werte,$yachse,$xachse,$tablehead,$legende){        
        
        
$max=0;$anzahl=0;$i=0;$a=0;    
        
        
        if(
is_array($werte)){    
            
            foreach(
$werte as $bezeichner => $values){                
                if(
$this->show_avg){
                    
$avg[]= round(array_sum($values)/count($values),2);    
                }                
                foreach(
$values as $x){                    
                    if(
$x>$max)$max=$x;
                    if(
$this->show_cap_avg){        
                        
$cap_avg[$i]+=$x;
                        
$i++;                                                    
                    }
                }                
                
$anzahl++;
            }
            
            
            if (
$max!=0){
                
?>
                <table border="0" cellpadding="0" cellspacing="0" width="100%">
                <tr bgcolor="<?=$this->bordercolor?>"><td class="dia_normal">
                    <table border="0" cellpadding="5" cellspacing="1" width="100%">
                    <tr bgcolor="<?=$this->headcolor?>"><td colspan="2"><b><? echo $tablehead ?></b></td></tr>
                    <tr bgcolor="<?=$this->bgcolor?>"><td class="dia_normal">
                    <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tr>
                        <td class="dia_small" valign="bottom"><b><? echo $yachse ?></b><br><br></td><td valign="bottom"><img src="<?=$this->imagefolder?>up_spitze.gif" border="0"></td>
                        <td colspan="2" class="dia_small" align="right"><?
                            $colorcount
=0;
                            for(
$i=0;$i<count($legende);$i++){                                
                                echo 
"<font    color=\"".$this->bar_colors[$colorcount]."\"><b>$legende[$i]</b>";
                                if(
$this->show_cap_avg){        
                                    echo 
"( ".round($cap_avg[$i]/$anzahl,2).$this->measure." )";                                        
                                }
                                echo 
"</font><br>";
                                
$colorcount=($colorcount+1)%count($this->bar_colors);
                            }                    
                        
?></td>
                    </tr>
            
                    <?
                
                    $i
=true;
                    foreach(
$werte as $bezeichner => $values){
                        
                        
$colorcount=0;                        
                        echo 
"<tr><td class=\"dia_small\" align=\"right\" valign=\"top\"><nobr>$bezeichner</nobr>";
                        if(
is_array($avg))
                                    echo 
" ($avg[$a]".$this->measure.")";
                        
$a++;
                        echo 
"</td>";
                        
                        if(
$i)
                            echo 
"<td rowspan=\"$anzahl\" background=\"".$this->imagefolder."up_dia.gif\" class=\"dia_very_small\">&nbsp;</td>";    
                        
                        
                        echo 
"<td width=\"100%\" class=\"dia_very_small\">";
                        
                        foreach(
$values as $x){
                            
$prozent=round(100/$max*$x);
                            
                            if(
$this->show_x_values){
                                echo 
"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td class=\"dia_small\" width=\"100%\">";    
                            }
                            
                            echo 
"<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"$prozent%\"><tr><td width=\"100%\" bgcolor=\"".$this->bar_colors[$colorcount]."\"><img src=\"".$this->imagefolder."pixel.gif\" width=\"$prozent%\" height=\"4\" border=\"0\"></td></tr></table>";
                            
                            if(
$this->show_x_values){
                                echo 
"</td><td class=\"dia_very_small\"><nobr>&nbsp;$x&nbsp;<b>".$this->measure."</b></nobr></td></tr></table>";    
                            }
                        
                            
$colorcount=($colorcount+1)%count($this->bar_colors);
                        }                    
                        echo 
"<br></td><td class=\"dia_small\"></td></tr>";    
                        
$i=false;                        
                    }
                    
?>
                    <tr><td class="dia_normal"><span></span></td><td valign="top"><img src="<?=$this->imagefolder?>ecke.gif" border="0"></td><td background="<?=$this->imagefolder?>right_dia.gif"><img src="<?=$this->imagefolder?>right_dia.gif" border="0"></td><td class="dia_normal"><img src="<?=$this->imagefolder?>right_spitze.gif" border="0"></td></tr>
                    <tr><td colspan="4" align="right" class="dia_small"><b><?=$xachse ?>&nbsp;</b>(<?=$this->maxstring?>:<? echo $max." ".$this->measure;?>)</td></tr>
                    
                    </table></td></tr>
                    </table></td></tr></table>
                <?
            
}
        }
    }
}

?>