Graph using PHP GD library

Discussion in 'PHP' started by pradeep, Jun 3, 2005.

  1. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Say if you want to generate graph on your website using data from a data base, you might be wondering how to go about it. Well the best way I would do it is use free PHP GD library.

    Here is a class i wrote to generate a graph in a PNG image for me.

    PHP:
    class BarGraph
    {
        var 
    $barWidth;
        var 
    $imgHeight=400;
        var 
    $imgWidth=600;
        var 
    $bgColor,$barColor;
        var 
    $barPadding;
        var 
    $data,$rangeMax=10;
        var 
    $im;

        function 
    init() /* initializes the image */
        
    {
            
    $this->im=imagecreate($this->imgWidth,$this->imgHeight);
        }

        function 
    setHeightWidth($h,$w/** sets the hieght and with of the image **/
        
    {
            
    $this->imgHeight=$h;
            
    $this->imgWidth=$w;
        }

        function 
    setBarWidth($width/* sets the bar width */
        
    {
            
    $this->barWidth=$width;
        }

        function 
    setBarPadding($padding/* sets the bar padding */
        
    {
            
    $this->barPadding=$padding;
        }

        function 
    setMax($max/* sets the maximum posible value in the data set */
        
    {
            
    $this->rangeMax=$max;
        }

        function 
    loadData($data/* load data, the input shud be an array */
        
    {
            
    $this->data=$data;
        }

        function 
    setBgColor($r,$g,$b/* sets the background color of the image */
        
    {
            
    $this->bgColor=imagecolorallocate($this->im,$r,$g,$b);
        }

        function 
    setBarColor($r,$g,$b/* sets the bar color of the image */
        
    {
            
    $this->barColor=imagecolorallocate($this->im,$r,$g,$b);
        }

        function 
    drawGraph($flag=0/* to draw graphs on the image */
        
    {
            if(
    $flag/* flag set to 1 to draw the second bar **/
            
    {
                
    $t=$this->barWidth+$this->barPadding;
            }
            else 
    /* else draws the first bar set */
            
    {
                
    imagefilledrectangle($this->im,0,0,$this->imgWidth,$this->imgHeight,$this->bgColor);
                
    $t=0;
            }

            for ( 
    $mon $mon count($this->data) ; $mon ++ ) 
            {
        
                
    $X = (($this->imgWidth/count($this->data))*$mon) + $this->barPadding $t;
                
    $Y = (10 $this->data[$mon])*($this->imgHeight/$this->rangeMax);
                
    $X1 = ($X $this->barWidth);
                
    $Y1 $this->imgHeight;

                
    imagefilledrectangle($this->im,$X,$Y,$X1,$Y1,$this->barColor);
            }
        }

        function 
    renderImage() /* creates the image & sends in to the browser */
        
    {
            
    header("Content-Type: image/png");
            
    imagepng($this->im);
        }
    }
    You can refer to all the image functions used here.

    Piece of sample code:

    PHP:
    include("bargraph.class.php");

    $pr=array(3,6,7,4,5,9,4,5,4,2,1,8);
    $p=$pr;
    shuffle($p);

    $g=new BarGraph;
    $g->setHeightWidth(400,600);
    $g->init();
    $g-setMax(10); //maximum data possible in the data set
    $g->setBarWidth(10);
    $g->setBarPadding(10);
    $g->setBarColor(255,130,130);
    $g->setBgColor(204,204,204);
    $g->loadData($pr);
    $g->drawGraph();

    $g->loadData($p);
    $g->setBarColor(130,130,255);
    $g->drawGraph(1);

    $g->renderImage();
    Example Output:

    [​IMG]

    Enjoy using this,
    Do let me know if you have any queries or suggestions.
     
  2. coool

    coool New Member

    Joined:
    Jul 12, 2007
    Messages:
    46
    Likes Received:
    0
    Trophy Points:
    0
    Hi

    I've tried your code and it's working perfect

    but there're many imp. things are missing !

    you can see what kind of graphs in my mind by looking at the attached picture :)
     

    Attached Files:

  3. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
  4. gkumar

    gkumar New Member

    Joined:
    Jun 16, 2009
    Messages:
    58
    Likes Received:
    5
    Trophy Points:
    0
    In this example we are going to draw a simple Bar graph. Take a school for exmaple. We are going to draw a graph for week days and the total number of student that come to school.

    First of all we declare two arrays. The first for the student and the second for week days. Then we calculate the maximal number of student. On the basis of that number we are going to calculate the percentage length of the other bar graphs.

    Code:
    <html>
    <head>
    <meta http-equiv="Content-Language" content="en-us">
    <title>Simple Bar Graph .. by viji patil</title>
    </head>
    <body>
    <?php
       
            // read the post data
            $data = array('100','200','300','400','500','350','270');
            $x_fld = array('Sun','Mon','Tue','Wen','Thu','Fir','Sat');
            $max = 0;
            for ($i=0;$i<7;$i++){
              if ($data[$i] > $max)$max=$data[$i];  // find the largest data
            }
            $im = imagecreate(320,255); // width , height px
    
            $white = imagecolorallocate($im,255,255,255); // allocate some color from RGB components remeber Physics
            $black = imagecolorallocate($im,0,0,0);   //
            $red = imagecolorallocate($im,255,0,0);   //
            $green = imagecolorallocate($im,0,255,0); //
            $blue = imagecolorallocate($im,0,0,255);  //
            //
            // create background box
            //imagerectangle($im, 1, 1, 319, 239, $black);
            //draw X, Y Co-Ordinate
            imageline($im, 10, 5, 10, 230, $blue );
            imageline($im, 10, 230, 300, 230, $blue );
            //Print X, Y
            imagestring($im,3,15,5,"Students",$black);
            imagestring($im,3,280,240,"Days",$black);
            imagestring($im,5,100,50,"Simple Graph",$red);
             imagestring($im,5,125,75,"by Vijit",$green);
    
            // what next draw the bars
            $x = 15;    // bar x1 position
            $y = 230;    // bar $y1 position
            $x_width = 20;  // width of bars
            $y_ht = 0; // height of bars, will be calculated later
            // get into some meat now, cheese for vegetarians;
            for ($i=0;$i<7;$i++){
              $y_ht = ($data[$i]/$max)* 100;    // no validation so check if $max = 0 later;
              imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
              imagestring( $im,2,$x-1,$y+1,$x_fld[$i],$black);
              imagestring( $im,2,$x-1,$y+10,$data[$i],$black);
              $x += ($x_width+20);   // 20 is diff between two bars;
             
            }
            imagejpeg( $im, "graph.jpeg", 90);
            imagedestroy($im);
            echo "<img src='graph.jpeg'><p></p>";
     
    ?>
    </body>
    </html>
    
     
  5. kevin.haghighat

    kevin.haghighat New Member

    Joined:
    Apr 13, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Warning: imagejpeg(): Unable to open 'graph.jpeg' for writing in /test.php on line 49

    What is the image graph.jpeg supposed to be?

    Kevin.

     
  6. johnny.dacu

    johnny.dacu New Member

    Joined:
    Jul 6, 2010
    Messages:
    88
    Likes Received:
    4
    Trophy Points:
    0
    Not bad.. still it's more for learning. i don't think GD was planned for something like this. PHP shoud just return data, drawing or bulding graphs shoud be done on cliend side (with flash or js)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice