-
Mar 3rd, 2009, 12:30 AM
#1
Thread Starter
WiggleWiggle
GDLib Graphing Class
Intro
I made this class because i could not, for the life of me, find a basic x/y axis graph for php. With this class you can do the following:
- Graph Multiple Objects
- Graph unlimited X/Y Cooridnate Pairs
- Choose Color for each Object
The Class
PHP Code:
header("Content-type: image/png"); header("Cache-Control: no-cache, must-revalidate"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");
class graph { var $imgx; var $imgy; //var $scale; var $mult; var $xorigin; var $yorigin; var $image; var $clrWhite; var $clrBlack; var $clrRed; var $currentColor; function __construct() { $this->imgx = 400; $this->imgy = 400; $this->mult = 10; $this->xorigin = ($this->imgx/2); $this->yorigin = ($this->imgy/2); $this->image = imagecreate($this->imgx, $this->imgy); $this->clrWhite = imagecolorallocate($this->image, 255, 255, 255); $this->clrBlack = imagecolorallocate($this->image, 0, 0, 0); $this->clrRed = imagecolorallocate($this->image, 255, 0, 0); $this->buildAxis(); } function buildAxis() { // CREATE X-AXIS imageline($this->image, 0, $this->yorigin, $this->imgx, $this->yorigin, $this->clrBlack); // CREATE Y-AXIS imageline($this->image, $this->xorigin, 0, $this->xorigin, $this->imgy, $this->clrBlack); } function makeGraph($grapharray) { $parabolacount = count($grapharray); $p = 0; for ($p; $p <= $parabolacount; $p++) { if (count($grapharray[$p][2])!= 3) { $red = rand(0,255); $green = rand(0,255); $blue = rand(0,255); } else { if ($grapharray[$p][2][0] > 255 || $grapharray[$p][2][0] < 0) { $red = 0; } else { $red = $grapharray[$p][2][0]; } if ($grapharray[$p][2][1] > 255 || $grapharray[$p][2][1] < 0) { $green = 0; } else { $green = $grapharray[$p][2][1]; } if ($grapharray[$p][2][2] > 255 || $grapharray[$p][2][2] < 0) { $blue = 0; } else { $blue = $grapharray[$p][2][2]; } } $this->currentColor = imagecolorallocate($this->image, $red, $green, $blue); $this->drawLines($grapharray[$p][0], $grapharray[$p][1], $this->currentColor); } } function drawLines($x, $y, $color) { //print_r($x); $xcount = count($x); $i = 0; for ($i; $i<=$xcount; $i++) { if ($i != $xcount-1) { imageline($this->image, ($this->xorigin+($x[$i])*$this->mult), ($this->yorigin-($y[$i])*$this->mult), ($this->xorigin+($x[$i+1])*$this->mult), ($this->yorigin-($y[$i+1])*$this->mult), $color); } else { imageline($this->image, ($this->xorigin+($x[$i])*$this->mult), ($this->yorigin-($y[$i])*$this->mult), ($this->xorigin+($x[$i])*$this->mult), ($this->yorigin-($y[$i])*$this->mult), $color); } } } function __destruct() { imagepng($this->image); imagedestroy($this->image); } }
Usage
Create a graph.php page. The following is an example:
PHP Code:
include ('class.graph.php'); $graph = new graph;
$graph->makeGraph( array( //parabola 1 array( //x point array(-3,-2,-1,0,1,2,3), //y points array(9,4,1,0,1,4,9), //color RGB array(255, 0, 0) ), //parabola 2 array( //x point array(-3,-2,-1,0,1,2,3), //y points array(-9,-4,-1,0,-1,-4,-9), //color RGB array(0, 0, 255) ) ) );
Then, where ever you want that graph to show, use the Image Tag:
HTML Code:
<img src="graph.php">
Here is another example, allowing you to set the points through $_GET. Although, this only allows 1 object At the moment.
PHP Code:
if ($_GET['x'] != "" && $_GET['y'] != "") { $xa = explode(',', $_GET['x']); $ya = explode(',', $_GET['y']); include ('class.graph.php'); $graph = new graph; $graph->makeGraph( array( //parabola 1 array( //x point $xa, //y points $ya, //color RGB array(255, 0, 0) ) ) );
}
The IMG tag:
HTML Code:
<img src="graph.php?x=-3,-2,-1,0,1,2,3&y=-9,-4,-1,0,-1,-4,-9">
Last edited by dclamp; Feb 5th, 2014 at 12:16 AM.
Reason: Updated IMG URL
My usual boring signature: Something
-
Jul 13th, 2012, 09:23 AM
#2
Fanatic Member
Re: GDLib Graphing Class
This is pretty sweet from what I can see. Will check it out soon!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|