 |
Account Login
|
 |
 |
Latest Articles
|
 |
 |
IRC Channel
|
 |
 |
Associates
|
 |
 |
Associates
|
 |
|
 |
|
 |
|
 |
12-05-2007, 05:50 PM
|
#1 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
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($im, 10, 5, 10, 230, $black ); imageline($im, 10, 230, 300, 230, $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($im, 10, 5, 10, 230, $black); imageline($im, 10, 230, 300, 230, $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);
?>
|
|
|
|
The Following 5 Users Say Thank You to Rendair For This Useful Post:
|
|
12-05-2007, 07:35 PM
|
#2 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-05-2007, 07:46 PM
|
#3 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
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($handle, 0);
$browser->Quit();
imagepng($im, "iesnap.png");
?>
|
|
|
12-05-2007, 07:53 PM
|
#4 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
|
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
|
|
12-05-2007, 07:54 PM
|
#5 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
Thank you!  Works
|
|
|
12-05-2007, 10:40 PM
|
#6 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-07-2007, 06:02 PM
|
#7 (permalink)
|
|
The Addict
Join Date: Nov 2007
Posts: 282
Thanks: 61
|
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!
|
|
|
|
12-07-2007, 06:29 PM
|
#8 (permalink)
|
|
La Vida es Sueño
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
|
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.
|
|
|
12-07-2007, 10:18 PM
|
#9 (permalink)
|
|
The Frequenter
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
|
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?
|
|
|
12-09-2007, 06:58 PM
|
#10 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
I believe version 4 - 5
|
|
|
12-11-2007, 06:01 PM
|
#12 (permalink)
|
|
The Addict
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
|
Yes i have heard of this..tis very good..
|
|
|
12-11-2007, 11:00 PM
|
#13 (permalink)
|
|
The Wanderer
Join Date: Dec 2007
Location: The Netherlands
Posts: 13
Thanks: 3
|
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?
|
|
|
|
07-03-2008, 10:19 PM
|
#14 (permalink)
|
|
The Visitor
Join Date: Jul 2008
Posts: 4
Thanks: 5
|
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??? 
|
|
|
|
05-25-2009, 01:34 PM
|
#16 (permalink)
|
|
The Wanderer
Join Date: May 2009
Posts: 6
Thanks: 0
|
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
|
|
|
|
05-25-2009, 03:37 PM
|
#17 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by RishikeshJha
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.
|
|
|
|
05-26-2009, 04:56 AM
|
#18 (permalink)
|
|
The Wanderer
Join Date: May 2009
Posts: 6
Thanks: 0
|
Quote:
Originally Posted by Salathe
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
|
|
|
|
05-26-2009, 09:26 AM
|
#19 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
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.
|
|
|
|
05-26-2009, 09:35 AM
|
#20 (permalink)
|
|
The Wanderer
Join Date: May 2009
Posts: 6
Thanks: 0
|
Quote:
Originally Posted by Salathe
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
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|