<?php
 // includovani potrebnych souboru lin a log jsou pro linearni a logaritmicke cary
include ("./config.php");
include ("./jpg/src/jpgraph.php");
include ("./jpg/src/jpgraph_line.php");
 //include ("jpgraph_log.php");

 
// pole dat ke kresleni
$i=0;
$time = time();
$interval=24*60*$dnu/12;
$datum="H:i";
$popisekx="hours";
$dnukrat=1;
if ($dnu > 1)
{
 $dnukrat=$dnu*1.9;
	$datum="H d/m";
	$popisekx="days";	
	$dnukrat=140;
	$interval=25*$dnu;
}
if ($dnu > 7)
{
 $dnukrat=$dnu*1.9;
	$datum=" d/m";
	$dnukrat=1000;
	$interval=7*$dnu;
}
// (($time-time) % (60*$dnu))<10");Limit  0,1410

$timekrat=$time-($dnu*24*60*60);
$sql="SELECT * FROM zatez WHERE id_server=$id_server AND time>$timekrat AND ((time) % ($dnukrat))<20 order by time Limit  0,1410";
//echo "$sql";
$aa=MySQL_Query($sql);
while($bb=mysql_fetch_array($aa))
{
	$xdata[$i]=date($datum,$bb["time"]);
	$ydata[$i]=$bb["cpu"];
	$y2data[$i]=$bb["ramused"]/$bb["ramall"]*100;
	$i++;
}

$vzorku=count($ydata);
$titulek="Usage of server ".$nazev." Count of samples".$vzorku;
//for($x=0;$x<$i;$x++)
//	echo "".$x." ".$ydata[$x]." - ".$ydata2[$x]."<br>";
if (count($ydata)<2)
{
	$ydata = array(0,100);
	$y2data = array(100,0);
	$titulek="Insufficiency of data for plot - ".$nazev;
}
//$datax=array("Jan","Feb","Mar","Apr","Maj","Jun","July","aug","Sep","Oct");

/*********** Vytvoreni grafu ************/
	$graph = new Graph(710,350);
	$graph->SetScale('textlin'); 
	 // nayev grafu 
	$graph->title->Set($titulek);
	$graph->title->SetFont(FF_FONT1,FS_BOLD);
	$graph->SetColor("white:1");
	 // okraje grafu levy pravy horni dolni 
	$graph->img->SetMargin(80,90,30,40);
 $graph->SetMarginColor('white');
	 // kde bude legenda 
	$graph->legend->Pos(0.01,0.07,"right","center");
	$graph->legend->SetFillColor("white");
	 // vlozeni stinu pod graf
	$graph->SetShadow();
	 // vlozeni popisku do xove osy
	//$graph->xaxis->SetTickLabels($datax);

	 // mrizka na xove ose
	$graph->xgrid->Show(true,false);

/*********** Prvni cara ************/
	 // Popis os a nazev grafu

	$graph->xaxis->title->Set("Time [".$popisekx."]");
	$graph->xaxis->SetTickLabels($xdata);
	$graph->xaxis->SetTextTickInterval($interval); 
	$graph->yaxis->title->Set("Usage of CPU [%]");//$graph->yaxis->SetTitle("Time [min]");

	 // Vzhled popisku os a nazvu grafu $graph->yaxis->title->Set("Usage of CPU [%]","low");
	$graph->yaxis->SetTitleMargin(50);
	$graph->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
	$graph->xaxis->title->SetFont(FF_FONT1,FS_BOLD);
	 // barva a sirka ypilonove osy
	$graph->yaxis->SetColor("blue:0.9");
	$graph->yaxis->SetWeight(2);
	 // kreleni mrizky
	$graph->ygrid->Show(true,true);

	 // nekresleni prvni nuly aby nespilvali s xovou nulou
	//	$graph->SetScale("intlin");

	//$graph->yaxis->SetTickLabels($procenta);
	//$graph->yaxis->SetTextTickInterval(4); 
	//$graph->yaxis->scale->SetAutoMin(0);
	$graph->yaxis->scale->SetGrace(0);
	//$graph->yaxis->scale->SetAutoMax(100);
	$graph->yaxis->scale->ticks->SupressFirst();
	 // vytvori novou instanci tridy lineplot malovani linearni cary vlozi tam data
	$lineplot=new LinePlot($ydata);
	 //legenda
	$lineplot->SetLegend("CPU");
	 // nastavi barvu a tloustku kreslene cary 
	$lineplot->SetColor("blue:0.9");
	$lineplot->SetWeight(2);  // Two pixel wide
	//$lineplot->SetFillColor("orange");
	 // vykresleni cary
	$graph->Add($lineplot);
	
/*********** Druha cara ************/
	 // Vlozeni druheho meritka
	$graph->SetY2Scale("lin");	 
 	 // Popis os a nazev grafu
 	 	$graph->y2axis->SetTitleMargin(50);
	$graph->y2axis->title->SetFont(FF_FONT1,FS_BOLD);
	$graph->y2axis->title->Set("Usage of RAM [%]");
	$graph->y2axis->SetColor("green:1.0");
	 // nekresleni prvni nuly aby nespilvali s xovou nulou
	$graph->y2axis->scale->ticks->SupressFirst();
	 // vytvori novou instanci tridy lineplot malovani linearni cary vlozi tam data
	$lineplot2=new LinePlot($y2data);
	 //legenda
	$lineplot2->SetLegend("RAM");
	 // nastavi barvu a tloustku kreslene cary 
	$lineplot2->SetColor("green:1.0");
	$lineplot2->SetWeight(2);  // Two pixel wide
	//$lineplot2->SetFillColor("orange");
	 // vykresleni cary
	$graph->AddY2($lineplot2);

/******* Zobrazeni grafu ************/
	 // Zobraz graf
	$graph->Stroke();


/******* Nepouzite poznamky ************/
	 // vykresli skokovou zmenu
	//$lineplot2->SetStepStyle();
	 // nastavi jake budou kresleny body
	//$lineplot->mark->SetColor("red");
	//$lineplot->mark->SetType(MARK_DIAMOND);
	 //kresleni mrizky
	//$graph->y2grid->Show(true,true);
	 // kolikaty popisek se ma tisknout
	//$graph->xaxis->SetTextTicks(2);
?>