<?php
include ("/var/www/jpgraph-2.3/src/jpgraph.php");
include ("/var/www/jpgraph-2.3/src/jpgraph_line.php");


mysql_connect("localhost", "anvid", "lösen") or die("Kan ej ansluta: " . mysql_error());
mysql_select_db("databasnamn") or die("Kan ej öppna databas vp_home");

if (isset($_GET['From'])) {
   $fran = $_GET['From'];
   $till = $_GET['To'];
} elseif (isset($_GET['Day'])) {
   $fran = Date("Y-m-d H:i",strtotime($_GET['Day']));
   $till = Date("Y-m-d H:i",strtotime($fran) +(1 * 6 * 60 * 60));
}
else {
$till = Date("Y-m-d H:i", time());
$fran = Date("Y-m-d H:i",strtotime($till) - (1 * 6 * 60 * 60));
}

$query = "SELECT `kbin`, `kbut`, `gvuf`, `gvur`, `rf`, `rr`, `vv`, `ute`,`kallare`,`plan1`, `time` FROM databasnamn WHERE time >= '$fran' and time < '$till'"; 
$result = mysql_query($query) or die("SQL fråga felaktig: " . mysql_error()); 


while( $myrow=mysql_fetch_row($result) )
{
      $ydataa[] = $myrow[5]; 
      $ydatab[] = $myrow[4]; 
      $ydatac[] = $myrow[3]; 
      $ydatad[] = $myrow[2]; 
      $ydatae[] = $myrow[1]; 
      $ydataf[] = $myrow[0]; 
      $ydatag[] = $myrow[6]; 
      $ydatah[] = $myrow[7];  
      $ydatai[] = $myrow[8]; 
      $ydataj[] = $myrow[9];  

      $xdata[] = Date("H:i", strtotime( $myrow[10] ) );
} 

// Create the graph. These two calls are always required
$graph = new Graph(1000,550);   
$graph->SetScale("textlin",-25,55);


$graph->title->Set("Värmepumpdata:  ".Date("Y-m-d H:i",strtotime($fran))." -> ".Date("Y-m-d H:i",strtotime($till)));

$graph ->img->SetMargin(40,20,30,70);
$graph ->xaxis->SetTextTickInterval(1);
$graph ->xaxis->SetTextLabelInterval(  count($xdata)/12);
$graph ->xaxis->scale->ticks->SetSize(8);
$graph ->xaxis->SetTickLabels($xdata);

$graph ->xaxis->SetLabelMargin(41); 
$graph ->xaxis->SetLabelMargin(40);
$graph ->xaxis->SetFont(FF_TIMES);
$graph ->xaxis->HideFirstTicklabel();
$graph ->xaxis->HideLastTicklabel();

$graph ->yaxis->scale->ticks->SetSize(7);
$graph ->yaxis->SetColor("red");
$graph ->yaxis->SetWeight(2);
$graph ->yaxis->title->Set("Grader");
$graph ->yaxis->title->SetFont(FF_FONT1,FS_BOLD);
 	 
$graph ->SetShadow(); 
$graph ->legend->SetLayout(LEGEND_HOR); 
$graph ->legend->Pos(.5,.94,"center","center"); 

$graph ->ygrid->Show(true,true);
$graph ->xgrid->Show(true,false);
$graph ->ygrid->SetFill(true,'#EFEFEF@0.5','#BBCCFF@0.5'); 


// Create the linear plot
$lineplot2=new LinePlot($ydataa);
$lineplot2 ->SetColor("blue");
$lineplot2 ->SetLegend("Radiator-retur");
$lineplot2 ->SetWeight(2);


// Create the linear plot
$lineplot1=new LinePlot($ydatab);
$lineplot1 ->SetColor("violetred4");
$lineplot1 ->SetLegend("Radiator-fram");
$lineplot1 ->SetWeight(2);

// Create the linear plot
$lineplot3=new LinePlot($ydatac);
$lineplot3 ->SetColor("springgreen");
$lineplot3 ->SetLegend("Golvvärmeuppe-retur");
$lineplot3 ->SetWeight(2);

// Create the linear plot
$lineplot4=new LinePlot($ydatad);
$lineplot4 ->SetColor("red");
$lineplot4 ->SetLegend("Golvvärmeuppe-fram");
$lineplot4 ->SetWeight(2);

// Create the linear plot
$lineplot5=new LinePlot($ydatae);
$lineplot5 ->SetColor("black");
$lineplot5 ->SetLegend("KB-ut");
$lineplot5 ->SetWeight(2);

// Create the linear plot
$lineplot6=new LinePlot($ydataf);
$lineplot6 ->SetColor("yellow3");
$lineplot6 ->SetLegend("KB-in");
$lineplot6 ->SetWeight(2);

// Create the linear plot
$lineplot7=new LinePlot($ydatag);
$lineplot7 ->SetColor("violetred1");
$lineplot7 ->SetLegend("Varmvatten");
$lineplot7 ->SetWeight(2);

// Create the linear plot
$lineplot8=new LinePlot($ydatah);
$lineplot8 ->SetColor("turquoise2");
$lineplot8 ->SetLegend("Ute");
$lineplot8 ->SetWeight(2);

// Create the linear plot
$lineplot9=new LinePlot($ydatai);
$lineplot9 ->SetColor("seagreen");
$lineplot9 ->SetLegend("Källare");
$lineplot9 ->SetWeight(2);

// Create the linear plot
$lineplot10=new LinePlot($ydataj);
$lineplot10 ->SetColor("mistyrose4");
$lineplot10 ->SetLegend("Markplan");
$lineplot10 ->SetWeight(2);

// Add the plot to the graph
$graph->Add($lineplot1);
$graph->Add($lineplot2);
$graph->Add($lineplot4);
$graph->Add($lineplot3);
$graph->Add($lineplot5);
$graph->Add($lineplot6);
$graph->Add($lineplot7);
$graph->Add($lineplot8);
$graph->Add($lineplot9);
$graph->Add($lineplot10);
// Display the graph
$graph->Stroke();
?>
