473,749 Members | 2,384 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Drawing bar graph using GD and PHP

59 New Member
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 2593
pbmods
5,821 Recognized Expert Expert
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
gubbachchi
59 New Member
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,2 000,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 Contributor
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 Recognized Expert Top Contributor
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
gubbachchi
59 New Member
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
rizwan6feb
108 New Member
Have a look at this
http://www.qualitycodes.com/tutorial.php?articleid=20
Sep 17 '08 #7

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

Similar topics

6
11919
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 or call graph). I have found a script to handle the graph in memory but i can already do that, my problem is to draw it (it an optimal way, and without crossing
0
1662
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 another graph with different data populated in that graph. I want to know how to provide href for each bar in the graph in the code behind.
0
1360
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 another graph with different data populated in that graph. I want to know how to provide href for each bar in the graph in the code behind.
2
1366
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 at http://www.icat.be/problems.zip ) The code is executed on a timer.tick event: // Draw Using Double Buffering, this works perfectly if the monitor
1
2756
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 trendline of that particular segment in new form . Plz help me ....
1
3630
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
1639
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 stock over that period. I want to give the user the functionality to draw a line on the graph (Straight line) representing what they consider the trend. All web based.. Any information would be of great help, I have already the component that does...
1
1605
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 that? thanx...
1
1443
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 varying data just moves a progress bar up and down. I'm looking for a way to display this sensor data as a graph on the
9
3181
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 want to be able to draw lines in a picturebox based upon certain data points I have received. I dragged a picturebox from the toolbar onto my form, but after having gone through the help files, looking online and trying a variety of things, I...
0
9566
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9333
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3319
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.