Example 1st Graph

Example 2nd Graph

http://127.0.0.1/pnggraph.php?graphType=bar&yValues=3,4,2,2,8&yValues1=3,4,2,2,8&yValues2=0,3,4,2,2,8
Produces graph.
yoo -------------------------------|------|
title panel
yok -------------------------------|------|
Key panel
yog -------------------------------|------|
yLabel | yScale | graph |
|
yos -------------------------------|------|
| xScale |
yol -------------------------------|------|
| xLabel |
yom -------------------------------|------|
yomm---------------------------------------
| | | | |
xoo xos xol xor xom xomm
*/
//
// sec 1.0 - Get Data from POST or QUERY_STRING
//
$graphType = $_REQUEST["graphType" ]; // if set to bar, bar chart drawn
$title = $_REQUEST["title" ]; // Large text at bottom
$key = $_REQUEST["key"]; // csv list for each line
$yLabel = $_REQUEST["yLabel"]; // Large text at bottom
$yScaleStr = $_REQUEST["yScale"]; // small text in middle
$xLabel = $_REQUEST["xLabel"]; // Large text at left
$xScaleStr = $_REQUEST["xScale"]; // small text in middle
$xvaluesA = explode(",", $_REQUEST["xValues"]) ;
$yvaluesA = explode(",", $_REQUEST["yValues"]) ;
$yvalues1A = explode(",", $_REQUEST["yValues1"]) ;
$yvalues2A = explode(",", $_REQUEST["yValues2"]) ;
$yvalues3A = explode(",", $_REQUEST["yValues3"]) ;
$yvalues4A = explode(",", $_REQUEST["yValues4"]) ;
$yvalues5A = explode(",", $_REQUEST["yValues5"]) ;
$yLogicA = explode(",", $_REQUEST["yLogic"]) ; // if present then draw Logic Analyser.
$keyA = explode(",", $_REQUEST["key"]) ;
//
// sec 2.0 - Get min and max for both x and Y so graphs fit.
//
$xmin = 0 ;
$xmax = 1 ;
//$ymin = 0 ;
//$ymax = 0 ;
function findMinMax( $yTempvaluesA){
if ( is_array( $yTempvaluesA ) ) {
global $ymin,$ymax,$xmin,$xmax, $mm ;
// find min and max values
$cnt = 0 ;
while( is_numeric( $yTempvaluesA[ $cnt ] ) ){
$y0 = $yTempvaluesA[ $cnt ] ;
// Initilise ymin and ymax using the first value
if ( ! is_numeric($ymin) ){
$ymin = $y0;
$ymax = $ymin;
}
if ( $ymin > $y0 ) { $ymin = $y0 ; }
if ( $ymax < $y0 ) { $ymax = $y0 ; }
if ( $xmax < $cnt ) { $xmax = $cnt ; }
$cnt++;
}
}
}
$xmin = 0 ;
// find min max for the six lines
findMinMax( $yvaluesA);
findMinMax( $yvalues1A);
findMinMax( $yvalues2A);
findMinMax( $yvalues3A);
findMinMax( $yvalues4A);
findMinMax( $yvalues5A);
findMinMax( $yLogicA);
//
// sec 3.0 - Map values onto Graph Canvas.
//
header("Content-type: image/png");
$x_graph_width = 400 ;
$x_graph_width = 500 ;
$y_graph_width = 200 ;
if ( ($xmax-$xmin) > 0 ) {
$xscale = ($x_graph_width)/($xmax-$xmin) ;
} else {
$xscale = 1 ;
}
if ( ($ymax-$ymin) > 0 ) {
$yscale = ($y_graph_width)/($ymax-$ymin) ;
} else {
$yscale = 1 ;
}
$xLabelHeight = 30;
$yoo = 1 ;
$yok = $xLabelHeight + $yoo ;
$yog = $xLabelHeight + $yok ;
$yos = $y_graph_width + $yog ;
$yol = $xLabelHeight + $yos ;
$yom = $xLabelHeight + $yol ;
$yomm = 1 + $yom ;
$xoo = 1 ;
$xos = $xLabelHeight + $xoo ;
$xol = $xLabelHeight + $xos ;
$xor = $x_graph_width + $xol ;
$xom = $xLabelHeight + $xor ;
$xomm = 1 + $xom ;
// Label Offsets
// point size
$yop = 10 ;
$xop = 10 ;
//graph
$xobl = $xol ;
$yobl = $yos ;
$xotr = $xor ;
$yotr = $yog ;
//
// sec 4.0 - Create Canvas and colours.
//
$im = imagecreatetruecolor($xomm+1, $yomm+1);
$im_graph = imagecreatetruecolor($x_graph_width+1, $y_graph_width+1);
imagesetthickness($im,3);
$black = imagecolorallocate($im, 0, 0, 0);
$white = imagecolorallocate($im, 255, 255, 255);
$red = imagecolorallocatealpha($im, 255, 0, 0, 4 );
$green = imagecolorallocatealpha($im, 0, 255, 0, 4 );
$blue = imagecolorallocatealpha($im, 0, 0, 255, 4 );
$yellow= imagecolorallocatealpha($im, 255, 255, 220, 4 );
$purple= imagecolorallocatealpha($im, 0, 255, 255, 4 );
$orange= imagecolorallocatealpha($im, 255, 200, 0, 4 );
$cyan = imagecolorallocatealpha($im, 200, 0, 200, 4 );
$gray = imagecolorallocatealpha($im, 127, 127, 127, 32 );
imagesetthickness($im,1);
// BackGround
imagefilledrectangle($im, $xoo, $yoo, $xom, $yom, $white);
// graph
//imagefilledrectangle($im, $xol, $yog, $xor, $yos, $white);
//
// we have to plot the graph then add axis.
// I cannot find a clipping rectangle function!
// so plot to $im_graph and then copy onto main image $im
//
$im_graph = imagecreatetruecolor($x_graph_width, $y_graph_width);
imagefilledrectangle($im_graph,0,0,$x_graph_width, $y_graph_width,$yellow);
// we have to plot the graph then add axis.
function plotBar( $yvaluesA, $lineColour , $offset ){
if ( is_array( $yvaluesA ) ) {
global $im_graph, $xscale , $y_graph_width, $yscale,$ymin ;
//itterate through the y values.
$cnt = 0 ;
// to have a thick line you have to use the imagesetbrush()
// and imageline($im_graph, x0,y0,x1,y1, IMG_COLOR_BRUSHED );
// Set the brush
$im_brush = imagecreatetruecolor(3,3);
imagefilledrectangle($im_brush, 0,0, 6,6, $lineColour );
imagesetbrush($im_graph, $im_brush);
while( isset( $yvaluesA[$cnt+1] ) ) {
// if x value does not exist use $cnt
$x0 = $cnt*7 ;
$x1 = ($cnt+1) ;
$y0 = $yvaluesA[$cnt] ;
$y1 = $yvaluesA[$cnt+1] ;
imageline( $im_graph, ( $x0 - $xmin + $offset ) * $xscale/7, $y_graph_width - ( $y0 - $ymin ) * $yscale, ( $x0 - $xmin + $offset ) * $xscale/7, $y_graph_width - $ymin * $yscale, IMG_COLOR_BRUSHED );
$cnt++ ;
}
}
}
function plotLine( $yvaluesA, $lineColour ){
if ( is_array( $yvaluesA ) ) {
global $im_graph, $xscale , $y_graph_width, $yscale,$ymin ;
//itterate through the y values.
$cnt = 0 ;
// to have a thick line you have to use the imagesetbrush()
// and imageline($im_graph, x0,y0,x1,y1, IMG_COLOR_BRUSHED );
// Set the brush
$im_brush = imagecreatetruecolor(3,3);
imagefilledrectangle($im_brush, 0,0, 6,6, $lineColour );
imagesetbrush($im_graph, $im_brush);
while( isset( $yvaluesA[$cnt+1] ) ) {
// if x value does not exist use $cnt
$x0 = $cnt ;
$x1 = ($cnt+1) ;
$y0 = $yvaluesA[$cnt] ;
$y1 = $yvaluesA[$cnt+1] ;
imageline($im_graph, ($x0-$xmin)*$xscale ,$y_graph_width - ($y0-$ymin)*$yscale, ($x1-$xmin)*$xscale, $y_graph_width-($y1-$ymin)*$yscale, IMG_COLOR_BRUSHED );
// imageline($im_graph, ($x0-$xmin)*$xscale ,$y_graph_width - ($y0-$ymin)*$yscale, ($x1-$xmin)*$xscale, $y_graph_width-($y1-$ymin)*$yscale, $lineColour );
$cnt++ ;
}
}
}
function plotLogic( $yvaluesA,$colour1,$colour0,$numBits ){
//
// plots the values in yLogic as a logic analyser waveform
//
$keyA = explode(",", $_REQUEST["key"]) ;
if ( is_array( $yvaluesA ) ) {
global $im_graph, $xscale , $y_graph_width, $yscale,$ymin ;
//itterate through the y values.
$cnt = 0 ;
imagesetthickness($im_graph,2);
while( isset( $yvaluesA[$cnt+1] ) ) {
// if x value does not exist use $cnt
$x0 = $cnt ;
$x1 = ($cnt+1) ;
$y0 = $yvaluesA[$cnt] ;
$y1 = $yvaluesA[$cnt+1] ;
// imageline($im_graph, ($x0-$xmin)*$xscale ,$y_graph_width - ($y0-$ymin)*$yscale, ($x1-$xmin)*$xscale, $y_graph_width-($y1-$ymin)*$yscale, IMG_COLOR_BRUSHED );
// imageline($im_graph, ($x0-$xmin)*$xscale ,$y_graph_width - ($y0-$ymin)*$yscale, ($x1-$xmin)*$xscale, $y_graph_width-($y1-$ymin)*$yscale, $lineColour );
$p2 = 1;
for( $cnt2 = 0 ; $cnt2 < $numBits; $cnt2++ ){
$bit = $y0 & $p2;
$nextBit = $y1 & $p2;
$p2 = $p2 + $p2;
if ( $bit > 0 ) {
$logicColour = $colour1;
$bitoffset = 0.5;
} else {
$logicColour = $colour0;
$bitoffset = 0.8;
}
if ( $nextBit <> $bit ) {
imageline($im_graph, ($x1-$xmin)*$xscale , ( $cnt2 + 0.5 )*$y_graph_width/$numBits, ($x1-$xmin)*$xscale, ( $cnt2 + 0.8 )*$y_graph_width/$numBits, $logicColour );
}
imageline($im_graph, ($x0-$xmin)*$xscale , ( $cnt2+$bitoffset )*$y_graph_width/$numBits, ($x1-$xmin)*$xscale, ( $cnt2 + $bitoffset )*$y_graph_width/$numBits, $logicColour );
}
$cnt++ ;
}
$p2 = 1;
for( $cnt2 = 0 ; $cnt2 < $numBits; $cnt2++ ){
$p2 = $p2 + $p2;
imagettftext( $im_graph, $y_graph_width/$numBits/3, 0, 0 , ( $cnt2+0.4 )*$y_graph_width/$numBits,$black, './Vera.ttf', "B".$cnt2 .",". $keyA[$cnt2] );
}
}
}
//
// sec 5.0 - Draw Graph.
//
// Plot data onto graph panel
if ( $graphType == "bar" ) {
plotBar( $yvaluesA, $red ,1 ) ;
plotBar( $yvalues1A, $green ,2 ) ;
plotBar( $yvalues2A, $blue ,3 ) ;
plotBar( $yvalues3A, $purple,4 ) ;
plotBar( $yvalues4A, $orange,5) ;
plotBar( $yvalues5A, $cyan ,6) ;
} else {
plotLine( $yvaluesA, $red) ;
plotLine( $yvalues1A, $green) ;
plotLine( $yvalues2A, $blue) ;
plotLine( $yvalues3A, $purple) ;
plotLine( $yvalues4A, $orange) ;
plotLine( $yvalues5A, $cyan) ;
}
$yLogicA = explode(",", $_REQUEST["yLogic"]) ;
if ( array_key_exists ( "yLogic" , $_REQUEST ) ) {
plotLogic( $yLogicA, $green,$blue, 10 );
};
//
// plot Graticle at 10 sub divisions
imagesetthickness($im_graph,1);
if ( ! array_key_exists ( "yLogic" , $_REQUEST ) ) {
$step = floor( $y_graph_width/10 ) ;
for( $cnt = 1 ; $cnt < 10 ; $cnt++ ){
$offset = $y_graph_width - $cnt*$step ;
imageline($im_graph, 0, $offset, $x_graph_width, $offset, $gray );
}
};
$step = floor( $x_graph_width/10 ) ;
for( $cnt = 1 ; $cnt < 10 ; $cnt++ ){
$offset = $cnt*$step ;
imageline($im_graph, $offset, 0, $offset , $y_graph_width, $gray );
};
$font = './Vera.ttf';
if( ! $_SERVER["QUERY_STRING"] ) {
$yop = $yop;
imagettftext($im, $yop, 0, $xol, $yog + $yop*2,$black, $font," usage:Set Query string. Spaces replaced by +" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*3,$black, $font," ?title=title+text&key=Key+text" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*4,$black, $font," &yLabel=Label+text&xLabel=Label+text" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*5,$black,$font," &xScale=smallText" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*6,$black,$font," &yScale=smallText" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*8,$black, $font," &yValues=10,20,34,2,34" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*9,$black, $font," &yValues1=10,20,34,2,34" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*10,$black, $font," &yValues2=10,20,34,2,34" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*11,$black, $font," &yValues3=10,20,34,2,34" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*12,$black, $font," &yValues4=10,20,34,2,34" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*13,$black,$font," &yValues5=10,20,34,2,34" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*15,$black,$font," &yLogic=3,4,2,2,2,2" );
imagettftext($im, $yop, 0, $xol, $yog + $yop*17,$black,$font," &graphType=bar" );
$title = " Title:";
$key = " Key:";
$xScale = "xScale:";
$yScale = "yScale:";
$xLabel = "xLabel:";
$yLabel = "yLabel:";
} else {
// I cannot find a cliping function so plot graph on a small panel and then copy onto main image
imagecopy ( $im , $im_graph, $xol, $yog, 0, 0, $x_graph_width, $y_graph_width );
imagerectangle($im, $xol, $yog, $xor, $yos, $black); // graph
}
//
// point,angle,x,y
imagettftext($im, $yop*1.5, 0, $xoo, ($yoo+$yok)/2 +$yop/2 , $black, $font, " " . $title ); // Title
imagesetthickness($im,3);
// Plot key
// $yoff = ( $yok+$yog )/2
$yoff = $yok+5 ;
if ( ! array_key_exists ( "yLogic" , $_REQUEST ) ) {
imageline($im, $xoo+ 5,$yoff, $xoo+20, $yoff,$red);
imageline($im, $xoo+25,$yoff, $xoo+40, $yoff,$green);
imageline($im, $xoo+45,$yoff, $xoo+60, $yoff,$blue);
imageline($im, $xoo+65,$yoff, $xoo+80, $yoff,$purple);
imageline($im, $xoo+85,$yoff, $xoo+100, $yoff,$orange);
imageline($im, $xoo+105,$yoff, $xoo+120, $yoff,$cyan);
imagettftext($im, $yop, 0, $xoo, ($yok+$yog)/2 +$yop/2 , $black, $font, " key: " . $_REQUEST["key"] ); // key
}
imagettftext($im, $yop*1.25, 90, ($xoo+$xos)/2+$xop/2, $yos, $black, $font, " " . $yLabel ); // yLabel
imagettftext($im, $yop*1.25, 0, $xol, ($yol+$yom)/2 +$yop/2, $black, $font, " " . $xLabel );
//
// sec 6.0 - Annotate Scales.
//
//
// y scale, plot $ymin at bottom, left justified and $ymin at top, right justified
//
if ( ! array_key_exists ( "yLogic" , $_REQUEST ) ) {
imagettftext($im, $yop, 90, ($xos+$xol)/2+$xop/2, $yos, $black, $font, $ymin ); // yScale
// find bounding box to find width.
// this works but is not right.
$bbox = imagettfbbox($yop, -90, $font, $ymax);
$width = $bbox[3];
imagettftext($im, $yop, 90, ($xos+$xol)/2+$xop/2, $yog+$width, $black, $font, $ymax ); // yScale
}
$bbox = imagettfbbox($yop, -90, $font, $yScaleStr);
$width = -$bbox[3];
imagettftext($im, $yop, 90, ($xos+$xol)/2+$xop/2, ($yog+$yos)/2-$width/2, $black, $font, $yScaleStr ); // yScale
//
// x scale, plot $xmin at left, left justified and $ymin at right, right justified
//
imagettftext($im, $yop, 0, $xol, ($yos+$yol)/2 +$yop/2, $black, $font, " " . $xmin ); // xScale
// find bounding box to find width.
$bbox = imagettfbbox($yop, 0, $font, $xmax);
$width = $bbox[2];
imagettftext($im, $yop, 0, $xor-$width/2 , ($yos+$yol)/2 +$yop/2, $black, $font, $xmax ); // xScale
$bbox = imagettfbbox($yop, 0, $font, $xScaleStr);
$width = $bbox[2];
imagettftext($im, $yop, 0, ($xol+$xor)/2-$width/2 , ($yos+$yol)/2 +$yop/2, $black, $font, $xScaleStr ); // xScale
// find bounding box to find width.
$copyRightText = "pnggraph.php Copyright (c) Doug Rice 2009 ";
$bbox = imagettfbbox($yop*0.5, 0, $font, $copyRightText );
$width = $bbox[2];
imagettftext($im, $yop*0.5, 0, $xomm-$width , $yol +$yop*0.5, $gray, $font, $copyRightText ); // xScale
//
// sec 7.0 - Output graph.
//
// output stream of data
imagepng($im);
//imagegd($im);
//imagejpeg($im,null,100);
//imagegif($im);
//image2wbm($im);
?>