TalkPHP
 
 
Account Login
Latest Articles
» How to keep your forms from double posting data
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (5) Thread Tools Display Modes
Old 12-05-2007, 04: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: 296
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.mypubquiz.co.uk - The Quiz Community - NEW QUIZZES
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, 06:35 PM   #2 (permalink)
TalkPHP Loves You
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Nottingham
Posts: 1,448
Thanks: 72
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, 06:46 PM   #3 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 296
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.mypubquiz.co.uk - The Quiz Community - NEW QUIZZES
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-05-2007, 06:53 PM   #4 (permalink)
TalkPHP Loves You
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Nottingham
Posts: 1,448
Thanks: 72
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, 06:54 PM   #5 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 296
Thanks: 18
Rendair is on a distinguished road
Default

Thank you! Works
__________________
www.mypubquiz.co.uk - The Quiz Community - NEW QUIZZES
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-05-2007, 09:40 PM   #6 (permalink)
TalkPHP Loves You
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Nottingham
Posts: 1,448
Thanks: 72
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, 05: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, 05:29 PM   #8 (permalink)
TalkPHP Loves You
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Nottingham
Posts: 1,448
Thanks: 72
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, 09:18 PM   #9 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 399
Thanks: 47
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, 05:58 PM   #10 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 296
Thanks: 18
Rendair is on a distinguished road
Default

I believe version 4 - 5
__________________
www.mypubquiz.co.uk - The Quiz Community - NEW QUIZZES
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-11-2007, 03:47 PM   #11 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 164
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, 05:01 PM   #12 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 296
Thanks: 18
Rendair is on a distinguished road
Default

Yes i have heard of this..tis very good..
__________________
www.mypubquiz.co.uk - The Quiz Community - NEW QUIZZES
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-11-2007, 10: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
Reply