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

// data z MySQL
  $link = mysqli_connect('192.168.1.3','MirekF','THkmd', 'control');
  $result = mysqli_query($link, "SELECT * FROM esp_data ORDER BY reading_time DESC limit 288");

$rows = mysqli_num_rows($result);
$ydata_u = $ydata_v = $ydata_p = $ydata_g = $ydata_door = $datax = [];

for ($i = $rows - 1; $i >= 0; $i--) {
    $row = mysqli_fetch_array($result, MYSQLI_NUM);
    if (!$row) continue; // ochrana proti null
    $row = mysqli_fetch_array($result, MYSQLI_NUM);
    $ydata_u[$i]=$row[3]; //UVNITR
    $ydata_v[$i]=$row[2]; //VENKU
    $ydata_p[$i]=$row[4]; //Podlaha
    $ydata_g[$i]=$row[20]; //Garáž
    $ydata_door[$i]=$row[21]; //Vrata
    $datax[$i]=substr($row[1],10,6);
}
  
// Create the graph. These two calls are always required
$graph = new Graph(600,400);
$graph->SetScale('textlin');

// Increase the margins (left,right,top,bottom)
$graph->img->SetMargin(40,20,20,40);

// Add graph and axis title
$graph->title->Set("Node1 24 hodin");
$graph->xaxis->title->Set("čas");
$graph->yaxis->title->Set("teploty C");
$graph->yaxis->SetColor("red");
$graph->yaxis->SetWeight(2);

// Create the linear plot
$lineplot=new LinePlot($ydata_u);
$lineplot->SetColor('violet');
$lineplot2=new LinePlot($ydata_v);
$lineplot2->SetColor('red');
$lineplot3=new LinePlot($ydata_p);
$lineplot3->SetColor('brown');
$lineplot4=new LinePlot($ydata_g);
$lineplot4->SetColor('black');
$lineplot5=new LinePlot($ydata_door);
$lineplot5->SetColor('red');

// Add the plot to the graph
$graph->Add($lineplot);
$graph->Add($lineplot2);
$graph->Add($lineplot3);
$graph->Add($lineplot4);
$graph->Add($lineplot5);
$graph->legend->Pos(0.05,0.5,"right","center");

//X Axis
$graph->xaxis->SetTickLabels($datax);
$graph->xaxis->SetTextTickInterval(24);

// Display the graph
$graph->Stroke();
?>
