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
Reply
 
LinkBack (5) Thread Tools Search this Thread Display Modes
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)
Old 12-05-2007, 07:35 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I've never actually tried to do a bar chart in PHP before! But if it's so easy then I sure will be adding it when I think the website would suit one. I always imagined it'd be rather difficult and that you'd need a 3rd party tool to create them. I forget its name, but there is one out there that does this all for you.

I was messing about with the GD library only the other day, so maybe I should keep going! It encouraged my to look deeper into it after I realised you could apply all sorts of fancy effects to your pictures, and I also found out how to take a screenshot of a website using GD and the Windows COM component.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-05-2007, 07:46 PM   #3 (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

Yes i have been trying to figure out how to take screenshots of websites. I found out about the screengrab functions PHP has to offer well the latest version. I cant manage to get them to work maybe you can enlighten me on the subject?

PHP Code:
imagegrabscreen();
imagegrabwindow(); 
PHP Code:
<?php
$im 
imagegrabscreen();
imagepng($im"myscreenshot.png");
?>
PHP Code:
<?php
$browser 
= new COM("InternetExplorer.Application");
$handle $browser->HWND;
$browser->Visible true;
$browser->Navigate("http://www.libgd.org");

/* Still working? */
while ($browser->Busy) {
    
com_message_pump(4000);
}
$im imagegrabwindow($handle0);
$browser->Quit();
imagepng($im"iesnap.png");
?>
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-05-2007, 07:53 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Do you get a black screen? If so then you'll need to open up services.msc, go to properties for the Apache service, click the "Log On" tab then tick the box "Allow service to interact with desktop".
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
codefreek (07-03-2008), Rendair (12-05-2007)
Old 12-05-2007, 07:54 PM   #5 (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

Thank you! Works
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-05-2007, 10:40 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

They should tell you about that on the php.net website. If you could do back-links I might have actually posted, but as you can't, sod them!
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-07-2007, 06:02 PM   #7 (permalink)
Nor
The Addict
 
Join Date: Nov 2007
Posts: 282
Thanks: 61
Nor is on a distinguished road
Default

Screen shots are only for windows though...correct me if I'm wrong.
__________________
PHP/XHTML Freelancer:
Cleanscript.com v3 - Programming starting at just $5 act now!
Nor is offline  
Reply With Quote
Old 12-07-2007, 06:29 PM   #8 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Yea. Unfortunately the COM component is only available on Windows. Although maybe it'll work if you have Wine installed? I'm not sure as I'm not into Linux that much, but it's just an idea.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 12-07-2007, 10:18 PM   #9 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Haven't ran into that specific problem before so it shouldn't pose a problem. We kinda use the same charts (this one is a little bit more degraded AND advanced at the same time) but no errors yet.

PHP version?
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 12-09-2007, 06:58 PM   #10 (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

I believe version 4 - 5
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-11-2007, 04:47 PM   #11 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 165
Thanks: 0
bluesaga is on a distinguished road
Default

There is a nice graph extension, namely JPGraph (http://www.aditus.nu/jpgraph/)
__________________
Halo 3 Cheats
bluesaga is offline  
Reply With Quote
Old 12-11-2007, 06:01 PM   #12 (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

Yes i have heard of this..tis very good..
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-11-2007, 11:00 PM   #13 (permalink)
The Wanderer
 
Join Date: Dec 2007
Location: The Netherlands
Posts: 13
Thanks: 3
Jelmer is on a distinguished road
Default

Although I've never tried the technique you use here to create a single image with php only, wouldn't it be better and easier to use html and some css to set the height of the bars for example?? It would be more accessible since people can still view the data because the numbers are displayed as html text instead of an image.

Thanks for the tutorial though, I'm not that experienced with php and might use this to add some text to thumbnail images. I'm wondering if it's possible to adjust the opacity of a certain rectangle you're creating for example, any ideas?
Jelmer is offline  
Reply With Quote
Old 07-03-2008, 10:19 PM   #14 (permalink)
Dal
The Visitor
 
Dal's Avatar
 
Join Date: Jul 2008
Posts: 4
Thanks: 5
Dal is on a distinguished road
Bug

I was just looking through google about this function imagegrabscreen() and I thought Id sign up on this forum to mention that there are potential security risks in activating the "Allow service to interact with desktop". As far as Im aware this is only available for windows servers and since I only have access to a shared Unix server from my host I cant test the theory. However M$ do advise strongly that this service never be used!

Id rather mention it than not as I was quite excited at the idea of being able to capture the screens of visitors... Then I thought some more... isnt this a break in some privacy law (UK / US)???

Anyway, cant test it so unsure on what exactly can be produced.

Kind regards
Dal
PS: didnt this thread start of with something completely different???
Dal is offline  
Reply With Quote
Old 08-12-2008, 02:26 PM   #15 (permalink)
The Visitor
 
Join Date: Aug 2008
Posts: 1
Thanks: 0
rizwan6feb is on a distinguished road
Default

Check this out, very nice tutorial on drawing bar graph with php
How to create bar graph in PHP with dynamic scaling
rizwan6feb is offline  
Reply With Quote
Old 05-25-2009, 01:34 PM   #16 (permalink)
The Wanderer
 
Join Date: May 2009
Posts: 6
Thanks: 0
RishikeshJha is on a distinguished road
Default bar graph is not working in any html tags?

hi when i m using the above given script in any html tag or even i m giving any html tag above or below this code... the functionality of script stops. a garbage value is seen...

i hv to put this bargaraph in a html page which contain some other data alos..

what to do...

plz give me soloution
RishikeshJha is offline  
Reply With Quote
Old 05-25-2009, 03:37 PM   #17 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by RishikeshJha View Post
plz give me soloution
No. If you really want to be helped, ask questions. If you really just want a solution handed to you on a plate, without knowing what mess you are already making, go hire a freelancer to start again from scratch.
Salathe is offline  
Reply With Quote
Old 05-26-2009, 04:56 AM   #18 (permalink)
The Wanderer
 
Join Date: May 2009
Posts: 6
Thanks: 0
RishikeshJha is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
No. If you really want to be helped, ask questions. If you really just want a solution handed to you on a plate, without knowing what mess you are already making, go hire a freelancer to start again from scratch.
Sorry if my words hurted u. actually i hv made different file for this code. when i m including it the image is not showing. even when i put any tag of html before or after this code image disappers in ie and in mozilla it returns garbage value. i need ur help

plz....

thanks
RishikeshJha is offline  
Reply With Quote
Old 05-26-2009, 09:26 AM   #19 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

We can't help you without knowing the problem. A short paragraph saying "it doesn't work!" is no use at all. Give us a live example of the problem, turn on error reporting, show us source code.
Salathe is offline  
Reply With Quote
Old 05-26-2009, 09:35 AM   #20 (permalink)
The Wanderer
 
Join Date: May 2009
Posts: 6
Thanks: 0
RishikeshJha is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
We can't help you without knowing the problem. A short paragraph saying "it doesn't work!" is no use at all. Give us a live example of the problem, turn on error reporting, show us source code.
ok

thanks
RishikeshJha is offline  
Reply With Quote
Reply


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:08 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