PHP Salt


A little dash of PHP... my collection of php functions

Version: 2.33.2
Last Build: December 29, 2023 20:28pm (PST)

Main Menu

Home

Get PHP Salt


The Functions

array
db
file
graph
html
mail
math
misc
mobile
script
string
system
time
web
psGraphLineMulti()
Line Multi Graph


Description:

A simple line graph that doest require any special libraries.
Uses tick marks in X and Y, and a simple title
Handles up to 4 data sets (lines)



Usage:

$imagePointer = psGraphLineMulti($width,$height,$aryDataSet,$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

$aryDataSet['Set 1'] = array( 124,145,176,226,454,216,1500,264,324,156,245,134,122,152,500,100,120);
$aryDataSet['Set 2'] = array( 324,345,166,523,354,416,1400,364,224,156,245,134,422,152,500,100,120);
$aryDataSet['Set 3'] = array( 424,445,276,233,344,316,1300,224,124,156,245,134,322,152,500,100,120);
$aryDataSet['Set 4'] = array( 224,245,176,333,244,416,1200,124,224,356,445,334,222,252,400,200,420);

$title = "Graph of multi line example";
$im = psGraphLineMulti($w,$h,$aryDataSet,$yTicks,$xTicks,$title);
imagejpeg($im);




Code:

function psGraphLineMulti($width,$height,$aryDataSet,$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, 153, 102);

$lineColor[0] = $blue;
$lineColor[1] = $red;
$lineColor[2] = $green;
$lineColor[3] = $black;

// start of graph area
$fontHeight = 12;
$fontWidth = 5;
$tickSize = 7;
$gPad = 5;
$gSX = 120;
$gSY = $gPad;
$gEY = $height - 45;
$gEX = $width - $gPad;

$gWidth = $gEX - $gSX;
$gHeight = $gEY - $gSY;

// number of data points
foreach ($aryDataSet as $setName=>$dataSet)
{
$pc[] = count($dataSet);
}
$pointCnt = max($pc);

// some backgrounds and borders
imagesetthickness($im,1);
imagefill($im, 0, 0, $white); // background
imagerectangle($im,0,0,$width-1,$height-1,$black); // outline

// draw graph title
$oW = ($width/2) - (strlen($title) / 2);
imagestring($im,2,$oW,$height-$fontHeight-$gPad,$title,$black); // draw title


// graph the data set names
$lc = 0;
foreach ($aryDataSet as $setName=>$dataSet)
{
$oH = ($fontHeight * $lc) + $gPad;
imagestring($im,2,$gPad,$oH,$setName,$lineColor[$lc]); // draw set name
$lc++;
}


$incX = $gWidth / ($pointCnt-1); // X space between data points

// lets loop through data and find min and max values
foreach ($aryDataSet as $setName=>$dataSet)
{
$aMin[] = min($dataSet);
$aMax[] = max($dataSet);
}
$min = min($aMin);
$max = max($aMax);
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
imageline($im,$gSX,$tY,$gEX,$tY,$greyLt); // line
}
}


// 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);
$lc = 0;
foreach ($aryDataSet as $setName=>$dataSet)
{
$x1 = $gSX;
$x2 = $gSX;
$y1 = $gEY - ($dataSet[0] * $factY) + $floorY;;
foreach ($dataSet as $val)
{
$y2 = $gEY - ($val * $factY) + $floorY;
imageline($im,$x1,$y1,$x2,$y2,$lineColor[$lc]);
$x1 = $x2; $y1 = $y2;
$x2 += $incX;
}
$lc++;
}

return $im;
}



Changlelog:




See Also

And a shot out to:

PHP - php.net
Fedora Server - getfedora.com
Shameless ads to pay for site