problem

GD and GD2 are useful libraries for creating graphics on-the-fly. Discuss your PHP GD and GD2 scripts here.

Moderators: onion2k, General Moderators

Post Reply
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

problem

Post by sandy1028 »

Hi,

Facing problem in plotting when the fixed width and heigth is given






Code: Select all

$graphValues=array($avg,$max); 

// Define .PNG image 
header("Content-type: image/png"); 
$imgWidth=50; 
$imgHeight=15; 

// Create image and define colors 
$image=imagecreate($imgWidth, $imgHeight); 
$colorWhite=imagecolorallocate($image, 0, 255, 255); 
$colorGrey=imagecolorallocate($image, 192, 192, 192); 
$colorBlue=imagecolorallocate($image, 0, 0, 255); 
$colorred=imagecolorallocate($image, 255, 0, 0); 


imagefill($image, 0, 0, $colorGrey); 

$avg_val= ($imgWidth * $avg)/$max; 


//verticle lines 
imageline($image, $i*50, 0, $i*50, 50, $colorGrey); 
imagefilledrectangle($image, 0 ,0,$avg_val,$imgWidth,$colorred); 
//Horizontal lines 

//destroy the image 
imagepng($image); 
imagedestroy($image);
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

You say you're facing a problem but you don't explain what it is. How do you expect anyone to help?
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Post by sandy1028 »

onion2k wrote:You say you're facing a problem but you don't explain what it is. How do you expect anyone to help?

I am facing problem taking large values in the array and ploting against the fixed width and heigth of the image...

I tried plotting like ($avg/$max)*50

But I feel it is not correct
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

The size of the vertical axis needed to be scaled against the size of the image. The most straightforward way to achieve that is with a percentage...

Code: Select all

$image_height = 50; //The height of the image in pixels.
$max_value = 100; //The maximum value of the graph.
$real_value = 50; //The value you want to plot.

//Take the ratio of the real value to the maximum (etc 50/100 = 0.5), and multiply it by the image height.
$plot_height = ($real_value/$max_value) * $image_height;

//Plot from 0,0 to $imgWidth,$image_value.
imagefilledrectangle($image, 0, 0, $imgWidth, $plot_height, $colorRed);
You might also find that with 0,0 being the top left your graph is upside down.
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Post by sandy1028 »

onion2k wrote:The size of the vertical axis needed to be scaled against the size of the image. The most straightforward way to achieve that is with a percentage...

Code: Select all

$image_height = 50; //The height of the image in pixels.
$max_value = 100; //The maximum value of the graph.
$real_value = 50; //The value you want to plot.

//Take the ratio of the real value to the maximum (etc 50/100 = 0.5), and multiply it by the image height.
$plot_height = ($real_value/$max_value) * $image_height;

//Plot from 0,0 to $imgWidth,$image_value.
imagefilledrectangle($image, 0, 0, $imgWidth, $plot_height, $colorRed);
You might also find that with 0,0 being the top left your graph is upside down.

Hi,

Thanks for the reply....

Is there any way to plot without percentage....
$imgHeight=15
$imgWidth=50
$avg=75
$max=333

How to divide the width based on the max value into ten parts and then plot........
How can we add the imageString to it
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Is there any way to plot without percentage....
$imgHeight=15
$imgWidth=50
$avg=75
$max=333
There's plenty of alternatives. Unless you state what you want then there's no way to suggest something though.
How to divide the width based on the max value into ten parts and then plot........
No idea what you mean.
How can we add the imageString to it
Don't understand the question here either. What string?
sandy1028
Forum Commoner
Posts: 60
Joined: Thu Jul 26, 2007 3:56 am

Post by sandy1028 »

onion2k wrote:
Is there any way to plot without percentage....
$imgHeight=15
$imgWidth=50
$avg=75
$max=333
There's plenty of alternatives. Unless you state what you want then there's no way to suggest something though.
How to divide the width based on the max value into ten parts and then plot........
No idea what you mean.
How can we add the imageString to it
Don't understand the question here either. What string?
Mark the $max and $avg value in the rectangle. Write the values externally to rectangle
Post Reply