Drawing bar graph using GD and PHP – PHP

  php

Q(Question):

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

<?php
header("Content-type: image/jpeg");
$data = '1000';
$max = '4000';
$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;
$y_ht = ($data/$max)* $height;
imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
imagestring( $im,2,$x-1,$y+10,$data,$black);
$x += ($x_width+20);
imagejpeg($im);
?>

Please give me some idea regarding this.

With regards

A(Answer):

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?

A(Answer):

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

A(Answer):

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.

A(Answer):

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

<?php
header("Content-type: image/jpeg");
$data = '1000';
$max = '4000';
$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;
$y_ht = ($data/$max)* $height;
imagerectangle($im,$x,$y,$x+$x_width,($y-$y_ht),$red);
imagestring( $im,2,$x-1,$y+10,$data,$black);
$x += ($x_width+20);
imagejpeg($im);
?>

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

A(Answer):

Thanks dlite922 for your reply.

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

With regards

A(Answer):

Have a look at this
http://www.qualitycodes.com/tutorial.php?articleid=20

LEAVE A COMMENT