PDA

View Full Version : making a graph


michael marder
01-14-2004, 04:18 AM
i'm setting up a PHP page that will put data to and retrieve data from a MySQL table. the idea is that people will put in their weight each day, and would want to review their progress at losing weight.

i want to be able to generate a graph that shows, let's say their daily weights for the last 30 days. can this be done? can someone point me in the right direction so i can teach myself how to do this.

PureForm
01-22-2004, 03:55 AM
You would want to use something like GD [http://www.php.net/manual/en/ref.image.php] or ImageMagick [HR has both], but that would take a long ass time to code. I say we have HR install JPGraph [http://www.aditus.nu/jpgraph/] ... which would make creating graphs a million times easier ... and it's free.

Viper007Bond
01-22-2004, 05:05 AM
Originally posted by PureForm
I say we have HR install JPGraph [http://www.aditus.nu/jpgraph/] ... which would make creating graphs a million times easier ... and it's free.
I believe they installed that for me. They'll install a lot of stuff if you need it.

Anyway, this gets... interesting. Beyond my skills.

y6y6y6
01-26-2004, 11:15 AM
php with GD works great for creating graphs with data drawn from MySQL. Here's an example from one of my sites (http://jonsullivan.org/graphs.php).

Viper007Bond
01-26-2004, 01:19 PM
Did you write that?

y6y6y6
01-26-2004, 04:08 PM
Yep. Much fun.

Viper007Bond
01-26-2004, 05:38 PM
Nice. Care to share a rough outline of how it's done?

y6y6y6
01-26-2004, 06:20 PM
I have a loop that grabs a recordset from the database and then calls these two functions:

function draw_bar($height,$position,&$im,$text)
{

$blue1 = ImageColorAllocate($im, 102, 204, 255);
$blue2 = ImageColorAllocate($im, 0, 102, 204);
$blue3 = ImageColorAllocate($im, 0, 153, 255);

$offset = 25;
$base_x = 30;
$base_y = 200;
$angle = 4;
$width = 30;
$depth = 10;

$base_y += $position * $angle;
$base_x += $position * $offset;

add_bar_label($base_x+$width-$depth,$base_y-$height-10,$text,$im);

$left_points = array($base_x-$depth,$base_y-$angle, $base_x-$depth,$base_y-$angle-$height, $base_x,$base_y-$height, $base_x,$base_y);
imagefilledpolygon($im, $left_points, 4, $blue1);

$right_points = array($base_x,$base_y, $base_x,$base_y-$height, $base_x+$width,$base_y-$angle-$height, $base_x+$width,$base_y-$angle);
imagefilledpolygon($im, $right_points, 4, $blue3);

$top_points = array($base_x-$depth,$base_y-$angle-$height, $base_x,$base_y-$height, $base_x+$width,$base_y-$angle-$height, $base_x+$width-$depth,$base_y-($angle*2)-$height);
imagefilledpolygon($im, $top_points, 4, $blue2);
}


function add_bar_label($x,$y,$text,&$im,$text_rgb=255)
{
$text_color = ImageColorAllocate ($im, $text_rgb, $text_rgb, $text_rgb);
ImageStringUp($im, 2, $x, $y, $text, $text_color);
}

Viper007Bond
01-26-2004, 06:23 PM
Hmm, thanks. I'll mess around with that and have some fun. :)

y6y6y6
01-26-2004, 06:33 PM
Hmmmmm...... After reading through that it's not very clear. Here's the loop that calls the funtion. $total gets set earlier and is just the sum of the category column:

$i = 0;
$PixelsPerPercent = 2.7;
while($row = mysql_fetch_array($RecordSet)){
$name = $row['Category'];
$thecount = $row["ItemCount"];

$percent = number_format(($thecount / $total) * 100,1);
$height = number_format($percent*$PixelsPerPercent);

draw_bar($height,$i,$im,"$percent% - $name");
$i++;
}