Comme indiqué précédement, le but était de faire du reporting :
http://img306.imageshack.us/img306/9419/graphing28yj.th.png
J’ai donc, avec ma nouvelle table, stocké mes dossiers avec le nom des clients et tout ca, et aussi le total de véhicules concerné (infos non présentes directement dans la table dossier d’origine.
Donc mon petit graphique n’est pas mal, il affiche bien le nombre de véhicules en fonction de la date du dossier.
Mais cela n’est pas suffisant, ce que je veux faire c’est afficher une année complète et le nombre de véhicules pour chaque mois… dans ma table la date du dossier est stockée sous la forme AAAA-MM-JJ
Et là j’avoue que je sais pas trop comment bricoler ca.
<?php
/* Cache de l'image */
$path = "graphing.png";
$cachefor = 1;
if (!file_exists($path) || filemtime($path) + $cachefor < time())
{
/* faire un tableau en GD */
$width = 640;
$height = 400;
$image = imagecreate($width, $height);
// couleurs utilisées
$background = imagecolorallocate($image, 218, 231, 237);
$white = imagecolorallocate($image, 255, 255, 255);
$navy = imagecolorallocate($image, 251, 192, 0);
$gray = imagecolorallocate($image, 0xc0, 0xc0, 0xc0);
$black = imagecolorallocate($image, 0, 0, 0);
// On dessine qqch ici
// Exemple 1 du président :
$Database = "bddclients";
$Username = "root";
$Password = "";
$Hostname = "localhost";
$db = mysql_connect($Hostname, $Username, $Password);
mysql_select_db($Database,$db);
$query = "SELECT nombreVehicules, Date FROM reporting";
$requeteID = mysql_query($query);
if (mysql_num_rows($requeteID))
{
while ($array=mysql_fetch_array($requeteID))
{
$data[$array['Date']] = $array['nombreVehicules'];
}
}
$maxval = max($data);
$nval = sizeof($data);
$vmargin = 20; // marge verticale (haut et bas)
$hmargin = 38; // marge horizontale (gauche et droite)
$base = floor(($width - $hmargin * 2) / $nval); // distance entre deux colonnes, calculée sur l'espace disponible, c'est à dire la largeur de l'image - la marge, le tout à diviser par le nombre de barres à tracer
$ysize = $height - 2 * $vmargin;
$xsize = $nval * $base;
imagerectangle($image, $hmargin, $vmargin, $hmargin + $xsize, $vmargin + $ysize, $black);
// Titre du graphique
$titlefont = 3;
$title = "DOI Avaries, test de graphique de reporting";
$txtsz = imagefontwidth($titlefont) * strlen($title);
$xpos = (int)($hmargin + ($xsize - $txtsz) / 2);
$xpos = max(1, $xpos);
$ypos = 3;
imagestring($image, $titlefont, $xpos, $ypos, $title, $black);
// Grille et échelles
$labelfont = 2;
$ngrid = 5;
$dydat = $maxval / $ngrid;
$dypix = $ysize / ($ngrid + 1);
for ($i = 0; $i <= ($ngrid +1); $i++)
{
$ydat = (int)($i * $dydat);
$ypos = $vmargin + $ysize - (int)($i*$dypix);
$txtsz = imagefontwidth($labelfont) * strlen($ydat);
$txtht = imagefontheight($labelfont);
$xpos = (int)(($hmargin - $txtsz) / 2);
$xpos = max(1, $xpos);
imagestring($image, $labelfont, $xpos, $ypos - (int)($txtht/2), $ydat, $black);
if (!($i == 0) && !($i > $ngrid))
imageline($image, $hmargin - 3, $ypos, $hmargin + $xsize, $ypos, $gray);
}
// Dessiner les barres
$padding = 3;
$yscale = $ysize / (($ngrid+1) * $dydat);
for ($i = 0; list($xval, $yval) = each($data); $i++)
{
$ymax = $vmargin + $ysize-1;
$ymin = $ymax - (int)($yval*$yscale);
$xmax = $hmargin + ($i+1)*$base - $padding;
$xmin = $hmargin + $i*$base + $padding;
imagefilledrectangle($image,$xmin,$ymin,$xmax,$ymax,$navy);
$txtsz = imagefontwidth($labelfont) * strlen($xval);
$xpos = $xmin + (int)(($base - $txtsz) / 2);
$xpos = max($xmin, $xpos);
$ypos = $ymax + 3;
imagestring($image, $labelfont, $xpos, $ypos, $xval, $black);
}
// Purger l'image !!
header("Content-type: image/png"); // header : fichier image PNG
imagepng($image, $path);
imagedestroy($image);
}
header("Content-type: image/png");
readfile($path);
?>
PS : par ailleurs vous noterez un léger bug graphique sur mon graph, alors que la première date possède UN véhicule, et bien la barre arrive presque à 2
je sais pas trop à quoi cela est dû 