si, eso funciona pero no cambia q quede una barra muy larga, con ese script pintaria 90 cuadritos. Pero si el maximo a representar es 1000 y tiene 100 q tiene q hacer pintar 100 cuadros llenos y 900 vacios?
Yo me referia a hacer algo parecido al siguiente escript q vi en un tutorial. Aunque no me queda muy claro lo que hace es dibujar 3 barras midiendo el % de gente q a votado para cada sistema operativo (aunque en el tutorial funciona a mi me salta el siguiente error:
Fatal error: Call to undefined function: imagecreate() in W:\www\prueba\SHOW_POLL.PHP on line 72
archivo vote.html
Yo lo q queria era utilizar este sistema de creacion de barras pero para medir la vida.
Código HTML:
<html>
<head>
<title>Encuesta</title>
<head>
<body>
<h1>Sondeo</h1>
<p>¿Cuásl tu sistema operativo favorito?</p>
<form method=post action="show_poll.php">
<input type=radio name=vote value="Windows">Windows<br>
<input type=radio name=vote value="Linux">Linux<br>
<input type=radio name=vote value="Solaris">Solaris<br><br>
<input type=submit value="Mostrar Resultados">
</form>
</body>
archivo SHOW_POLL.PHP
Código PHP:
<?
/*******************************************
Petición a la base de datos para obtener la información de la encuesta
*******************************************/
// hacer log in en la base de datos
if (!$db_conn = @mysql_connect("localhost", "root", "root"))
{
echo "No se puede conectar a la base de datos<br>";
exit;
};
@mysql_select_db("web");
if (!empty($vote)) // Si han cubierto bien el formulario
{
$vote = addslashes($vote);
$query = "update poll_results
set num_votes = num_votes + 1
where candidate = '$vote'";
if(!($result = @mysql_query($query, $db_conn)))
{
echo "No se ha podido conectar a la base de datos<br>";
exit;
}
};
// obtener los resultados actuales de la encuentas, independientemente de lo que haya votado
$query = "select * from poll_results";
if(!($result = @mysql_query($query, $db_conn)))
{
echo "No se puede conectar a la base de datos<br>";
exit;
}
$num_candidates = mysql_num_rows($result);
// calcular el número total de votos hasta ahora
$total_votes=0;
while ($row = mysql_fetch_object ($result))
{
$total_votes += $row->num_votes;
}
mysql_data_seek($result, 0); // resetear el resultado
/*******************************************
Cálculo inicial para el gráfico
*******************************************/
// configurar constantes
$width=500; // ancho de la imagen en pixeles - encajará en 640x480
$left_margin = 50; // espacio a dejar a la izquierda de la imagen
$right_margin= 50; // lo mismo para la derecha
$bar_height = 40;
$bar_spacing = $bar_height/2;
$font = "arial.ttf";
$title_size= 16; // puntos
$main_size= 12; // puntos
$small_size= 12; // puntos
$text_indent = 10; // posición para las etiquetas de texto a la izquierda
// configurar el punto inicial desde el cual dibujar
$x = $left_margin + 60; // colocar la línea de base para dibujar del gráfico
$y = 50; // lo mismo
$bar_unit = ($width-($x+$right_margin)) / 100; // un "punto" en el gráfico
// cálcula el alto del gráfico - barras más espacios más el margen
$height = $num_candidates * ($bar_height + $bar_spacing) + 50;
/*******************************************
Configurar la imagen base
*******************************************/
// crear un lienzo en blanco
$im = imagecreate($width,$height);
// Asignar colores
$white=ImageColorAllocate($im,255,255,255);
$blue=ImageColorAllocate($im,0,64,128);
$black=ImageColorAllocate($im,0,0,0);
$pink = ImageColorAllocate($im,255,78,243);
$text_color = $black;
$percent_color = $black;
$bg_color = $white;
$line_color = $black;
$bar_color = $blue;
$number_color = $pink;
// Crear "lienzo" para dibujar
ImageFilledRectangle($im,0,0,$width,$height,$bg_color);
// Dibujar borde en torno al lienzo
ImageRectangle($im,0,0,$width-1,$height-1,$line_color);
// Añadir Título
$title = "Resultados Sondeo";
$title_dimensions = ImageTTFBBox($title_size, 0, $font, $title);
$title_length = $title_dimensions[2] - $title_dimensions[0];
$title_height = abs($title_dimensions[7] - $title_dimensions[1]);
$title_above_line = abs($title_dimensions[7]);
$title_x = ($width-$title_length)/2; // centrarlo en x
$title_y = ($y - $title_height)/2 + $title_above_line; // centrarlo en y
ImageTTFText($im, $title_size, 0, $title_x, $title_y,
$text_color, $font, $title);
// Dibujar una línea de base un poco por encima de la primera localización de la barra
// a un poco por debajo de la última
ImageLine($im, $x, $y-5, $x, $height-15, $line_color);
/*******************************************
Dibujar los datos en el gráfico
*******************************************/
// Obtener cada línea de los datos de la base de datos y dibujar las barras correspondientes
while ($row = mysql_fetch_object ($result))
{
if ($total_votes > 0)
$percent = intval(round(($row->num_votes/$total_votes)*100));
else
$percent = 0;
// muestra el tanto por ciento para este valor
ImageTTFText($im, $main_size, 0, $width-30, $y+($bar_height/2),
$percent_color, $font, $percent."%");
if ($total_votes > 0)
$right_value = intval(round(($row->num_votes/$total_votes)*100));
else
$right_value = 0;
// tamaño de barra para este valor
$bar_length = $x + ($right_value * $bar_unit);
// dibujar barra para este valor
ImageFilledRectangle($im, $x, $y-2, $bar_length, $y+$bar_height, $bar_color);
// dibujar título para este valor
ImageTTFText($im, $main_size, 0, $text_indent, $y+($bar_height/2),
$text_color, $font, "$row->candidate");
// dibujar contorno mostrando 100%
ImageRectangle($im, $bar_length+1, $y-2,
($x+(100*$bar_unit)), $y+$bar_height, $line_color);
// mostrar números
ImageTTFText($im, $small_size, 0, $x+(100*$bar_unit)-50, $y+($bar_height/2),
$number_color, $font, $row->num_votes."/".$total_votes);
// hacia abajo a la siguiente barra
$y=$y+($bar_height+$bar_spacing);
}
/*******************************************
Mostrar imagen
*******************************************/
Header("Content-type: image/png");
ImagePng($im);
/*******************************************
Limpiar
*******************************************/
ImageDestroy($im);
?>