TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
 
 
LinkBack (5) Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 12-05-2007, 05:50 PM   5 links from elsewhere to this Post. Click to view. #1 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default Bar Chart in PHP

Yes its that time again another chart tutorial. This one is going to be a simple bar chart

You can view a demo HERE

Firstly we are going to set a header type as image

PHP Code:
 header("Content-type: image/jpeg"); 
Now we are going to set some values in an array to use and then all them all together
PHP Code:

$data 
= array('3400','2570','245','473','1000','3456','780');
$sum array_sum($data); 
Now we need to set the height and width of the actual chart itself.

PHP Code:
$height 230;
$width 320
For the sake of this tutorial do not edit this. Otherwise this may cause errors.

Now we can create the actual chart background or where the chart is actually shown on. However you want to look at it.

PHP Code:
$im imagecreate($width,$height); // width , height px 

Now we can set the background colour of the graph and also create some colours we are going to use for the bars and the lines.

PHP Code:
$white imagecolorallocate($im,255,255,255); 
$black imagecolorallocate($im,0,0,0);   
$red imagecolorallocate($im,255,0,0); 
Now its time to create the X & Y axis lines.

PHP Code:

imageline
($im10510230$black );
imageline($im10230300230$black); 
PHP Code:
imageline  resource image,x-coordinate first point,y-coordinate first point,x-coordinate second point,y-coordinate second point,colour 
Now we need to set up some information so we can place these bars.

PHP Code:

$x 
15// set how far from the side then how far from next bar
$y 230// set where bar should reach on the y in this case on the y line we created
$x_width 20// Width of each of the bars 
$y_ht 0//height of the bars 
Now we can start creating the bars themself.

PHP Code:
for ($i=0;$i<7;$i++)
{
        
  
$y_ht = ($data[$i]/$sum)* $height// work out height and make sure they don't go larger then the image    

     
imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
     
imagestring$im,2,$x-1,$y+10,$data[$i],$black);
              
  
$x += ($x_width+20); // set the new distance for the next bar 
         

Now we can display the final results

PHP Code:
imagejpeg($im); 
This bar chart can easily be upgraded and i will be in the next advance tutorial on bar graphs. Stay tuned.

Full Code
PHP Code:
<?php
  
           header
("Content-type: image/jpeg");
         
        
// read the post data
        
$data = array('3400','2570','245','473','1000','3456','780');
        
$sum array_sum($data);
        
        
$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($im10510230$black);
        
imageline($im10230300230$black);
    

        
$x 15;   
        
$y 230;   
        
$x_width 20;  
        
$y_ht 0
       
        for (
$i=0;$i<7;$i++){
        
          
$y_ht = ($data[$i]/$sum)* $height;    
          
              
imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
              
imagestring$im,2,$x-1,$y+10,$data[$i],$black);
              
          
$x += ($x_width+20);  
         
        }
        
        
imagejpeg($im);

?>
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
The Following 5 Users Say Thank You to Rendair For This Useful Post:
codefreek (07-03-2008), ETbyrne (08-12-2008), Karl (12-05-2007), Nor (12-11-2007), Wildhoney (12-05-2007)
 


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/advanced-php-programming/1629-bar-chart-php.html
Posted By For Type Date
Datuve :: Forums :: Kaa lapaa dinamiski izveidot diagramu vai grafiku? This thread Refback 01-03-2008 05:46 PM
Drawing anti-aliased lines using the GD library - PHP This thread Refback 12-31-2007 02:22 AM
Drawing anti-aliased lines using the GD library - Object Mix This thread Refback 12-30-2007 03:45 AM
Drawing anti-aliased lines using the GD library - alt.comp.lang.php | Google Groups This thread Refback 12-29-2007 04:50 AM
Drawing anti-aliased lines using the GD library - Usenet Forums This thread Refback 12-23-2007 06:53 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:21 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design