index.php

<HTML>
<BODY>
<H1>index.php</H1>
<?php
highlight_file
("index.php");
?>
<HR><H1>grapher.php</H1>
<?php
highlight_file
("grapher.php");
?>
<HR>
<?php
require ("./grapher.php");

// Implémente le grapher
$graphtest =  new grapher("Comparaison des ages par sexe et par ann&eacute;e",1);

// Dimensionne le graph
$graphtest -> setDimensions(360,350,400,200);
$graphtest -> setYIndic(5);

// Définit les couleurs
$graphtest -> addElt("Homme","#4BB4F1");
$graphtest -> addElt("Femme","#FF13BA");

// Ajoute des valeurs (en général issue d'une BDD)
$graphtest -> addValue("1973","Homme",31);
$graphtest -> addValue("1973","Femme",42);
$graphtest -> addValue("1974","Homme",12);
$graphtest -> addValue("1974","Femme",8);
$graphtest -> addValue("1975","Homme",55);
$graphtest -> addValue("1975","Femme",34);
$graphtest -> addValue("1976","Homme",75);
$graphtest -> addValue("1976","Femme",9);

// dessine le graphique
$graphtest -> draw(); 

// Implémente le grapher
$graphtest2 =  new grapher("Une Info quelconque par demi-journee");

// Dimensionne le graph
$graphtest2 -> setDimensions(360,700,400,200);
$graphtest2 -> setYIndic(1000);

// Ajoute des valeurs (en général issue d'une BDD)
$graphtest2 -> addValue("AM","Lundi",8815);
$graphtest2 -> addValue("PM","Lundi",2541);
$graphtest2 -> addValue("AM","Mardi",165);
$graphtest2 -> addValue("PM","Mardi",4861);
$graphtest2 -> addValue("AM","Mercredi",6813);
$graphtest2 -> addValue("PM","Mercredi",8465);
$graphtest2 -> addValue("AM","Jeudi",1676);
$graphtest2 -> addValue("PM","Jeudi",9545);
$graphtest2 -> addValue("AM","Vendredi",8461);
$graphtest2 -> addValue("PM","Vendredi",1684);
$graphtest2 -> addValue("AM","Samedi",956);
$graphtest2 -> addValue("PM","Samedi",2512);
$graphtest2 -> addValue("AM","Dimanche",134);
$graphtest2 -> addValue("PM","Dimanche",10);

// dessine le graphique
$graphtest2 -> draw(); 
?>
</BODY>
</HTML>

grapher.php

<?php
class grapher{
/** variables de classe **/
  
var $__orientation;
  var 
$__graphName;
  var 
$__xSize;
  var 
$__ySize;
  var 
$__xOrg;
  var 
$__yOrg;
  var 
$__yIndic;
  var 
$__valUnit;
  var 
$__eltUnit;
  var 
$__eltColors;
  var 
$__maxValue 0;
  var 
$__values;
  var 
$__nbValues;
  var 
$__RVB "808080";
  var 
$__graphNum;
  
/** constructeur label : Nom du Graph; orientation : 0->barres verticales, 1-> barres horizontales **/
/** eltUnit : label de l'axe des donnees, valUnit : label de l'axe des ordonnees **/  
  
function grapher($label="Unamed Graph",$orientation=0,$eltUnit="",$valUnit=""){
    
$this -> __orientation $orientation;
    
$this -> __graphName   $label;
    
$this -> __nbValues    0;
    
$this -> __graphNum    rand(0,strtotime('now'));
  }

/** setDimensions x=largeur, y=hauteur, x0=position gauche, y0= position droite **/
  
function setDimensions($x0,$y0,$x,$y){
    
$this -> __xSize $x;
    
$this -> __ySize $y;
    
$this -> __xOrg  $x0;
    
$this -> __yOrg  $y0;
  }

/** addElt eltName : nom a afficher dans la legende, eltColor : couleur de la barre associee **/
  
function addElt($eltName,$eltColor){
    
$this -> __eltColors[$eltName] = $eltColor;
  }

/** setYIndic step pas entre deux lignes horizontales **/
  
function setYIndic($step=100){
    
$this -> __yIndic $step;
  }

/** addValue $grpName : nom du groupe, $eltName : nom de la donnee, $value : valeur de la donnee **/
  
function addValue($grpName,$eltName,$value){
    if(!isset(
$this -> __eltColors[$eltName])){
      
$this -> __RVB dechex((hexdec($this -> __RVB)+hexdec("345678"))%16777216);
      
$this -> __eltColors[$eltName] = "#".$this -> __RVB;
    }
    
$this -> __values[$grpName][$eltName]=$value;
    
$this -> __maxValue = ( $this -> __maxValue $value ) ? $this -> __maxValue $value;
    
$this -> __nbValues ++;
  }

/** draw **/
  
function draw(){
      
$this -> __orientation == $this -> drawVT() : $this -> drawHz();  
  }
    
  function 
drawHz(){
    if (
sizeof($this -> __values) == 0)
      die (
"Pas de données") ;
    
$pix "barre_mask".$this -> __orientation.".png";
    
$x_step = ($this -> __xSize 60)/$this -> __maxValue;
    
$y_step = ($this -> __ySize 100 $this -> __nbValues)/($this -> __nbValues);

    echo 
"\n".'<!-- graphe --!><STYLE>'."\n".'DIV.graph'.$this -> __graphNum.'{background-color:silver;position:absolute;';
    echo 
'top:'.$this -> __yOrg.'px;left:'.$this -> __xOrg.'px;';
    echo 
'height:'.$this -> __ySize.'px;width:'.$this -> __xSize.'px;}'."\n";
    echo 
'DIV.graph'.$this -> __graphNum.':before{content:"'.$this -> __graphName.'";display:block;text-align:center;}'."\n";
    echo 
'DIV.axes'.$this -> __graphNum.'{background-color:white;position:absolute;top:60px;left:50px;';
    echo 
'width:'.($this -> __xSize 55).'px;';
    echo 
'height:'.($this -> __ySize 100).'px;';
    echo 
'border-width:thin;border-style:solid none none solid;z-index:1;}'."\n";
      
    echo 
'DIV.Ystep'.$this -> __graphNum.'{border-color:slategray;font-size:12;text-align:left;';
    echo 
'border-width:thin;border-style: solid none none none;position:absolute;';
    echo 
'left:-45px;width:45px;z-index:2;}'."\n";
      
    echo 
'DIV.Xstep'.$this -> __graphNum.'{position:absolute;border-width:thin;top:-15px;bottom:0px;width:'.($this->__yIndic $x_step).';border-color:slategray;';
    echo 
'border-style:none dotted none none;font-size:8;text-align:right;vertical-align:bottom;overflow:hidden;}'."\n";
      
    echo 
'DIV.legende{overflow:hidden;background-color:white;position:absolute;left:5px;bottom:5px;right:5px;height:25px;}'."\n";
    echo 
'DIV.legcolor{float:left;height:6px;margin:9px 0px;width:10px;background-repeat:repeat-x;';
    echo 
'background-position:center;background-image:url("./barre_mask1.png");}'."\n";
    echo 
'DIV.legtxt{float:left;height:24px;}'."\n";
    echo 
'DIV.spacer{float:left;width:20px;}'."\n";
    echo 
'DIV.barre'.$this -> __graphNum.'{position:absolute;height:6px;left:0px;background-repeat:repeat-x;';
    echo 
'background-position:center;background-image:url("./'.$pix.'");z-index:3;}'."\n";
    echo 
'</STYLE>'."\n";
      
      
    echo 
'<DIV class="graph'.$this -> __graphNum.'">'."\n";
    echo 
"\n".'<!-- axes --!>'."\n";
    echo 
'<DIV class="axes'.$this -> __graphNum.'">'."\n";

    
$pos=0;
    
$index=1;
    for(
$pos=0;$pos<($this -> __xSize 60 - ($this -> __yIndic $x_step));$pos+=($this -> __yIndic $x_step)){
      echo 
'<DIV class="Xstep'.$this -> __graphNum.'" style="left:'.$pos.'px;">';
      echo (
$index%5==0?($index*$this -> __yIndic):"").'</DIV>'."\n";
      
$index ++;
    }

    
$bottom$y_step/2;
    foreach(
$this -> __values as $grpName => $elt){
      echo 
'<DIV class="Ystep'.$this -> __graphNum.'" style="bottom:'.($bottom-$y_step/2).'px;height:';
      echo (
sizeof($elt)*($y_step+6)).'px;">'.$grpName.'</DIV>'."\n";
      foreach(
$elt as $eltName => $value){
    echo 
'<DIV class="barre'.$this -> __graphNum.'" style="background-color:'.$this -> __eltColors[$eltName].';';
    echo 
'bottom:'.$bottom.'px;width:'.($value*$x_step).'"></DIV>'."\n";
    
$bottom += $y_step;
    
$bottom += ;
      }
    }

    echo 
'</DIV>'."\n";
    echo 
"\n".'<!-- legende --!>';
    echo 
'<DIV class="legende">'."\n";
    foreach(
$this -> __eltColors as $eltName => $color){
      echo 
'<DIV class="legcolor" Style="background-color:'.$color.'"></DIV>'."\n";
      echo 
'<DIV class="legtxt">'.$eltName.'</DIV>'."\n".'<DIV class="spacer">&nbsp;</DIV>'."\n";
    }
    echo 
'</DIV>'."\n";
    echo 
"\n".'</DIV>'."\n";
  }
    
  function 
drawVT(){
    if (
sizeof($this -> __values) == 0)
      die (
"Pas de données") ;
    
$pix "barre_mask".$this -> __orientation.".png";
    
$x_step = ($this -> __xSize 55 $this -> __nbValues)/($this -> __nbValues);
    
$y_step = ($this -> __ySize 100)/$this -> __maxValue;

    echo 
"\n".'<!-- graphe --!><STYLE>'."\n".'DIV.graph'.$this -> __graphNum.'{background-color:silver;position:absolute;';
    echo 
'top:'.$this -> __yOrg.'px;left:'.$this -> __xOrg.'px;';
    echo 
'height:'.$this -> __ySize.'px;width:'.$this -> __xSize.'px;}'."\n";
    echo 
'DIV.graph'.$this -> __graphNum.':before{content:"'.$this -> __graphName.'";display:block;text-align:center;}'."\n";
    echo 
'DIV.axes'.$this -> __graphNum.'{background-color:white;position:absolute;top:20px;left:50px;';
    echo 
'width:'.($this -> __xSize 55).'px;';
    echo 
'height:'.($this -> __ySize 95).'px;';
    echo 
'border-width:thin;border-style:none none solid solid;z-index:1;}'."\n";
    echo 
'DIV.Ystep'.$this -> __graphNum.'{border-color:slategray;font-size:8;';
    echo 
'border-width:thin;border-style: dotted none none none;position:absolute;';
    echo 
'left:-45px;right:0px;height:'.($this->__yIndic $y_step).';z-index:2;}'."\n";
    echo 
'DIV.Xstep'.$this -> __graphNum.'{position:absolute;border-width:thin;bottom:-25px;height:25px;border-color:slategray;';
    echo 
'border-style:none none none solid;font-size:12;text-align:center;overflow:hidden;}'."\n";
    echo 
'DIV.legende{overflow:hidden;background-color:white;position:absolute;left:5px;bottom:5px;right:5px;height:25px;}'."\n";
    echo 
'DIV.legcolor{float:left;height:6px;margin:9px 0px;width:10px;background-repeat:repeat-x;';
    echo 
'background-position:center;background-image:url("./barre_mask1.png");}'."\n";
    echo 
'DIV.legtxt{float:left;height:24px;}'."\n";
    echo 
'DIV.spacer{float:left;width:20px;}'."\n";
    echo 
'DIV.barre'.$this -> __graphNum.'{position:absolute;width:6px;bottom:0px;background-repeat:repeat-y;';
    echo 
'background-position:center;background-image:url("./'.$pix.'");z-index:3;}'."\n";
    echo 
'</STYLE>'."\n";
      
      
    echo 
'<DIV class="graph'.$this -> __graphNum.'">'."\n";
    echo 
"\n".'<!-- axes --!>'."\n";
    echo 
'<DIV class="axes'.$this -> __graphNum.'">'."\n";

    
$pos=0;
    
$index=1;
    for(
$pos=0;$pos<($this -> __ySize 100 - ($this -> __yIndic $y_step));$pos+=($this -> __yIndic $y_step)){
      echo 
'<DIV class="Ystep'.$this -> __graphNum.'" style="bottom:'.$pos.'px;">';
      echo (
$index%5==0?($index*$this -> __yIndic):"").'</DIV>'."\n";
      
$index ++;
    }

    
$left$x_step/2;
    foreach(
$this -> __values as $grpName => $elt){
      echo 
'<DIV class="Xstep'.$this -> __graphNum.'" style="left:'.($left-$x_step/2).'px;width:';
      echo (
sizeof($elt)*($x_step+6)-6).'px;">'.$grpName.'</DIV>'."\n";
      foreach(
$elt as $eltName => $value){
    echo 
'<DIV class="barre'.$this -> __graphNum.'" style="background-color:'.$this -> __eltColors[$eltName].';';
    echo 
'left:'.$left.';height:'.($value*$y_step).'"></DIV>'."\n";
    
$left += $x_step;
    
$left += ;
      }
    }

    echo 
'</DIV>'."\n";
    echo 
"\n".'<!-- legende --!>';
    echo 
'<DIV class="legende">'."\n";
    foreach(
$this -> __eltColors as $eltName => $color){
      echo 
'<DIV class="legcolor" Style="background-color:'.$color.'"></DIV>'."\n";
      echo 
'<DIV class="legtxt">'.$eltName.'</DIV>'."\n".'<DIV class="spacer">&nbsp;</DIV>'."\n";
    }
    echo 
'</DIV>'."\n";
    echo 
"\n".'</DIV>'."\n";
  }
}
?>

25
50
1973
1974
1975
1976
Homme
 
Femme
 
5000
AM
PM
Lundi
 
Mardi
 
Mercredi
 
Jeudi
 
Vendredi
 
Samedi
 
Dimanche