<?php
include_once 'php-ofc-library/open-flash-chart.php';
include_once 'cfg/mysql.php';
connectdb();

$room = $_COOKIE['room'];
$description = $_COOKIE['description'];

if($description=='Radiator Rechts' OR $description=='Radiator Links' OR $description=='Luchtuitlaat temperatuursensor' OR $description=='atrium, 1e' OR $description=='atrium, 3e'  OR $description=='buiten' OR $description=='kamer, buro')
{
	$result = mysql_query("SELECT TIME(start) AS xaxis, (value) as value, sensors.description as description FROM tuples LEFT JOIN sensors ON tuples.sensorID=sensors.sensorID WHERE (roomID=$room) AND (sensors.description='$description') ORDER BY start limit 100");
}
else
{
	$result = mysql_query("SELECT TIME(start) AS xaxis, (value*1000) as value, sensors.description as description FROM tuples LEFT JOIN sensors ON tuples.sensorID=sensors.sensorID WHERE (end IS NOT NULL) AND (roomID=$room) AND (sensors.description='$description') ORDER BY start limit 100");
}

$result2 = mysql_query("SELECT MAX(value) AS yaxis from tuples ORDER BY start limit 100");

$data = array();
while($row = mysql_fetch_array($result))
{
  $data[] = intval($row['value']);
  $xaxis[] = ($row['xaxis']);
}

while($row = mysql_fetch_array($result2))
{
  $yaxis = intval($row['yaxis']);
}

$title = new title('Kamer '.$room);

$yaxisnew = (ceil($yaxis / 50)) * 50;

//-- LINE 1 --\\

$line_1_default_dot = new dot();
$line_1_default_dot->size(4)->halo_size(2)->colour('#339933')->on_click("line_1");
$line_1 = new line();
$line_1->set_default_dot_style($line_1_default_dot);
$line_1->set_values( $data );
$line_1->set_width( 2 );
$line_1->set_colour('#f7921c');
$line_1->set_key($description, 14);


//-- Y AXIS --\\

$y = new y_axis();
$y->set_range( 0, 300, 30 );
$y->set_colour('#000000');
$y->set_grid_colour('#D8D8D8');


//-- X AXIS --\\

$x = new x_axis();
$x->offset(false)->steps(10);
$x->colour('#000000')->grid_colour('#D8D8D8');

$x_labels = new x_axis_labels();
$x_labels->set_steps( 10 );
$x_labels->set_colour( '#000000' );
$x_labels->set_labels( $xaxis );
$x->set_labels( $x_labels );


//--CHART --\\

$chart = new open_flash_chart();
$chart->set_title( $title );
$chart->add_element( $line_1 );
$chart->set_x_axis( $x );
$chart->set_y_axis( $y );
$chart->set_bg_colour( '#FFFFFF' );

echo $chart->toPrettyString();
$chartwrite = $chart->toPrettyString();

$fh = fopen("graphs/".$room."_".$description.".txt", 'w') or die("can't open file");
fwrite($fh, $chartwrite);
fclose($fh);


?>