added testcase for highcharts

highcharts
agp8x 2015-05-07 20:22:05 +02:00
parent c20a864700
commit db8125640b
1 changed files with 179 additions and 0 deletions

179
highcharts.php Normal file
View File

@ -0,0 +1,179 @@
<?php
/**
* Created by PhpStorm.
* User: agp8x
* Date: 07.05.15
* Time: 18:36
*/
include "config.php";
include "lib/DBLib.php";
function average($list)
{
if(!$list){
return null;
}
$i = 0;
$sum = 0;
foreach ($list as $value) {
$i++;
$sum += $value['value']/100;
}
if ($i>0) {
return round($sum / $i, 1);
}else{
return null;
}
}
$db = new DBLib($database['host'], $database['user'], $database['password'], $database['database']);
#echo $db;
$sensors = ["temp1"=> "Innen", "temp2"=>"Aussen"];
$series=array();
foreach ($sensors as $sensor=>$name) {
$date = new DateTime("2015-04-01T00:00:00+02:00", new DateTimeZone("Europe/Berlin"));
$days = array();
for ($i = 0; $i < $date->format("t"); $i++) {
$start = $date->format("U");
#echo $date->format("H:i:s Y.m.d");
#echo " -- ";
#echo $date->format("U");
#echo "\n";
$date->add(new DateInterval("P1D"));
$end = $date->format("U");
$entries = $db->selectRange($sensor, "*", array("time", $start, $end));
$days[] = average($entries);
}
$series[]=array("name"=>$name, "data"=>$days);
}
$json= json_encode($series,JSON_NUMERIC_CHECK /*| JSON_PRETTY_PRINT*/);
$series2=[];
$days = array();
foreach(["temp2"=>"Aussen"] as $sensor=>$name){
$date = new DateTime("2015-04-01T00:00:00+02:00", new DateTimeZone("Europe/Berlin"));
$date2 = new DateTime("2015-04-01T00:00:00+02:00", new DateTimeZone("Europe/Berlin"));
for ($j = 0; $j < $date->format("t"); $j++) {
$hours = [];
for ($i = 0; $i < 24; $i++) {
$start = $date2->format("U");
$date2->add(new DateInterval("PT1H"));
$end = $date2->format("U");
#var_dump(array("time", $start, $end));
$entries = $db->selectRange($sensor, "*", array("time", $start, $end));
$hours[] = average($entries);
}
$days[]=["name"=>$date->format("d.m.Y")." (".$name.")", "data"=>$hours];
$date->add(new DateInterval("P1D"));
}
}
$json2= json_encode($days,JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
#$start = mktime(0, 0, 0, 7, 1, 2014);
#echo date("c",$start);
#echo date("H:i:s Y.m.d",$start);
#echo "\n";
#$days = date("t", $start);
#$end = 60 * 60 * 24 * $days + $start;
#echo date("H:i:s Y.m.d",$end);
#$result = $db->selectRange("temp2", "*", array("time", $start, $end));
#var_dump($result);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Highcharts Example</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<style type="text/css">
${demo.css}
</style>
<script type="text/javascript">
$(function () {
$('#container').highcharts({
title: {
text: 'Monthly Average Temperature',
x: -20 //center
},
subtitle: {
text: 'April 2014\<br\>Source: agp8x.org',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '°C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: <?= $json ?>
});
});
$(function () {
$('#container2').highcharts({
title: {
text: 'Daily Average Temperature',
x: -20 //center
},
subtitle: {
text: 'April 2014\<br\>Source: agp8x.org',
x: -20
},
xAxis: {
categories: []
},
yAxis: {
title: {
text: 'Temperature (°C)'
},
plotLines: [{
value: 0,
width: 1,
color: '#808080'
}]
},
tooltip: {
valueSuffix: '°C'
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle',
borderWidth: 0
},
series: <?= $json2 ?>
});
});
</script>
</head>
<body>
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
<div id="container2" style="min-width: 310px; height: 400px; margin: 0 auto"></div>
</body>
</html>