<?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');


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

$start = time();
$end = $start+NDATAPOINTS*SAMPLERATE;
$xdata_vacuum_1 = array();
$xdata_vacuum_2 = array();
$xdata_vacuum_3 = array();

//lecture data base
$db = mysql_connect('127.0.0.1', 'root', '');   // connexion
mysql_select_db('xfel',$db);                // selection 

$req_vacuum = mysql_query("SELECT v1,v2,v3 FROM vacuum WHERE id_stand='b'");// requetes
$req_measure = mysql_query("SELECT hour FROM measure");
 
$res = mysql_num_rows($req_vacuum);                    // compte le nb d'enregistrements
//echo 'Il y a '.$res.' enregistrement(s) dans la table xfel : ';// affichage du resultat 

// on fait une boucle qui va faire un tour pour chaque enregistrement
$index=0; 
while($data=mysql_fetch_assoc($req_vacuum)) 
    { 
    $data_vacuum_1[$index] = $data['v1'];
    $data_vacuum_2[$index] = $data['v2'];
    $data_vacuum_3[$index] = $data['v3'];
    $index++;
    } 
$index=0;
while($data=mysql_fetch_assoc($req_measure)) 
    { 
    $data_measure[$index] = $data['hour'];
    $index++;
    } 

mysql_close($db);// fermeture connexion


for( $i=0; $i < $index; ++$i ) {
  //   echo '(',$i,')' . ' ' . $data_measure[$i] . ' ' . $data_vacuum_1[$i] . ' ' . $data_vacuum_2[$i] . ' ' . $data_vacuum_3[$i] . ' - ' ;
   $data_temperature[$i] = $data_vacuum_1[$i];
   $data_vacuum[$i] = $data_vacuum_2[$i];
   $data_power[$i] = $data_vacuum_3[$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 V1 vacuum graph
//------------------------------------------------------------------
$graph = new Graph(WIND_WIDTH,WIND_HEIGHT);
$graph->SetMarginColor(BKG_COLOR);
$graph->SetScale('datlin',0,60);
$graph->Set90AndMargin(50,10,70,30);
$graph->SetFrame(true,'white',0);
$graph->SetBox();

$graph->title->Set('V1');
$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->scale->SetDateFormat('H:i');
$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_temperature,$xdata);
$line = new LinePlot($data_vacuum_1);
$line->SetStepStyle();
$line->SetColor('red');

$graph->AddLine($line);

//$graph->Add($line);

//------------------------------------------------------------------
// Setup the V2 vacuum graph
//------------------------------------------------------------------
$graph2 = new Graph(WIND_WIDTH-30,WIND_HEIGHT);
//$graph2->SetScale('datlin');
$graph2->SetScale('datlin',0,60);
$graph2->Set90AndMargin(5,20,70,30);
$graph2->SetMarginColor(BKG_COLOR);
$graph2->SetFrame(true,'white',0);
$graph2->SetBox();

$graph2->title->Set('V2');
$graph2->title->SetColor('blue');
$graph2->title->SetFont(FF_FONT1,FS_BOLD,48);
$graph2->title->SetMargin(5);

$graph2->xaxis->HideLabels();
$graph2->xgrid->Show();

$graph2->yaxis->SetLabelAngle(90);
$graph2->yaxis->SetColor('blue');
$graph2->yaxis->SetFont(FF_FONT0,FS_NORMAL,9);
$graph2->yaxis->SetLabelMargin(0);
//$graph2->yaxis->scale->SetAutoMin(50);

//$line2 = new LinePlot($data_vacuum,$xdata);
$line2 = new LinePlot($data_vacuum_2);
$line2->SetStepStyle();
$line2->SetColor('blue');

$graph2->AddLine($line2);

//$graph2->Add($line2);

//------------------------------------------------------------------
// Setup the V3 vacuum graph
//------------------------------------------------------------------
$graph3 = new Graph(WIND_WIDTH-30,WIND_HEIGHT);
$graph3->SetScale('datlin',0,60);
$graph3->Set90AndMargin(5,20,70,30);
$graph3->SetMarginColor(BKG_COLOR);
$graph3->SetFrame(true,'white',0);
$graph3->SetBox();

$graph3->title->Set('V3');
$graph3->title->SetColor('black');
$graph3->title->SetFont(FF_FONT1,FS_BOLD,48);
$graph3->title->SetMargin(5);

$graph3->xaxis->HideLabels();
$graph3->xgrid->Show();

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

//$line3 = new LinePlot($data_power,$xdata);
$line3 = new LinePlot($data_vacuum_3);
$line3->SetStepStyle();
$line3->SetColor('black');

$graph3->AddLine($line3);

//$graph3->Add($line3);

//-----------------------
// Create a multigraph
//----------------------
$mgraph = new MGraph();
$mgraph->SetMargin(2,2,2,2);
$mgraph->SetFrame(true,'darkgray',2);
$mgraph->SetFillColor(BKG_COLOR);
$mgraph->Add($graph,0,50);
$mgraph->Add($graph2,250,50);
$mgraph->Add($graph3,460,50);
$mgraph->title->Set('XFEL Stand A August 2013');
$mgraph->title->SetFont(FF_FONT2,FS_BOLD,80);
$mgraph->title->SetMargin(8);
$mgraph->Stroke();

?>
