<?php

    $i=0; $data = array(
     <BOUCLE_db(POUR) {tableau #ENV{data}} {","}> 
       "#CLE" => #VALEUR
     </BOUCLE_db>
     	);

    if(count($data) == 0) {
        echo "<svg:svg xmlns:svg='http://www.w3.org/2000/svg' width='100%' height='auto' viewBox='-15 0 145 20'>\n"; 
        echo " <svg:text x='50' y='60' transform='scale(0.2 0.2)' fill='black'>[(#ENV{titre}) - ]Aucune donnée.</svg:text>\n";
        echo "</svg:svg>\n";
    } else {
        // Mise à jour des min/max
        $fyear = $lyear = $count =0;
        foreach($data as $key => $value) {
        	// Extraction de l'année
        	preg_match('/x(\d{4})/',$key,$matches);
        	$annee = $matches[1]; 
        	$count += $value;
        	if($i==0) {
        		$fyear = $annee; $lyear = $annee;
        		$i=1;
        	} else {
        		$fyear = ($annee < $fyear)?$annee:$fyear;
        		$lyear = ($annee > $lyear)?$annee:$lyear;
        	}
        }
    
        $dyear = $lyear - $fyear +1;
        $xview = 10*12 + 21;
        $height = 10* $dyear;
        $yview = $height + 20;
     
        echo "<svg:svg xmlns:svg='http://www.w3.org/2000/svg' width='100%' height='auto' viewBox='-12 -2 $xview $yview'>\n";
        echo " <svg:g style='stroke:black; stroke-width:0.5;'>\n";
        echo "   <svg:line x1='20' y1='$height' x2='20' y2='0' />\n";
        echo "   <svg:line x1='20' y1='$height' x2='125' y2='$height' />\n";
        echo " </svg:g>\n";
        echo " <svg:g transform='scale(1 -1) translate(5 -$height)' style='stroke:black; fill:white; stroke-width:0.5;'>\n";

        $grid = array();
        $imois = array('J','F','M','A','M','J','J','A','S','O','N','D');
        $vleg = "";
         
        for($i=0; $fyear+$i <= $lyear; $i++) {
          	// Initialisation des mois à white - 12 mois
          	for($j=0; $j<12; $j++) { $grid[$i][$j] = 'white'; }
          	$py = $i*25 +10; $year = $fyear+$i;
          	$vleg .= "<svg:text x='-40' y='-$py' transform='scale(0.4 -0.4)' fill='black'>$year</svg:text>\n";
        }
        
        foreach($data as $key => $value) {         
          	preg_match('/x(\d{4})-(\d{2})/',$key,$matches);
          	$annee = $matches[1]; $mois=$matches[2]-1;
          	$index = $annee - $fyear;
          	if($value==0) $color = 'white';
            	elseif($value==1) $color = 'lightgreen';
            	elseif($value <5) $color = 'green';
            	else $color = 'forestgreen';           
            $grid[$index][$mois] = $color;      
        }
          
        for($j=0; $j<12; $j++) {
            $px = $j * 10;
            for($i=0; $fyear+$i <= $lyear; $i++) {
            	$py = $i * 10;
            	echo "   <svg:rect x='$px' y='$py' width='10' height='10' fill='".$grid[$i][$j]."'/>\n";
            }
            $tx = $j*25  + 10; 
            echo "<svg:text x='$tx' y='15' transform='scale(0.4 -0.4)' fill='black'>". $imois[$j] ."</svg:text>\n";
        }
        
        echo $vleg;
        echo "</svg:g>";
        $yt = round(($height + 12)/0.3);
        echo "<svg:text x='30' y='$yt' transform='scale(0.3 0.3)' fill='black'> [(#ENV{titre}) - ]Total: $count</svg:text>\n";
        echo "</svg:svg>\n"; 
    }
?>