1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Graph using PHP GD library

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

  1. pradeep

    pradeep Team Leader

    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.

    class BarGraph

    init() /* initializes the image */

    setHeightWidth($h,$w/** sets the hieght and with of the image **/

    setBarWidth($width/* sets the bar width */

    setBarPadding($padding/* sets the bar padding */

    setMax($max/* sets the maximum posible value in the data set */

    loadData($data/* load data, the input shud be an array */

    setBgColor($r,$g,$b/* sets the background color of the image */

    setBarColor($r,$g,$b/* sets the bar color of the image */

    drawGraph($flag=0/* to draw graphs on the image */
    $flag/* flag set to 1 to draw the second bar **/
    /* else draws the first bar set */

            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;


    renderImage() /* creates the image & sends in to the browser */
    header("Content-Type: image/png");
    You can refer to all the image functions used here.

    Piece of sample code:



    $g=new BarGraph;
    $g-setMax(10); //maximum data possible in the data set


    Example Output:


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

    coool New Member


    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

  4. gkumar

    gkumar New Member

    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.

    <meta http-equiv="Content-Language" content="en-us">
    <title>Simple Bar Graph .. by viji patil</title>
            // 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,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;
              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);
            echo "<img src='graph.jpeg'><p></p>";
  5. kevin.haghighat

    kevin.haghighat New Member

    Warning: imagejpeg(): Unable to open 'graph.jpeg' for writing in /test.php on line 49

    What is the image graph.jpeg supposed to be?


  6. johnny.dacu

    johnny.dacu New Member

    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