<?php // content="text/plain; charset=utf-8"
require_once ('jpgraph/jpgraph.php');
require_once ('jpgraph/jpgraph_line.php');
require_once ('jpgraph/jpgraph_date.php');
require_once ('jpgraph/jpgraph_mgraph.php');

if(isset($_POST['banc'])) {

$idCond=$_POST['cond'];
$banc=$_POST['banc'];
$param=$_POST['param'];


DEFINE('NDATAPOINTS',28000);
DEFINE('SAMPLERATE',300);

$start = time();
$end = $start+NDATAPOINTS*SAMPLERATE;
$xdata_param = array();


//lecture data base
$db = mysql_connect('lalsrv1.lal.in2p3.fr', 'xfel', 'pwd4xfel', '');   // connexion
mysql_select_db('xfel',$db);                // selection 


//$req_pic    = mysql_query("SELECT Pic FROM power WHERE id_stand='b'and id_conditioning=233");// requetes
//$req_measure = mysql_query("SELECT hour FROM measure");
//$req_hour_pic = mysql_query("SELECT measure.hour AS 'Date', power.Pic AS 'Pic' FROM measure INNER JOIN power ON (measure.id_measure=power.id_measure AND measure.id_conditioning=power.id_conditioning) WHERE measure.id_conditioning = 233 AND power.id_stand = 'd'");

$requete = "SELECT measure.hour AS 'Date', ";
if (!strcmp($_POST['param'],"V1")) {
	$requete .= "vacuum.V1 AS 'V1' FROM measure INNER JOIN vacuum ON (measure.id_measure=vacuum.id_measure AND measure.id_conditioning=vacuum.id_conditioning) ";
}
if (!strcmp($_POST['param'],"V2")) {
	$requete .= "vacuum.V2 AS 'V2' FROM measure INNER JOIN vacuum ON (measure.id_measure=vacuum.id_measure AND measure.id_conditioning=vacuum.id_conditioning) ";
}
if (!strcmp($_POST['param'],"V3")) {
	$requete .= "vacuum.V3 AS 'V3' FROM measure INNER JOIN vacuum ON (measure.id_measure=vacuum.id_measure AND measure.id_conditioning=vacuum.id_conditioning) ";
}
if (!strcmp($_POST['param'],"Pic")) {
	$requete .= "power.Pic AS 'Pic' FROM measure INNER JOIN power ON (measure.id_measure=power.id_measure AND measure.id_conditioning=power.id_conditioning) ";
}
if (!strcmp($_POST['param'],"Pch")) {
	$requete .= "power.Pch AS 'Pch' FROM measure INNER JOIN power ON (measure.id_measure=power.id_measure AND measure.id_conditioning=power.id_conditioning) ";
}
if (!strcmp($_POST['param'],"Prc")) {
	$requete .= "power.Prc AS 'Prc' FROM measure INNER JOIN power ON (measure.id_measure=power.id_measure AND measure.id_conditioning=power.id_conditioning) ";
}
if (!strcmp($_POST['param'],"PT100_1w")) {
	$requete .= "tceramic.PT100_1w AS 'PT100_1w' FROM measure INNER JOIN tceramic ON (measure.id_measure=tceramic.id_measure AND measure.id_conditioning=tceramic.id_conditioning) ";
}
if (!strcmp($_POST['param'],"PT100_1c")) {
	$requete .= "tceramic.PT100_1c AS 'PT100_1c' FROM measure INNER JOIN tceramic ON (measure.id_measure=tceramic.id_measure AND measure.id_conditioning=tceramic.id_conditioning) ";
}
if (!strcmp($_POST['param'],"PT100_2w")) {
	$requete .= "tceramic.PT100_2w AS 'PT100_2w' FROM measure INNER JOIN tceramic ON (measure.id_measure=tceramic.id_measure AND measure.id_conditioning=tceramic.id_conditioning) ";
}
if (!strcmp($_POST['param'],"PT100_2c")) {
	$requete .= "tceramic.PT100_2c AS 'PT100_2c' FROM measure INNER JOIN tceramic ON (measure.id_measure=tceramic.id_measure AND measure.id_conditioning=tceramic.id_conditioning) ";
}
$requete .= "WHERE measure.id_conditioning = ";
$requete .= $idCond;
if ( (!strcmp($_POST['param'],"V1")) || (!strcmp($_POST['param'],"V2")) || (!strcmp($_POST['param'],"V3")))
	$requete .= " AND vacuum.id_stand = ";
if ((!strcmp($_POST['param'],"Pic")) || (!strcmp($_POST['param'],"Pch")) || (!strcmp($_POST['param'],"Prc")))
	$requete .= " AND power.id_stand = ";
if ((!strcmp($_POST['param'],"PT100_1w")) || (!strcmp($_POST['param'],"PT100_1c")) || (!strcmp($_POST['param'],"PT100_2w")) || (!strcmp($_POST['param'],"PT100_2c")))
	$requete .= " AND tceramic.id_stand = ";
$requete .= "'" .$banc ."'";

$req_hour_param = mysql_query($requete);
$res = mysql_num_rows($req_hour_param);                    // compte le nb d'enregistrements
if ($res==0)//pas d'enregistrement
{
	echo 'empty set';
	mysql_close($db);// fermeture connexion
	exit;
}
// on fait une boucle qui va faire un tour pour chaque enregistrement
$index=0; 
while($data=mysql_fetch_assoc($req_hour_param)) 
    { 
    $data_measure[$index] = $data['Date'];
	if (!strcmp($_POST['param'],"V1"))
	    $data_param[$index] = $data['V1'];
	if (!strcmp($_POST['param'],"V2"))
	    $data_param[$index] = $data['V2'];
	if (!strcmp($_POST['param'],"V3"))
	    $data_param[$index] = $data['V3'];
	if (!strcmp($_POST['param'],"Pic"))
	    $data_param[$index] = $data['Pic'];
	if (!strcmp($_POST['param'],"Pch"))
	    $data_param[$index] = $data['Pch'];
	if (!strcmp($_POST['param'],"Prc"))
	    $data_param[$index] = $data['Prc'];
	if (!strcmp($_POST['param'],"PT100_1w"))
	    $data_param[$index] = $data['PT100_1w'];
	if (!strcmp($_POST['param'],"PT100_1c"))
	    $data_param[$index] = $data['PT100_1c'];
	if (!strcmp($_POST['param'],"PT100_2w"))
	    $data_param[$index] = $data['PT100_2w'];
	if (!strcmp($_POST['param'],"PT100_2c"))
	    $data_param[$index] = $data['PT100_2c'];
//	echo $data_measure[$index] . $data_param[$index];
    $index++;
    }

mysql_close($db);// fermeture connexion

for( $i=0; $i < $index; ++$i ) {
   $xdata[$i] = $data_measure[$i];
}

$xdata[$i] = $start + $i * SAMPLERATE;

//DEFINE('BKG_COLOR','lightgray:1.7');
DEFINE('BKG_COLOR','green:1.98');
DEFINE('WIND_HEIGHT',800);
DEFINE('WIND_WIDTH',250);

//------------------------------------------------------------------
// Setup the Param graph
//------------------------------------------------------------------
$graph = new Graph(WIND_WIDTH,WIND_HEIGHT);
$graph->SetMarginColor(BKG_COLOR);
//$graph->SetScale('datlin',0,1000);
if ( (!strcmp($_POST['param'],"V1")) || (!strcmp($_POST['param'],"V2")) || (!strcmp($_POST['param'],"V3")))
	$graph->SetScale('datlin',0,10000);
if ((!strcmp($_POST['param'],"Pic")) || (!strcmp($_POST['param'],"Pch")) || (!strcmp($_POST['param'],"Prc")))
	$graph->SetScale('datlin',0,1000);
if ((!strcmp($_POST['param'],"PT100_1w")) || (!strcmp($_POST['param'],"PT100_1c")) || (!strcmp($_POST['param'],"PT100_2w")) || (!strcmp($_POST['param'],"PT100_2c")))
	$graph->SetScale('datlin',0,50);
$graph->Set90AndMargin(50,10,70,30);
$graph->SetFrame(true,'white',0);
$graph->SetBox();

//$graph->title->Set('Pic');
$graph->title->Set($param);
$graph->title->SetColor('red');
$graph->title->SetFont(FF_FONT1,FS_BOLD,48);
$graph->title->SetMargin(5);

$graph->xaxis->SetFont(FF_FONT0,FS_NORMAL,9);

$graph->xaxis->SetTickLabels($xdata);

$graph->xgrid->Show();

$graph->yaxis->SetLabelAngle(90);
$graph->yaxis->SetColor('red');
$graph->yaxis->SetFont(FF_FONT0,FS_NORMAL,9);
$graph->yaxis->SetLabelMargin(0);
$graph->yaxis->scale->SetAutoMin(10);

$line = new LinePlot($data_param);
$line->SetStepStyle();
$line->SetColor('red');

$graph->AddLine($line);

//-----------------------
// Create a multigraph
//----------------------
$mgraph = new MGraph();
$mgraph->SetMargin(2,2,2,2);
$mgraph->SetFrame(true,'darkgray',2);
$mgraph->SetFillColor(BKG_COLOR);
$mgraph->Add($graph,460,50);
//$mgraph->title->Set('XFEL Banc D 18 mars 2014');
$title = "XFEL Stand ";
$title .=  $banc;
//$title .= " 18 mars 2014";
//echo $data_measure[0];
$title .= " " .$data_measure[0];
$mgraph->title->Set($title);
$mgraph->title->SetFont(FF_FONT2,FS_BOLD,80);
$mgraph->title->SetMargin(8);
$mgraph->Stroke();

}
else {}

?>
