Jump to content

Making a function grapher and graph generator with GD


horsebox

Recommended Posts

I just started using GD yesterday and I've been trying to make a decent graph generator but I'm having serious trouble

http://toxicopoeia.com/test.php

I just got a template for a dead basic graph generator and my plan was to modify it and turn it into a proper graph generator and function grapher but I'm stuck. Heres the code.

 

test.php

<? 
function getvars($var)
{
$cleaned = preg_replace("[^A-Za-z 0-9\/]", "", $_POST["$var"]);
return $cleaned;
}

if (isset($_POST['dimensions']) and $_POST['dimensions'] > 0) { $dimensions = (int)$_POST['dimensions']; } else { $dimensions = 10; }
if (isset($_POST['x_property'])) { $x_property = getvars("x_property"); } else { $x_property = "Property"; }
if (isset($_POST['x_unit'])) { $x_unit = getvars("x_unit");  } else { $x_unit = "Unit"; }
if (isset($_POST['y_property'])) { $y_property = getvars("y_property");  } else { $y_property = "Property"; }
if (isset($_POST['y_unit'])) { $y_unit = getvars("y_unit");  } else { $y_unit = "Unit"; }
if (isset($_POST['x_increment']) and $_POST['x_increment'] > 0) { $x_increment = (int)$_POST['x_increment']; } else { $x_increment = 1; }
if (isset($_POST['y_increment']) and $_POST['y_increment'] > 0) { $y_increment = (int)$_POST['y_increment']; } else { $y_increment = 1; }
if (isset($_POST['x_start']) and $_POST['x_start'] > 0) { $x_start = (int)$_POST['x_start']; } else { $x_start = 1; }
if (isset($_POST['x_end']) and $_POST['x_end'] > 0) { $x_end = (int)$_POST['x_end']; } else { $x_end = $dimensions; }
if (isset($_POST['y_start']) and $_POST['y_start'] > 0) { $y_start = (int)$_POST['y_start']; } else { $y_start = 1; }
if (isset($_POST['y_end']) and $_POST['y_end'] > 0) { $y_end = (int)$_POST['y_end']; } else { $y_end = $dimensions; }

echo '
<form action="test.php" method="post" name="graph">
Dimensions <input type="text" name="dimensions" size="2" maxlength="2" value=' . $dimensions . '><br>
X Property <input type="text" name="x_property" size="10" maxlength="15" value=' . $x_property . '> Unit <input type="text" name="x_unit" size="5" maxlength="10" value=' . $x_unit . '><br>
Y Property <input type="text" name="y_property" size="10" maxlength="15" value=' . $y_property . '> Unit <input type="text" name="y_unit" size="5" maxlength="10" value=' . $y_unit . '><br>
X-Range <input type="text" name="x_start" size="2" maxlength="2" value=' . $x_start . '> to <input type="text" name="x_end" size="2" maxlength="2" value=' . $x_end . '><br>
Y-Range <input type="text" name="y_start" size="2" maxlength="2" value=' . $y_start . '> to <input type="text" name="y_end" size="2" maxlength="2" value=' . $y_end . '><br>
X-Increment <input type="text" name="x_increment" size="2" maxlength="2" value=' . $x_increment . '><br>
Y-Increment <input type="text" name="y_increment" size="2" maxlength="2" value=' . $y_increment . '><br>
<input type="submit" name="go"></form><br>
<img src="grid2.php?dimensions='; 
echo "$dimensions&points=$points&x_property=$x_property&x_unit=$x_unit&y_property=$y_property&y_unit=$y_unit
&x_increment=$x_increment&y_increment=$y_increment&x_start=$x_start&x_end=$x_end&y_start=$y_start&y_end=$y_end&font=Abbeyline.ttf"; echo '" /><br><br>';

?>

 

grid2.php:

$dimensions = (int)$_GET['dimensions']; 
$x_increment = (int)$_GET['x_increment']; 
$y_increment = (int)$_GET['y_increment'];
$x_property = getvars("x_property"); 
$x_unit = getvars("x_unit"); 
$y_property = getvars("y_property"); 
$y_unit = getvars("y_unit"); 

$dim = $dimensions * 25;
$dim2 = $dim - 1;
$dim3 = $dimensions + 1;
$x_offset2 = 50;
$x_offset = 25;
$y_offset = 7;
$xi = 1;
$yi = $dimensions;


$points = "0 0, 0 0, 1 4, 2 7, 3 8, 4 5, 8 7, 7 15, 25 19";

$p = explode(", ",$points);

$pcount = count($p) - 1; 

for ($i=0; $i<$pcount+1; $i++){
if (isset($p[$i])) { 
	$p2 = explode(" ",$p[$i]); 
	$x[$i] = $p2[0] * 25; 
	$y[$i] = $p2[1] * 25; 
}

}

// Define .PNG image
header("Content-type: image/png");

$imgWidth=$dim + 80;
$imgHeight=$dim + 50;

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

// Set the line thickness to 5


// Create border around image
imagesetthickness($image, 3);
imageline($image, $x_offset-1, 0, $x_offset-1, $dim, $colorRed);
imagesetthickness($image, 1);
imageline($image, $x_offset, 0, $dim + $x_offset, 0, $colorBlack);
imageline($image, $dim2 + $x_offset, 0, $dim2 + $x_offset, $dim2, $colorBlack);
imagesetthickness($image, 3);
imageline($image, $x_offset, $dim2, $dim2 + $x_offset, $dim2, $colorBlack);

// Create grid
imagesetthickness($image, 1);
for ($i=1; $i<$dim3; $i++){
// Y Axis
imageline($image, $i*25, 0, $i*25, $dim, $colorBlack);
imagestring($image,3,25,$y_offset-10,$yi,$colorBlack);

// X Axis
imageline($image, $x_offset, $i*25, $dim + $x_offset, $i*25, $colorBlack);
imagestring($image,3,15,$imgHeight-25,0,$colorBlack);
imagestring($image,3,$x_offset2-2,$imgHeight-35,$xi,$colorBlack);


$x_offset2 += 25;
$y_offset += 25;

$xi = $xi + $x_increment;
$yi = $yi - $y_increment;
}

imagestring($image,5,($dim/2)-40,$imgHeight-18,"$x_property ($x_unit)",$colorGreen);

imagestringup($image,5,1,($y_offset/2)+40,"$y_property ($y_unit)",$colorGreen);

// Create line graph

 

imagesetthickness($image, 2);

 

for ($i=2; $i<$pcount; $i++){

imageline($image, $x[$i], ($dim-$y[$i]), $x[$i+1], ($dim-$y[$i+1]), $colorBlue);

 

}

 

// Output graph and clear image from memory

imagepng($image);

imagedestroy($image);[/code]

 

Anyone know a better method of doing this? Sooner or later I'm gonna have to make it display curves rather than just straight lines so in order to do that I'm guessing I have to make a load of tiny lines which contantly change slope as they approach the plotted point. Right now the lines are all made in multiples of 25.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.