SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Graphing in PhP

  1. #1
    Old Folks 127.0.0.1 pacres's Avatar
    Join Date
    Sep 2003
    Location
    ed.ab.ca.na.ea
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Graphing in PhP

    I have heard that you can dynamically create graph images with php.

    My question is how would I go about getting started with this? I have no idea how it fits into php, but I would like to use it on a project I am working on. Any help that you can provide is appreciated.

  2. #2
    chown linux:users\ /world Hartmann's Avatar
    Join Date
    Aug 2000
    Location
    Houston, TX, USA
    Posts
    6,455
    Mentioned
    11 Post(s)
    Tagged
    0 Thread(s)
    The best place to start is probably:
    http://www.aditus.nu/jpgraph/index.php

    It is an object oriented library for drawing graphs using PHP. Take a look and see if it does what you need done.

  3. #3
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take a look at the JpGraph links in my signature
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  4. #4
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I can't help you with GD, because I'm not too certain about it myself.

    I do use another technique for barcharts, involving small images.

    imagine the set of data below....

    Code:
    Apples   -   76
    Oranges   -   23
    Bananas   -   1
    Basically, I set a maximum width (or height) for my chart (eg 400px), divide that figure by the total number of results (in this case, 100) giving me a component size of 4px.

    Now, by setting the width (or height) of a small image to 4 * the figure for that fruit, and outputting the image link.

    Put it all together, and you get...

    PHP Code:
    <?
    $chart_width 
    400;
    $fruits = array("apples"=>76,"oranges"=>23,"bananas"=>1);
    $fruittotal array_sum($fruits);
    $fruit_unit $chart_width $fruittotal;
    echo 
    "<table>";
    while(list(
    $k,$v) = each ($fruits)){
            
    $this_fruit_width $fruit_unit $v;
            echo 
    "<tr><td>$k</td><td><img src='http://www.entwickler.com/itr/polls/le/include/mainbar.gif' width='$this_fruit_width' height='7' border='0'></td></tr>";                                                                                                                                                     }                                                                                                                                                                                                                                                                                                                       echo "</table>";
    ?>
    See an example in the wild at..... http://www.php-mag.net/survey/pme/pme.html
    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  5. #5
    Old Folks 127.0.0.1 pacres's Avatar
    Join Date
    Sep 2003
    Location
    ed.ab.ca.na.ea
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks

    This helps to get me started. Right now we are just experimenting with things. the php-mag.net example looks like something we may be looking for, but the JP graphs might be a little nice for visual aspects.

  6. #6
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,236
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Chillijam
    I can't help you with GD, because I'm not too certain about it myself.

    I do use another technique for barcharts, involving small images.

    imagine the set of data below....

    Code:
    Apples   -   76
    Oranges   -   23
    Bananas   -   1
    Basically, I set a maximum width (or height) for my chart (eg 400px), divide that figure by the total number of results (in this case, 100) giving me a component size of 4px.

    Now, by setting the width (or height) of a small image to 4 * the figure for that fruit, and outputting the image link.

    Put it all together, and you get...
    ...snip...
    That must be a VERY slow graphing tool. You're generating literally hundreds of unnecessary table cells!

  7. #7
    SitePoint Addict Chillijam's Avatar
    Join Date
    Nov 2003
    Location
    England
    Posts
    293
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vgarcia
    That must be a VERY slow graphing tool. You're generating literally hundreds of unnecessary table cells!
    Erm... unless I coded it wrong, there are only 2 cells per records......
    The small image (1 * 7 px) is just resized (using width or height properties) depending on the value you want to represent.
    Your mind is like a parachute. It works best when open.
    (HH The Dalai Lama)

  8. #8
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is an example I wrote to do a simple bar graph WITHOUT any image at all!

    PHP Code:
    <?php
    /**
     * script to create a bar graph using only css
     *
     * @author      Jason E. Sweat
     * @since       2002-11-22
     * @version
     *      <pre>
     *      $Log: cssbar.php,v $
     *      Revision 1.1  2002/12/11 04:30:08  sweatje
     *      CSS bar chart application
     *
     *      </pre>
     */

    $width 200;
    $data = array('Yes' => 68'No' => 23'Maybe' => 12);

    $total 0;
    foreach(
    $data as $key => $value) {
      
    $total += $value;
    }
    foreach(
    $data as $key => $value) {
      
    $dataPct[$key]   = number_format($value/$total*1001).'%';
      
    $dataWidth[$key] = round($width*$value/$total0);
    }

    print <<<EOS
    <!DOCTYPE html
         PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>CSS Bar Graph</title>
    <style type="text/css">
    .layout {
      border:           blue solid 3px;
      border-spacing:   0px;
    }
    .layout td {
      padding:          5px 10px 5px 10px;
    }
    .border {
      border:           black solid 1px;
      width:            
    {$width}px;
    }
    .border td {
      padding:          0px 0px 0px 0px;
    }
    .barfill {
      text-align:       center;
      color:            rgb(255, 255, 0);
      font-weight:      bold;
      background-color: rgb(255, 0,   0);
    }
    </style>
    </head>
    <body>
    <table class="layout">
    EOS;

    foreach(
    $data as $key => $value) {
      print <<<EOS
    <tr>
      <td>
    $key</td>
      <td align="right">
    {$dataPct[$key]}</td>
      <td>
        <table class="border">
        <tr><td class="barfill" width="
    {$dataWidth[$key]}px">
        
    $value
        </td><td>&nbsp;</td></tr></table>
      </td>
    </tr>
    EOS;
    }

    print 
    "</table>\n</body>\n</html>\n";

    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •