<?php
  | 
/*---------------------------------------------------------------*/
  | 
/*
  | 
    Titre : Représentation graphique                                                                                     
  | 
                                                                                                                          
  | 
    URL   : https://phpsources.net/code_s.php?id=969
  | 
    Date Ã©dition     : 15 Fév 2019                                                                                 
  | 
*/
  | 
/*---------------------------------------------------------------*/
  | 
  | 
 function draw_graphe($stats , $x , $y , $margin , $mode , $filename){
  | 
  | 
 /* Parametres: 
  | 
  * $stats : tableau de valeurs 
  | 
  * $x : largeur de l'image a creer en px 
  | 
  * $y : hauteur de l'image a creer en px 
  | 
  * $margin : marge entre le bord de l'image et le graphe en px 
  | 
  * $mode : type de graphe ("C" : lignes, "H" : histogrammes, 
  | 
  * "C+H" : les deux a la fois) 
  | 
  * $filename : nom de sauvegarde de l'image 
  | 
  */ 
  | 
  
  | 
  if (!is_array($stats) || ($x <= 0) || ($y <= 0) || ($margin <= 0))
  | 
  return 0; 
  | 
  $vals = array_values($stats); 
  | 
  $keys = array_keys($stats); 
  | 
  
  | 
  $pic = imagecreate($x, $y); 
  | 
  $min = min($vals); 
  | 
  $max = max($vals); 
  | 
  $taille = sizeof($vals); 
  | 
  
  | 
  $white = imagecolorallocate($pic, 255, 255, 255); 
  | 
  $black = imagecolorallocate($pic, 0, 0, 0); 
  | 
  $red = imagecolorallocate($pic, 255, 0, 0); /* couleur de la courbe */ 
  | 
  
  | 
  /* remplit le fond de l'image */ 
  | 
  imagefill($pic, 0, 0, $white); 
  | 
  /* dessine les axes et les fleches */ 
  | 
  $lx1 = $margin; 
  | 
  $ly1 = $margin + 10; 
  | 
  $lx2 = $margin; 
  | 
  $ly2 = $y - $margin; 
  | 
  imageline($pic, $lx1, $ly1, $lx2, $ly2, $black); 
  | 
  $triangle = array 
  | 
  ($lx1, $ly1 - 10, 
  | 
   $lx1 - 5, $ly1, 
  | 
   $lx1 + 5, $ly1); 
  | 
  imagefilledpolygon($pic, $triangle, 3, $black); 
  | 
  $lx1 = $margin; 
  | 
  $ly1 = $y - $margin; 
  | 
  $lx2 = $x - $margin - 10; 
  | 
  $ly2 = $y - $margin; 
  | 
  imageline($pic, $lx1, $ly1, $lx2, $ly2, $black); 
  | 
  $triangle = array 
  | 
  ($lx2 +10, $ly2, 
  | 
   $lx2, $ly1 - 5, 
  | 
   $lx2, $ly1 + 5); 
  | 
  imagefilledpolygon($pic, $triangle, 3, $black); 
  | 
  
  | 
  /* difference entre 2 valeurs de l'axe des ordonnes */ 
  | 
  $range_y = ($max-$min)/$taille; 
  | 
  /* nombre de pixels separant 2 valeurs sur l'axe des abscisses */ 
  | 
  $dif_x = (int)(($x - 2*$margin - 20)/$taille); 
  | 
  /* nombre de pixels separant 2 valeurs sur l'axe des ordonnes */ 
  | 
  $dif_y = (int)(($y - 2*$margin - 20)/($max - $min + $range_y)); 
  | 
  /* dessine les histogrammes */ 
  | 
  if ($mode == "H" || $mode == "C+H") 
  | 
  { 
  | 
  srand((double)microtime()*1000000); 
  | 
  for ($i=0;$i<$taille;$i++) 
  | 
  { 
  | 
  $rx1 = (int)($i*$dif_x + $margin); 
  | 
  $ry1 = (int)($y - $margin - ($vals[$i]-$min + $range_y)*$dif_y); 
  | 
  $rx2 = (int)(($i+1)*$dif_x + $margin); 
  | 
  $ry2 = (int)$y-$margin; 
  | 
  
  | 
  /* couleur aleatoire */ 
  | 
  $r = rand(1, 255); 
  | 
  $g = rand(1, 255); 
  | 
  $b = rand(1, 255); 
  | 
  $col = imagecolorallocate($pic, $r, $g, $b); 
  | 
  imagefilledrectangle($pic, $rx1, $ry1, $rx2, $ry2, $col); 
  | 
  imagestring($pic, 3, $rx1+$dif_x/2, $ry2+10, $keys[$i], $black); 
  | 
  } 
  | 
  for ($i=0;$i<$taille;$i++) 
  | 
  { 
  | 
  $rx1 = (int)($i*$dif_x + $margin); 
  | 
  $ry1 = (int)($y - $margin - ($vals[$i]-$min + $range_y)*$dif_y); 
  | 
  
  | 
  imagestring($pic, 3, $rx1+$dif_x/2, $ry1 - $margin/2, $vals[$i], $black);
  | 
  } 
  | 
  } 
  | 
  /* dessine les portions de droites */ 
  | 
  if ($mode == "C" || $mode == "C+H") 
  | 
  { 
  | 
  for ($i=0;$i<$taille-1;$i++) 
  | 
  { 
  | 
  $lx1 = (int)(($i+1)*$dif_x + $margin - $dif_x/2); 
  | 
  $ly1 = (int)($y - $margin - ($vals[$i]-$min + $range_y)*$dif_y); 
  | 
  $lx2 = (int)(($i+2)*$dif_x + $margin - $dif_x/2); 
  | 
  $ly2 = (int)($y - $margin - ($vals[$i+1]-$min + $range_y)*$dif_y);
  | 
  /* marque les graduations de l'axe des abscisses */ 
  | 
  if ($mode == "C") 
  | 
   { 
  | 
  if ($i==0) 
  | 
   { 
  | 
  imagestring($pic, 3, $lx1, $ly1 - $margin/2, $vals[$i], $black); 
  | 
   imagestring($pic, 3, $lx1, $y-$margin+10, $keys[$i], $black); 
  | 
   } 
  | 
  imagestring($pic, 3, $lx2, $ly2 - $margin/2, $vals[$i+1], $black);
  | 
  imagestring($pic, 3, $lx2, $y-$margin+10, $keys[$i+1], $black); 
  | 
  } 
  | 
  imageline($pic, $lx1, $ly1, $lx2, $ly2, $red); 
  | 
  } 
  | 
  } 
  | 
  /* marque les graduations de l'axe des ordonnées */ 
  | 
  $lx = 10; 
  | 
  $acc = $y - $margin - 10; 
  | 
  $limite = $margin + 10; 
  | 
  $valeur = $min - $range_y; 
  | 
  while ($acc >= $limite) 
  | 
  { 
  | 
  imagestring($pic, 3, $lx, $acc, "$valeur", $black); 
  | 
  $valeur += $range_y; 
  | 
  $acc -= $dif_y*$range_y; 
  | 
  } 
  | 
  @imagepng($pic, $filename); 
  | 
  if (!file_exists($filename)) 
  | 
  return 0; 
  | 
  return 1; 
  | 
}
  | 
  | 
?>
  | 
  | 
 |