File: 0.01.2a/web/test.graph.php (View as Code)

1: 2: ob_start(); 3: require("include.php"); 4: $NATS->Start(); 5: /* 6: if (!$NATS_Session->Check($NATS->DB)) 7: { 8: header("Location: ./?login_msg=Invalid+Or+Expired+Session"); 9: exit(); 10: } 11: if ($NATS_Session->userlevel<1) UL_Error("View Test History"); 12: */ 13: 14: function ty($y) 15: { 16: global $height; 17: return $height-$y; 18: } 19: 20: 21: // width height suid startx finishx 22: 23: if (isset($_REQUEST['width'])) $width=$_REQUEST['width']; 24: else $width=600; 25: if (isset($_REQUEST['height'])) $width=$_REQUEST['height']; 26: else $height=200; 27: // start image 28: $im=@imagecreate($width,$height) 29: or die("Cannot create image"); 30: 31: // setup colours 32: $c_white=imagecolorallocate($im,255,255,255); 33: $c_black=imagecolorallocate($im,0,0,0); 34: $c_red=imagecolorallocate($im,250,50,50); 35: $c_axes=imagecolorallocate($im,200,200,200); 36: $c_blue=imagecolorallocate($im,50,50,255); 37: $c_green=imagecolorallocate($im,0,200,0); 38: $c_orange=imagecolorallocate($im,200,200,0); 39: 40: $c_bg=$c_white; 41: $c_txt=$c_black; 42: 43: // fill background 44: imagefill($im,1,1,$c_bg); 45: 46: function ierror($t) 47: { 48: global $im,$width,$height,$c_red; 49: header("Content-type: image/png"); 50: imagestring($im,2,($width/2)-20,$height/2,"ERROR: ".$t,$c_red); 51: imagepng($im); 52: imagedestroy($im); 53: exit(); 54: } 55: 56: 57: if (!isset($_REQUEST['nodeid'])) ierror("No node ID"); 58: else $nodeid=$_REQUEST['nodeid']; 59: 60: $day=date("d"); 61: $month=date("m"); 62: $year=date("Y"); 63: 64: if (isset($_REQUEST['startx'])) $startx=$_REQUEST['startx']; 65: else 66: { // 0:00 today HMS mo da yr 67: $startx=mktime(0,0,0,$month,$day,$year); 68: //$startx=1203451396; 69: } 70: 71: if (isset($_REQUEST['finishx'])) $finishx=$_REQUEST['finishx']; 72: else $finishx=mktime(23,59,59,$month,$day,$year); 73: //$finishx=1203454996; 74: $periodx=$finishx-$startx; 75: $startt=date("H:i:s d/m/y",$startx); 76: $finisht=date("H:i:s d/m/y",$finishx); 77: 78: 79: // titles and stuff 80: imagestring($im,1,2,2,$startt,$c_txt); 81: imagestring($im,1,$width-90,2,$finisht,$c_txt); 82: imagestring($im,2,($width/2)-30,0,$nodeid,$c_txt); 83: 84: // v-axes 85: imageline($im,50,ty(1),50,ty($height-20),$c_axes); 86: imageline($im,$width-5,ty(1),$width-5,ty($height-20),$c_axes); 87: // y-axes 88: imageline($im,50,ty(1),$width-5,ty(1),$c_axes); 89: imageline($im,$width-5,ty($height-20),50,ty($height-20),$c_axes); 90: $xoff=50+1; 91: $xlen=$width-$xoff-5; 92: 93: $yoff=1; 94: $ylen=$height-20; 95: 96: //ierror("hello"); 97: 98: // range data 99: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\""; 100: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($finishx)." "; 101: $q.="ORDER BY testvalue ASC LIMIT 0,1"; // lowest 102: $r=$NATS->DB->Query($q); 103: //ierror($q); 104: if (!$row=$NATS->DB->Fetch_Array($r)) ierror("No data for test"); 105: $lowest=$row['testvalue']; 106: $dlow=$lowest; 107: if ($dlow>0) $dlow=0; 108: $NATS->DB->Free($r); 109: 110: $q="SELECT testvalue FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\""; 111: 112: $q.=" AND recordx>=".ss($startx)." AND recordx<=".ss($_REQUEST['finishx'])." "; 113: 114: $q.= "ORDER BY testvalue DESC LIMIT 0,1"; //highest 115: 116: $r=$NATS->DB->Query($q); 117: $row=$NATS->DB->Fetch_Array($r); 118: $highest=$row['testvalue']; 119: $dhigh=$highest; 120: $NATS->DB->Free($r); 121: 122: $drange=$dhigh-$dlow; 123: 124: // calculate scales 125: $xscale=$xlen/$periodx; 126: if ($drange>0) $yscale=$ylen/$drange; 127: else $yscale=1; // doesn't display but no change! 128: 129: function posx($time) // timex 130: { 131: global $xscale,$startx,$xoff; 132: $drawx=$xscale*($time-$startx); 133: $screenx=$drawx+$xoff; 134: return $screenx; 135: } 136: 137: function posy($value) // stock value 138: { 139: global $yscale,$dlow,$yoff; 140: $drawy=$yscale*($value-$dlow); 141: $screeny=$drawy+$yoff; 142: return $screeny; 143: } 144: 145: 146: 147: // show axes scales 148: imagestring($im,1,2,ty(10),$dlow,$c_txt); 149: imagestring($im,1,2,ty($height-18),$dhigh,$c_txt); 150: 151: 152: 153: // get data and draw 154: $q="SELECT testvalue,alertlevel,recordx FROM fnrecord WHERE testid=\"".ss($_REQUEST['testid'])."\" "; 155: $q.="AND recordx>=".ss($startx)." AND recordx<=".ss($finishx); 156: //$q.="LIMIT 0,100"; 157: $r=$NATS->DB->Query($q); 158: $lastx=0; 159: $lasty=0; 160: 161: $startval=0; 162: $finishval=0; 163: 164: while ($row=mysql_fetch_array($r)) 165: { 166: $x=posx($row['recordx']); 167: $y=posy($row['testvalue']); 168: 169: 170: if ($lastx!=0) 171: { 172: if ($row['alertlevel']==-1) $c=$c_black; 173: else if ($row['alertlevel']==0) $c=$c_green; 174: else if ($row['alertlevel']==1) $c=$c_orange; 175: else if ($row['alertlevel']==2) $c=$c_red; 176: else $c=$c_black; 177: imageline($im,$lastx,ty($lasty),$x,ty($y),$c); 178: } 179: 180: //imageellipse($im,$x,ty($y),1,1,$c_red); 181: 182: $lastx=$x; 183: $lasty=$y; 184: 185: } 186: mysql_free_result($r); 187: 188: 189: 190: // output image 191: header("Content-type: image/png"); 192: imagepng($im); 193: imagedestroy($im); 194: exit(); // just in case 195: 196: ?> 197: