<?php
// definimos las columnas con sus valores
$cells = array("pantalones"=>70, "zapatos"=>34, "camisas"=>22, "calcetines"=>60, "faldas"=>77);
$max = max($cells);
$total = count($cells);
// definicion de tamaños
$totalwidth = 400;
$totalheight = 300;
$xgutter = 20; // margen derecho y izquierdo
$ygutter = 20; // marge superior y inferior
$internalgap = 10; // espacio entre las barras
$bottomspace = 30; // espacio en la parte inferior ademas del margen
putenv('GDFONTPATH=' . realpath('.'));
$font = "arialbd.ttf";
header("Content-type: image/gif");
$graphCanX = ($totalwidth - $xgutter*2);
$graphCanY = ($totalheight - $ygutter*2 - $bottomspace); // starting draw position x - axis
$posX = $xgutter; // starting draw pos - y - axis
$posY = $totalheight - $ygutter - $bottomspace;
$cellwidth = (int)(($graphCanX - ($internalgap * ($total-1))) / $total) ;
$textsize = (int)($bottomspace);
// ajustamos el tamaño de la fuente segun la anchura de la letra en el espacio
foreach ($cells as $key => $val) {
while (true) {
$box = ImageTTFbBox($textsize, 0, $font, $key);
$textWidth = abs($box[2]);
if ($textWidth < $cellwidth) {
break;
}
$textsize--;
}
}
$image = imagecreate($totalwidth, $totalheight);
$fondo = ImageColorAllocate($image, 20, 100, 87);
$barra = ImageColorAllocate($image, 100, 56, 56);
$letra = ImageColorAllocate($image, 255, 255, 255);
reset($cells);
foreach ($cells as $key => $val) {
$cellheight = (int) (($val/$max) * $graphCanY);
$center = (int)($posX+($cellwidth/2));
imagefilledrectangle($image, $posX, ($posY-$cellheight), ($posX+$cellwidth), $posY, $barra);
$box = ImageTTFbBox($textsize, 0, $font, $key);
$tw = $box[2];
ImageTTFText($image, $textsize, 0, ($center-($tw/2)), ($totalheight-$ygutter), $letra, $font, $key);
$posX += ($cellwidth + $internalgap);
}
imagegif($image);
?>
Comentarios sobre la versión: 1 (0)
No hay comentarios