I want to have a graph on a web page, a simple x-y graph from two arrays, $x and $y.
I have written a php page that can be used as an image, you know <img src="plot.php"> on my main page, but I can only plot hard coded data. I cannot figure out how to send my $x and $y from the main page to "plot.php". I don't want to send data in the url since I don't want to limit the size of $x and $y. And I don't know how to post form data to the page when I include it in an <img> tag.
I had an idea to create a graph object or a function that would return an image that I would somehow display. Something on the form:
I hope you understand what I mean, because I'm confused myself. Any suggestions are welcome. Here is plot.php:
Code:
<?php
header("Content-type: image/png");
$x = array(0,1,2,3,4);
$y = array(-1,0,2,2,1);
$image;
$width = 800;
$height = 250;
$marginleft = 25;
$marginright = 25;
$margintop = 20;
$marginbottom = 20;
function xpix($p) {
global $x, $width, $marginleft, $marginright;
return ( ($p-min($x)) * ($width-$marginleft-$marginright) / (max($x)-min($x)) ) + $marginleft;
}
function ypix($p) {
global $y, $height, $margintop, $marginbottom;
return ( (max($y)-$p) * ($height-$margintop-$marginbottom) / (max($y)-min($y)) ) + $margintop;
}
# Make sure x and y are of equal size.
$x = array_slice($x, 0, min(count($x),count($y)));
$y = array_slice($y, 0, min(count($x),count($y)));
$image = imagecreate($width,$height);
$background = imagecolorallocate($image, 127, 127, 127);
$black = imagecolorallocate($image, 0, 0, 0);
for ($i = 0; $i < count($x)-1; $i++) {
imageline($image, xpix($x[$i]), ypix($y[$i]), xpix($x[$i+1]), ypix($y[$i+1]), $black);
imagestring($image, 5, xpix($x[$i+1])-25, ypix($y[$i+1]), "(".$x[$i+1].",".$y[$i+1].")", $black);
}
imagestring($image, 5, xpix($x[0])-25, ypix($y[0]), "(".$x[0].",".$y[0].")", $black);
imageline($image, xpix(min($x)), ypix(0), xpix(max($x)), ypix(0), $black);
imagepng($image);
imageDestroy($image);
?>
edit: ok, problem solved using a session. Not much life on these forums by the way?