1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Drawing Bar Graphs using GD/MySQL

Discussion in 'PHP' started by coool, Jul 30, 2007.

  1. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    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
    PHP:
    <?php 
    //already been connected to database 

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

    $sql "SELECT COUNT(Items) FROM Table1 GROUP BY Status"
    $y mysql_query($sql) or die("Error - Could not $sqlmysql_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 Lina",$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:
     
    .... 
    <img src="image.php"/> 
    .... 
    
    Error: after excuting page.php
    The image “http://websiteName.com/image.php” cannot be displayed, because it contains errors.
     
  2. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,646
    Likes Received:
    86
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    Please refer to the PHP Manual!
    ImagePng($image, "graph.png", 90);

    The imagepng() outputs a GD image stream (image) in PNG format to standard output (usually the browser) or, if a filename is given by the filename it outputs the image to the file.
     
  3. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    Things are working perfectly

    Thanks :)
     

Share This Page