473,326 Members | 2,438 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Drawing bar graph using GD and PHP

Hi all,

I have got a problem with plotting bar graph using GD. I am learning GD now.

I have written the code for basic bar graph with one bar. What I need is, scale on Y-axis i.e. divisions on y-axis like 500,1000,1500 upto value which I have mentioned in $max variable which in my case is 4000.
Here is the code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. header("Content-type: image/jpeg");
  4.  
  5. $data = '1000';
  6. $max = '4000';
  7.  
  8. $height = 255;
  9. $width = 320;
  10.  
  11. $im = imagecreate($width,$height); // width , height px
  12.  
  13. $white = imagecolorallocate($im,255,255,255); 
  14. $black = imagecolorallocate($im,0,0,0);   
  15. $red = imagecolorallocate($im,255,0,0);   
  16.  
  17. imageline($im, 10, 5, 10, 230, $black);
  18. imageline($im, 10, 230, 300, 230, $black);
  19.  
  20. $x = 15;   
  21. $y = 230;   
  22. $x_width = 20;  
  23. $y_ht = 0; 
  24.  
  25. $y_ht = ($data/$max)* $height;  
  26.  
  27. imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
  28. imagestring( $im,2,$x-1,$y+10,$data,$black);
  29.  
  30. $x += ($x_width+20);  
  31.  
  32. imagejpeg($im);
  33.  
  34. ?> 
Please give me some idea regarding this.

With regards
May 29 '08 #1
6 2573
pbmods
5,821 Expert 4TB
Heya, gubbachchi.

Could you be a little more specific?

What is your code supposed to do that it's not doing?
What is your code *not* supposed to do that it is doing?
May 30 '08 #2
Heya, gubbachchi.

Could you be a little more specific?

What is your code supposed to do that it's not doing?
What is your code *not* supposed to do that it is doing?

Hi,

Thanks for your reply.
Actually what I need is, I need to mark divisions on X-axis and y-axis.
That is on x-axis i need divisions of 10,20,30,40 and on y-axis 500,1000,1500,2000,2500 to be marked on horizontal and vertical axis to be marked. I hope you got the point now. Can you please help me out.

With regards
May 30 '08 #3
coolsti
310 100+
Take a look here:

http://www.phpclasses.org/browse/file/6059.html

This is what I found when I had to implement bargraphs in my application. I use it pretty much like it is with some minor modifications to suit my needs. Even if you do not use this and rather make your own, the code is not difficult to understand and is a great "learn by example", which is how I always learn something new in programming.
May 30 '08 #4
dlite922
1,584 Expert 1GB
Hi all,

I have got a problem with plotting bar graph using GD. I am learning GD now.

I have written the code for basic bar graph with one bar. What I need is, scale on Y-axis i.e. divisions on y-axis like 500,1000,1500 upto value which I have mentioned in $max variable which in my case is 4000.
Here is the code

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. header("Content-type: image/jpeg");
  4.  
  5. $data = '1000';
  6. $max = '4000';
  7.  
  8. $height = 255;
  9. $width = 320;
  10.  
  11. $im = imagecreate($width,$height); // width , height px
  12.  
  13. $white = imagecolorallocate($im,255,255,255); 
  14. $black = imagecolorallocate($im,0,0,0);   
  15. $red = imagecolorallocate($im,255,0,0);   
  16.  
  17. imageline($im, 10, 5, 10, 230, $black);
  18. imageline($im, 10, 230, 300, 230, $black);
  19.  
  20. $x = 15;   
  21. $y = 230;   
  22. $x_width = 20;  
  23. $y_ht = 0; 
  24.  
  25. $y_ht = ($data/$max)* $height;  
  26.  
  27. imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
  28. imagestring( $im,2,$x-1,$y+10,$data,$black);
  29.  
  30. $x += ($x_width+20);  
  31.  
  32. imagejpeg($im);
  33.  
  34. ?> 
Please give me some idea regarding this.

With regards

Why not do this with HTML and CSS.

Do a forloop for each bar, and the heigh of each bar is the style.height = $height.

Please note that's not suppose to be actual code*

Pretty nifty stuff if you use AJAX!

-Dan
May 31 '08 #5
Thanks dlite922 for your reply.

But I need to do this using GD only, this is requirement of the project.

With regards
May 31 '08 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: TheDD | last post by:
Hi all, i have a question: is there any librairy for drawing graphs with PHP? Not graphs like histogram or alike, but like a set of shapes linked together in a certain way (like a flow chart...
0
by: SS KANAGAL | last post by:
Hello, I am drawing a bar graph in an aspx page using System.Drawing namespace . Now I want to provide drill down facility for the graph i.e. on click of a bar in the graph it should get me yet...
0
by: SS KANAGAL | last post by:
Hello, I am drawing a bar graph in an aspx page using System.Drawing namespace . Now I want to provide drill down facility for the graph i.e. on click of a bar in the graph it should get me yet...
2
by: Senne Vaeyens | last post by:
Hi, I'm writing an app that will show a scrolling transparent graph and I'm ancountering some problems, explained in the following code: (a VS project explaining the problem(s) can be downloaded...
1
by: Anupam Soni CMC Ltd. | last post by:
Hi All, I have drawn Pie/Bar using some values now I want to capture any segment of Pie or Block of Bar graph if user click on that area. Plz suggest how can I do that . I have to show...
1
by: DBC User | last post by:
Hi Sharpies, This is a very basic question. I am using .Net IDE 2003 and System.Drawing.dll (1.1.4322). I wrote a simple program like this (ommitting the commons) using System.Drawing; .....
1
by: anthonyroche | last post by:
Does anyone know of a product that allows a user to draw a line on a chart? eg A historical stock tracker would have a line graph of say Aug 03 to Dec 03, which is a line graph representing a...
1
by: Bad_Kid | last post by:
(c++) I have to present a wave which is stored in a file (int numbers, I know sample's freq) on some kind of graph. I've never been using drawing in c++. Any suggestions, the easiest way to do...
1
by: RichMo | last post by:
Hi, I'm new to C++ and i can't seem to find a tutorial on the net for this one. I have a dialog based MFC app running that reads sensors using a USB interface device. At the moment the...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.