PHP - Line Graph Library

From Global Programming Syntax

Jump to: navigation, search

Sometimes you may want to make a line graph and it can be a devil to make and that is why I have made a library specifically for doing line graphs. The library is as follows:

<?php
class img {
private $obj;
private $x;
private $y;
function __construct($xx,$yy,$file=false) {
$xx=(int) $xx;
$yy=(int) $yy;
if ($xx<1) { $xx=1; }
if ($yy<1) { $yy=1; }
$this->x=$xx;
$this->y=$yy;
if ($file!=false) {
if (!file_exists($file)) {
$file=false;
} else {
$func='imagecreatefrom';
$type=exif_imagetype($file);
if ($type==IMAGETYPE_GIF) {
$func.='gif';
} elseif ($type==IMAGETYPE_JPEG) {
$func.='jpeg';
} elseif ($type==IMAGETYPE_PNG) {
$func.='png';
} elseif ($type==IMAGETYPE_WBMP) {
$func.='wbmp';
} elseif ($type==IMAGETYPE_XBM) {
$func.='xbm';
} else {
$file=false;
}
$this->obj = $func($file);
}
}
if ($file==false) {
$this->obj = imagecreatetruecolor($xx,$yy);
}
}
 
function bgcolor($r,$g,$b) {
imagefilledrectangle($this->obj,0,0,$this->x,$this->y,imagecolorallocate($this->obj,(int) $r,(int) $g,(int) $b));
}
 
function image_size() {
return array('x'=>$this->x,'y'=>$this->y);
}
 
function display($type) {
$type=strtolower($type);
if ($type=='jpg' || $type=='jpeg') {
header('Content-type: image/jpeg');
imagejpeg($this->obj);
} elseif ($type=='gif') {
header('Content-type: image/gif');
imagegif($this->obj);
} elseif ($type=='png') {
header('Content-type: image/png');
imagepng($this->obj);
}
return '';
}
 
function addbg($file) {
if (file_exists($file)) {
$func='imagecreatefrom';
$type=exif_imagetype($file);
if ($type==IMAGETYPE_GIF) {
$func.='gif';
} elseif ($type==IMAGETYPE_JPEG) {
$func.='jpeg';
} elseif ($type==IMAGETYPE_PNG) {
$func.='png';
} elseif ($type==IMAGETYPE_WBMP) {
$func.='wbmp';
} elseif ($type==IMAGETYPE_XBM) {
$func.='xbm';
}
if ($func!='imagecreatefrom') {
list($width,$height)=getimagesize($file);
imagecopyresized ($this->obj, $func($file), 0, 0, 0, 0, $this->x, $this->y, $width , $height );
}
}
}
 
function add_graph_points($points,$thick,$xscale,$yscale,$r=0,$g=0,$b=0) {
if (isset($points[0][0]) && isset($points[0][1]) && isset($points[1][0]) && isset($points[1][1])) {
$xscale=$this->x/$xscale;
$yscale=$this->y/$yscale;
$color=imagecolorallocate($this->obj,$r,$g,$b);
for ($i=0;$i<count($points[0]);$i++) {
if ($i>0) {
$x1=floor($xprev*$xscale);
$x2=floor($points[0][$i]*$xscale);
$y1=$this->y-floor($yprev*$yscale);
$y2=$this->y-floor($points[1][$i]*$yscale);
//echo "$x1,$x2,$y1,$y2<br>";
if ($thick == 1) {
imageline($this->obj, $x1, $y1, $x2, $y2, $color);
} else {
$t = $thick / 2 - 0.5;
if ($x1 == $x2 || $y1 == $y2) {
imagefilledrectangle($this->obj, round(min($x1, $x2) - $t), round(min($y1, $y2) - $t), round(max($x1, $x2) + $t), round(max($y1, $y2) + $t), $color);
} else {
$k = ($y2 - $y1) / ($x2 - $x1); //y = kx + q
$a = $t / sqrt(1 + pow($k, 2));
$pointsb = array(
round($x1 - (1+$k)*$a), round($y1 + (1-$k)*$a),
round($x1 - (1-$k)*$a), round($y1 - (1+$k)*$a),
round($x2 + (1+$k)*$a), round($y2 - (1-$k)*$a),
round($x2 + (1-$k)*$a), round($y2 + (1+$k)*$a),
);
imagefilledpolygon($this->obj, $pointsb, 4, $color);
imagepolygon($this->obj, $pointsb, 4, $color);
}
}
}
$xprev=$points[0][$i];
$yprev=$points[1][$i];
}
}
}
 
}
?>

Now to use this library it only requires a few lines of code then your done. Below is an example of every function that you can use.

<?php
include ('library.php');
 
$image = new img(640,480);
$image->bgcolor(127,127,127);
$image->addbg('background.jpg');
 
$thickness=3;
$height_scale=100; //100%
$width_scale=5; //5 points
$_2darray=array(array(0,1,2,3,4,5),array(0,10,40,30,60,20));
$linecolor_red=0;
$linecolor_green=0;
$linecolor_blue=0;
$image->add_graph_points($_2darray,$thickness,$width_scale,$height_scale,$linecolor_red,$linecolor_green,$linecolor_blue);
 
$image->display('jpeg'); //jpeg, jpg, gif, png
?>

Next is how do I understand the above code. It is really simple. First you include the library. Then you set a new image with its size. Below that you may assign a background color and a background image. Note that assigning the background color and background image must be done before adding graph points. Immediately after adding the background then you may add the graph points. The 2d array is a simple design. The first inner array is where on the x axis you want the point placed and the second array is where on the y axis you want the point placed. Also you may set the highest x value and highest y value with $width_scale and $height_scale. Also in the function you will want to determin the thickness of the line. And after all of that there is an optional parameter for the red green blue colors of the line.

Hope this code is useful and enjoy.

Personal tools
languages
page stats
Toolbox