<?php
$width = $_REQUEST['w'];
$height = $_REQUEST['h'];
if(!$width) $width = 400;
if(!$height) $height = 300;
header("Content-type: image/png");
$im = imagecreate($width, $height);
$bg = imagecolorallocate($im, 255, 255, 255);
$lines = $_REQUEST['n'];
$graphs = array();
$p = 0;
$minv = null;
$maxv = null;
$maxp=0;
for ($j = 0; $j < $lines; $j++) {
$vs = array();
$v = $_REQUEST['v' . $j.'0'];
$i = 0;
while(isset($v)) {
$v = $_REQUEST['v' . $j.$i];
if (!isset($v)) break;
$vs[$i++] = $v;
if ($i > $maxp) $maxp = $i;
if(!is_numeric($v)) continue;
if ($minv==null || $v < $minv) $minv = $v;
if ($maxv==null || $v > $maxv) $maxv = $v;
}
$graphs[$p++] = $vs;
}
$span = $maxv - $minv;
for ($j=0; $j<$p; $j++) {
$color = imagecolorallocate($im, 255*(($j+1)&4), 255*(($j+1)&2), 255*(($j+1)&1));
$vs = $graphs[$j];
if(is_numeric($vs[0])) {
$oldy = ($height-1)*($vs[0]-$minv)/$span;
imagestring($im, 5, 0, min($height-$oldy, $height-15), $vs[0], $color);
}
else $oldy=null;
$oldx = 0;
$dir = true; //direction of graph, up/down
$write = 0; //time since last label
$writex=0;
for ($i=1; $i<count($vs); $i++) {
if(!is_numeric($vs[$i])) continue;
$y = ($height-1)*($vs[$i]-$minv)/$span;
$x = $width * $i / ($maxp-1);
if(is_null($oldy)){$oldy=$y;$oldx=$x;}
if ($y > $oldy) {
if (!$dir) $write +=1;
$dir = true;
}
else {
if ($dir) $write+=1;
$dir = false;
}
$write=1;
if ($write &&(abs($y-$oldy)>20 || abs($x-$writex)>30)) $write=9999999;
else $write = 0;
imageline ($im, $oldx, $height-1-$oldy, $x, $height-1-$y, $color);
if ($write>$width/count($vs)) { imagestring($im, 5, min($x, $width-10*strlen($vs[$i])), min($height-$y, $height-15), $vs[$i], $color); $write=0; $writex=$x;}
$oldx = $x;
$oldy = $y;
}
}
imagepng($im);
?>