Location: PHPKode > projects > Monyet > plotter/lineserv.php
<?php
include ("jpgraph/src/jpgraph.php");
include ("jpgraph/src/jpgraph_log.php");
include ("jpgraph/src/jpgraph_line.php");

//how i can pass the data array.. still try to figure it out
//finally i am using get method

$title = $_GET["title"];
$avg = $_GET["avg"];
$interval = $_GET["interval"];
//QUERY START and FINISH
if(isset($_GET['st'])){
	$q_start_time = " AND posttime >= \"".$_GET['st']."\"";
}
if(isset($_GET['ft'])){
	$q_finish_time = " AND posttime <= \"".$_GET['ft']."\"";
}
$agent_id = $_GET['agent_id'];
$zoomin = $_GET['zoomin'];
$zoomout = $_GET['zoomout'];
$service_id = $_GET['service_id'];
$db = new PDO('mysql:host=localhost;dbname=monyet', 'monyet', 'nyitnyit',array(
		     PDO::ATTR_PERSISTENT => true));
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sql = "SELECT green, red,starttime,finishtime FROM servmondef WHERE agent_id = ".$agent_id." AND service_id = ".$service_id;
$stmt = $db->prepare($sql);
$stmt->execute();
$row_bound = $stmt->fetch(PDO::FETCH_OBJ);
$green = $row_bound->green;
$red = $row_bound->red;
$starttime = $row_bound->starttime;
$finishtime = $row_bound->finishtime;

//get boundary
$valley = $green;
$peak = $red;

$sql = "SELECT DATE_FORMAT(posttime, \"%k:%i\") as posttime, elapsed FROM s_".$service_id."_".$agent_id." 
		  WHERE elapsed is not NULL ".$q_start_time." ".$q_finish_time;
$stmt = $db->prepare($sql);
$posttime = array();
$elapsed = array();
$stmt->execute();
$greenline = array();
$redline = array();
$inactive = array();
$result = $stmt->fetchAll(PDO::FETCH_OBJ);

if($avg==0){
	foreach($result as $key => $value){
		$posttime[] = $value->posttime;
		$elapsed[] = $value->elapsed;
		if((strtotime($value->posttime)>strtotime($starttime))&&(strtotime($value->posttime)<strtotime($finishtime))){
			$inactive[] = NULL; 
		}else{
			$inactive[] = $value->elapsed; 
		}
	
		if($peak < $value->elapsed){
			$peak = $value->elapsed;
		}
		if($valley > $value->elapsed){
			$valley = $value->elapsed;
		}
		$greenline[] = $green;
		$redline[] = $red;
	}
}
else{
	$total = count($result);
	for($i=0;$i<$total;$i=$i+$interval){
		if($i+$interval<$total){
			$temp = 0;
			for($z=$i;$z<($i+$interval);$z++){
				$temp += $result[$z]->elapsed;
			}
			$value = $temp/$interval;
			$elapsed[] = $value;
			$posttime[] = $result[$i]->posttime;
			$greenline[] = $green;
			$redline[] = $red;
			if((strtotime($result[$i]->posttime)>strtotime($starttime))&&(strtotime($result[$i]->posttime)<strtotime($finishtime))){
				$inactive[] = NULL; 
			}else{
				$inactive[] = $value; 
			}
			if($peak < $value){
				$peak = $value;
			}
			if($valley > $value){
				$valley = $value;
			}
		}
		else{
			$temp = 0;
			$count = 0;
			for($z=$i;$z<$total;$z++){
			  $temp += $result[$z]->elapsed;
			  $count++;
			}
			$value = $temp/$count;
			if((strtotime($result[$i]->posttime)>strtotime($starttime))&&(strtotime($result[$i]->posttime)<strtotime($finishtime))){
				$inactive[] = NULL; 
			}else{
				$inactive[] = $value; 
			}
			$posttime[] = $result[$i]->posttime;
			$elapsed[] = $value;
			$greenline[] = $green;
			$redline[] = $red;
			if($peak < $value){
				$peak = $value;
			}
			if($valley > $value){
				$valley = $value;
			}
		}
	}
	$interval = 1;
}	


$peak += ($peak-$valley)*0.15;
$valley -= ($peak-$valley)*0.05;
if($valley < 0){
	$valley = 0;
}
// Setup the graph
$graph = new Graph(510,300);
$graph->SetMarginColor('white');
$graph->SetScale("textlin",$valley,$peak);
$graph->SetFrame(false);
$graph->SetMargin(30,30,30,30);

$graph->title->Set($title);

$graph->yaxis->HideZeroLabel();
$graph->ygrid->SetFill(false,'#hide@address.com','#hide@address.com');
$graph->xgrid->Show();

$graph->xaxis->SetTickLabels($posttime);
$graph->xaxis->SetTextTickInterval($interval);

// Create the first line
$p1 = new LinePlot($elapsed);
$p1->SetColor("navy");
$p1->SetLegend('performance');
$graph->Add($p1);

$txt=new Text("[-]");
$txt->SetPos(492,25);
$txt->SetFont(FF_FONT1,FS_NORMAL);
if($zoomout){
	$txt->SetColor("black");
}
else{
	$txt->SetColor("gray");
}
$graph->AddText($txt);

$txt=new Text("[+]");
$txt->SetPos(492,45);
$txt->SetFont(FF_FONT1,FS_NORMAL);
if($zoomin){
	$txt->SetColor("black");
}
else{
	$txt->SetColor("gray");
}
$graph->AddText($txt);

$txt=new Text("[A]");
$txt->SetPos(492,85);
$txt->SetFont(FF_FONT1,FS_NORMAL);
if($avg==0){
	$txt->SetColor("black");
}
else{
	$txt->SetColor("gray");
}
$graph->AddText($txt);

$txt=new Text("[D]");
$txt->SetPos(492,105);
$txt->SetFont(FF_FONT1,FS_NORMAL);
if($avg==1){
	$txt->SetColor("black");
}
else{
	$txt->SetColor("gray");
}
$graph->AddText($txt);


$p2 = new LinePlot($greenline);
$p2->SetColor("yellow");
//$p2->SetLegend('lower bound LAN');
$p2->SetStyle("dashed");
$graph->Add($p2);


// Create the third line
$p3 = new LinePlot($redline);
$p3->SetColor("red");
//$p3->SetLegend('upper bound LAN');
$p3->SetStyle("dashed");
$graph->Add($p3);

$p5 = new LinePlot($inactive);
$p5->SetColor("gray");
$graph->Add($p5);


$graph->legend->SetShadow('hide@address.com',5);
$graph->legend->SetPos(0.1,0.1,'right','top');
// Output line
$graph->Stroke();
?>
Return current item: Monyet