Drawing Bar Graphs using GD/MySQL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Drawing Bar Graphs using GD/MySQL

Post by coool »

Hi..

I'm having problem in this code !

I beileve it's almost done, but just need a little fix !

here's the code:
image.php

Code: Select all

<?php
//already been connected to database

$sql = "SELECT Status FROM Table1 GROUP BY Status";
$x = mysql_query($sql) or die("Error - Could not $sql" . mysql_error());

$sql = "SELECT COUNT(Items) FROM Table1 GROUP BY Status";
$y = mysql_query($sql) or die("Error - Could not $sql" . mysql_error());

//x= sold,shipped,InStock,UnKnown
//y= 8,6,3,5

        $width = 300;
        $height = 200;

        header ("Content-type: image/png");

        $image = ImageCreate($width,$height) or die ("Cannot Create image");

        $white = ImageColorAllocate($image,255,255,255);
        $black = ImageColorAllocate($image,0,0,0);
        $red = ImageColorAllocate($image,255,0,0);
        $green = ImageColorAllocate($image,0,255,0);
        $blue = ImageColorAllocate($image,0,0,255);

        $maxY = 0;
        for ($i=0;$i<7;$i++){
          if ($y[$i] > $maxY)$maxY=$y[$i];
        }

        ImageRectangle($image,1,1,319,239,$black);

        imageLine($image,10,5,10,230,$blue);
        imageLine($image,10,230,300,230,$blue);

        ImageString($im,3,15,5,"CR ID",$black);
        ImageString($im,3,280,240,"Status",$black);
        ImageString($im,5,100,50,"Simple Graph",$red);
        ImageString($im,5,125,75,"by ..",$green);

        $xPos = 15;
        $yPos = 230;
        $barWidth = 20;
        $barHeight = 0;

        for ($i=0;$i<3;$i++){
          $barHeight = ($y[$i]/$maxY)* 100;
          imagerectangle($image,$xPos,$yPos,$xPos+$barWidth,($yPos-$barHeight),$red);
          imagestring( $image,2,$xPos-1,$yPos+1,$x[$i],$black);
          imagestring( $image,2,$xPos-1,$yPos+10,$y[$i],$black);
          $xPos += ($barWidth+20);
        }

        ImagePng($image, "graph.png", 90);
?>
page.php

Code: Select all

....
<img src="image.php"/>
....

Code: Select all

[b]Error:[/b] after excuting page.php
The image “http://websiteName.com/image.php” cannot be displayed, because it contains errors.
Last edited by coool on Mon Jul 30, 2007 10:51 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Comment out the lines

Code: Select all

header ("Content-type: image/png"); 
and

Code: Select all

ImagePng($image, "graph.png", 90);
Then try viewing the image. It should give you an idea of what the error is. If there's no error displayed add the following at the top of the script:

Code: Select all

    error_reporting(E_ALL);
    ini_set("error_reporting",TRUE);
    ini_set("display_errors", "on");
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

hmmm..

I've commented them out


Warning: imagestring(): supplied argument is not a valid Image resource
in these lines:

Code: Select all

ImageString($im,3,15,5,"CR ID",$black);
        ImageString($im,3,280,240,"Status",$black);
        ImageString($im,5,100,50,"Simple Graph",$red);
        ImageString($im,5,125,75,"by ..",$green);
I've fixed the mistake here by writing the full variable name -- it's $image NOT $im


Warning: Division by zero
in this line:

Code: Select all

$barHeight = ($y[$i]/$maxY)* 100;
could not fix this problem :( what's wrong !
how can i calculate the bar height ??
Last edited by coool on Mon Jul 30, 2007 10:52 am, edited 1 time in total.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

mysql_query doesn't return an array. Your code seems to think it does.
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

oh yeah i just noticed that

by testing the output of $maxY

Code: Select all

$maxY = 0;
        for ($i=0;$i<7;$i++){
          if ($y[$i] > $maxY)$maxY=$y[$i];
        echo $maxY;
        }
I'll check this out until the $maxY get the right answer

thanks alot for your help so far

please be with me until this code is solved :)
coool
Forum Commoner
Posts: 45
Joined: Wed Jul 11, 2007 5:51 pm

Post by coool »

oh I'm fine now, things are working perfectly

thanks :)
Post Reply