psGraphLineDelta() Delta Line Graph
Description:
A simple delta line graph that doesnt require any special libraries. Uses tick marks in X and Y, and a simple title. Segments swinging up over 0 have a green color, down below 0 have red
Usage:
$imagePointer = psGraphLineDelta($width,$height,$dataSet,$yTicks,$xTicks[,$title][,$maxY]);
Dont forget to use ob_start(); and ob_end_clean(), so as to avoid any data (blank lines) fronm being introduced into the output from included files.
Example:
ob_start(); include("../inc/phpsalt.php");
ob_end_clean(); header("Content-type: image/jpeg");
$w = 800; $h = 200;
$yTicks[1500] = "1500"; $yTicks[1000] = "1000"; $yTicks[500] = "500"; $yTicks[200] = "200"; $yTicks[100] = "-100";
$xTicks[5] = "5"; // 5th item $xTicks[10] = "10"; // 10th item $xTicks[15] = "15"; // 15th item
$dataSet = array( 424,145,176,223,-32,416,1500,-40,-20,-5,-60,134,122,152,500,100,120);
$title = "Graph of data example"; $im = psGraphLineDelta($w,$h,$dataSet,$yTicks,$xTicks,$title);
imagejpeg($im);
Code:
function psGraphLineDelta($width,$height,$dataSet,$yTicks,$xTicks,$title="",$maxY=0) {
$im = imagecreatetruecolor($width,$height); $white = imagecolorallocate($im, 255,255,255); $black = imagecolorallocate($im, 0,0,0); $grey = imagecolorallocate($im, 127,127,127); $greyLt = imagecolorallocate($im, 192,192,192); $red = imagecolorallocate($im, 255,0,0); $blue = imagecolorallocate($im, 0,0,255); $green = imagecolorallocate($im, 0, 255, 0);
// start of graph area $fontHeight = 12; $fontWidth = 5; $tickSize = 7; $gPad = 5; $gSX = 70; $gSY = $gPad; $gEY = $height - 45; $gEX = $width - $gPad;
$gWidth = $gEX - $gSX; $gHeight = $gEY - $gSY;
// number of data points $pointCnt = count($dataSet);
// some backgrounds and borders imagesetthickness($im,1); imagefill($im, 0, 0, $white); // background imagerectangle($im,0,0,$width-1,$height-1,$black); // outline
$oW = ($width/2) - (strlen($title) / 2); imagestring($im,2,$oW,$height-$fontHeight-$gPad,$title,$black); // draw title
$incX = $gWidth / ($pointCnt-1); // X space between data points
// lets loop through data and find min and max values $min = min($dataSet); $max = max($dataSet); if ($maxY > 0) { $max = $maxY; } $max = $max * 1.05; // adds 5% padding to highest value
$factY = $gHeight / ($max - $min); $floorY = $min * $factY; // adjust Y values to match floor
// imagestring ( resource $image , int $font , int $x , int $y , string $string , int $color )
// draw the Y ticks imagesetthickness($im,1); foreach ($yTicks as $key=>$val) { if ($min <= $key && $key <= $max) { $tX = $gPad; $tY = $gEY - ($key * $factY) + $floorY; //print "x:$tX y:$tY str:$val\r\n"; $oW = (strlen($val) * $fontWidth) + $tickSize + $gPad; imagestring($im,2,$gSX-$oW,$tY-7,$val,$blue); // draw the string imageline($im,$gSX - $tickSize,$tY,$gSX-4,$tY,$grey); // tick if ($key == 0 ) { imageline($im,$gSX,$tY-1,$gEX,$tY-1,$blue); } // double for 0 imageline($im,$gSX,$tY,$gEX,$tY,$greyLt); // line if ($key == 0 ) { imageline($im,$gSX,$tY+1,$gEX,$tY+1,$blue); } // double for 0 } }
// draw the X ticks imagesetthickness($im,1); foreach ($xTicks as $key=>$val) { $tX = $gSX + ($key * $incX); $tY = $gEY + $gPad; $oW = (strlen($val) * $fontWidth); imagestring($im,2,$tX-($oW),$tY+$tickSize,$val,$blue); // draw the string imageline($im,$tX,$gEY+3,$tX,$gEY+$tickSize,$grey); // tick imageline($im,$tX,$gSY,$tX,$gEY,$greyLt); // line }
// outline graph area imagesetthickness($im,1); imagerectangle($im,$gSX-1,$gSY,$gEX,$gEY,$blue); // outline graph area
// draw the graph imagesetthickness($im,2); $x1 = $gSX; $x2 = $gSX; $y1 = $gEY - ($dataSet[0] * $factY) + $floorY;; foreach ($dataSet as $val) { $y2 = $gEY - ($val * $factY) + $floorY; if ($val >= 0 ) { imageline($im,$x1,$y1,$x2,$y2,$green); } else { imageline($im,$x1,$y1,$x2,$y2,$red); } $x1 = $x2; $y1 = $y2; $x2 += $incX; }
return $im; }
Changlelog:
|
|