Drawing bar graph using GD and PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gubbachchi
    New Member
    • Jan 2008
    • 59

    Drawing bar graph using GD and PHP

    Hi all,

    I have got a problem with plotting bar graph using GD. I am learning GD now.

    I have written the code for basic bar graph with one bar. What I need is, scale on Y-axis i.e. divisions on y-axis like 500,1000,1500 upto value which I have mentioned in $max variable which in my case is 4000.
    Here is the code

    Code:
    <?php
      
    header("Content-type: image/jpeg");
                   
    $data = '1000';
    $max = '4000';
            
    $height = 255;
    $width = 320;
            
    $im = imagecreate($width,$height); // width , height px
    
    $white = imagecolorallocate($im,255,255,255); 
    $black = imagecolorallocate($im,0,0,0);   
    $red = imagecolorallocate($im,255,0,0);   
    
    imageline($im, 10, 5, 10, 230, $black);
    imageline($im, 10, 230, 300, 230, $black);
        
    $x = 15;   
    $y = 230;   
    $x_width = 20;  
    $y_ht = 0; 
           
    $y_ht = ($data/$max)* $height;  
               
    imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
    imagestring( $im,2,$x-1,$y+10,$data,$black);
                  
    $x += ($x_width+20);  
             
    imagejpeg($im);
    
    ?>
    Please give me some idea regarding this.

    With regards
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, gubbachchi.

    Could you be a little more specific?

    What is your code supposed to do that it's not doing?
    What is your code *not* supposed to do that it is doing?

    Comment

    • gubbachchi
      New Member
      • Jan 2008
      • 59

      #3
      Originally posted by pbmods
      Heya, gubbachchi.

      Could you be a little more specific?

      What is your code supposed to do that it's not doing?
      What is your code *not* supposed to do that it is doing?

      Hi,

      Thanks for your reply.
      Actually what I need is, I need to mark divisions on X-axis and y-axis.
      That is on x-axis i need divisions of 10,20,30,40 and on y-axis 500,1000,1500,2 000,2500 to be marked on horizontal and vertical axis to be marked. I hope you got the point now. Can you please help me out.

      With regards

      Comment

      • coolsti
        Contributor
        • Mar 2008
        • 310

        #4
        Take a look here:

        http://www.phpclasses. org/browse/file/6059.html

        This is what I found when I had to implement bargraphs in my application. I use it pretty much like it is with some minor modifications to suit my needs. Even if you do not use this and rather make your own, the code is not difficult to understand and is a great "learn by example", which is how I always learn something new in programming.

        Comment

        • dlite922
          Recognized Expert Top Contributor
          • Dec 2007
          • 1586

          #5
          Originally posted by gubbachchi
          Hi all,

          I have got a problem with plotting bar graph using GD. I am learning GD now.

          I have written the code for basic bar graph with one bar. What I need is, scale on Y-axis i.e. divisions on y-axis like 500,1000,1500 upto value which I have mentioned in $max variable which in my case is 4000.
          Here is the code

          Code:
          <?php
            
          header("Content-type: image/jpeg");
                         
          $data = '1000';
          $max = '4000';
                  
          $height = 255;
          $width = 320;
                  
          $im = imagecreate($width,$height); // width , height px
          
          $white = imagecolorallocate($im,255,255,255); 
          $black = imagecolorallocate($im,0,0,0);   
          $red = imagecolorallocate($im,255,0,0);   
          
          imageline($im, 10, 5, 10, 230, $black);
          imageline($im, 10, 230, 300, 230, $black);
              
          $x = 15;   
          $y = 230;   
          $x_width = 20;  
          $y_ht = 0; 
                 
          $y_ht = ($data/$max)* $height;  
                     
          imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
          imagestring( $im,2,$x-1,$y+10,$data,$black);
                        
          $x += ($x_width+20);  
                   
          imagejpeg($im);
          
          ?>
          Please give me some idea regarding this.

          With regards

          Why not do this with HTML and CSS.

          Do a forloop for each bar, and the heigh of each bar is the style.height = $height.

          Please note that's not suppose to be actual code*

          Pretty nifty stuff if you use AJAX!

          -Dan

          Comment

          • gubbachchi
            New Member
            • Jan 2008
            • 59

            #6
            Thanks dlite922 for your reply.

            But I need to do this using GD only, this is requirement of the project.

            With regards

            Comment

            • rizwan6feb
              New Member
              • Jul 2007
              • 108

              #7
              Have a look at this

              Comment

              Working...