Better Graphs #4
This commit is contained in:
		@@ -1,4 +1,10 @@
 | 
			
		||||
<?php
 | 
			
		||||
class ChartScale{
 | 
			
		||||
	const HOUR = 'info';
 | 
			
		||||
	const DAY = 'warning';
 | 
			
		||||
	const MONTH = 'warning';
 | 
			
		||||
	const YEAR = 'error';
 | 
			
		||||
}
 | 
			
		||||
class ChartManager{
 | 
			
		||||
	function generateChart($data, $min = 0, $max = 100)
 | 
			
		||||
	{
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,7 @@
 | 
			
		||||
<meta charset="UTF-8">
 | 
			
		||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
 | 
			
		||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
 | 
			
		||||
<link rel="stylesheet" href="./app/templates/css/main.css">
 | 
			
		||||
<link rel="stylesheet" href="./app/templates/css/main.css?v2">
 | 
			
		||||
<link rel="stylesheet" href="./app/templates/css/font-awesome.min.css">
 | 
			
		||||
<link rel="stylesheet" href="./app/templates/css/modal.css">
 | 
			
		||||
<link rel="stylesheet" href="./app/templates/css/pre.css">
 | 
			
		||||
 
 | 
			
		||||
@@ -50,34 +50,29 @@ class Ajax extends Template
 | 
			
		||||
		} else if (isset($_POST['subDevice']) && isset($_POST['action']) && $_POST['action'] == "chart") {
 | 
			
		||||
			//TODO lepe rozstrukturovat
 | 
			
		||||
			$subDeviceId = $_POST['subDevice'];
 | 
			
		||||
 | 
			
		||||
			$period = $_POST['period'];
 | 
			
		||||
			$groupBy = $_POST['group'];
 | 
			
		||||
 | 
			
		||||
			$subDevice = SubDeviceManager::getSubDevice($subDeviceId);
 | 
			
		||||
			$records = RecordManager::getAllRecordForGraph($subDeviceId);
 | 
			
		||||
 | 
			
		||||
			$records = RecordManager::getAllRecordForGraph($subDeviceId, $period, $groupBy);
 | 
			
		||||
 | 
			
		||||
			$array = array_column($records, 'value');
 | 
			
		||||
			$arrayTime = array_column($records, 'time');
 | 
			
		||||
			$output = [];
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			foreach ($array as $key => $value) {
 | 
			
		||||
				/*if ($value == 1 || $value == 0)
 | 
			
		||||
				{
 | 
			
		||||
				$output[$key]['y'] = $value;
 | 
			
		||||
			}
 | 
			
		||||
				if ($subDevice['type'] == 'light'){
 | 
			
		||||
					if ($value > 810){
 | 
			
		||||
						$output[$key]['y'] = 1;
 | 
			
		||||
					} else {
 | 
			
		||||
						$output[$key]['y'] = 0;
 | 
			
		||||
	}*/
 | 
			
		||||
	$output[$key]['y'] = $value;
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
				$timeStamp = new DateTime($arrayTime[$key]);
 | 
			
		||||
				$output[$key]['t'] = $timeStamp->format("Y-m-d") . 'T' . $timeStamp->format("H:i:s") . 'Z';
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
			$data = json_encode($output);
 | 
			
		||||
 | 
			
		||||
			$arrayTimeStamps = array_column($records, 'time');
 | 
			
		||||
@@ -87,49 +82,13 @@ foreach ($arrayTimeStamps as $key => $value) {
 | 
			
		||||
 | 
			
		||||
			$labels = json_encode($arrayTimeStamps);
 | 
			
		||||
			$range = RANGES[$subDevice['type']];
 | 
			
		||||
			$graphType = $range['graph'];
 | 
			
		||||
 | 
			
		||||
			header('Content-Type: application/json');
 | 
			
		||||
/*$JSON = '{
 | 
			
		||||
"type": "line",
 | 
			
		||||
"data": {
 | 
			
		||||
"labels": ' . $data . ',
 | 
			
		||||
"datasets": [{
 | 
			
		||||
"data": ' . $data . ',
 | 
			
		||||
"backgroundColor": "#7522bf",
 | 
			
		||||
"lineTension": 0,
 | 
			
		||||
"radius": 5
 | 
			
		||||
}]
 | 
			
		||||
},
 | 
			
		||||
"options": {
 | 
			
		||||
"legend": {
 | 
			
		||||
"display": false
 | 
			
		||||
},
 | 
			
		||||
"scales": {
 | 
			
		||||
"xAxes": [{
 | 
			
		||||
"type": "time",
 | 
			
		||||
"time": {
 | 
			
		||||
"unit": "hour"
 | 
			
		||||
}
 | 
			
		||||
}],
 | 
			
		||||
"yAxes": [{
 | 
			
		||||
"ticks": {
 | 
			
		||||
"min": ' . $range['min'] . ',
 | 
			
		||||
"max": ' . $range['max'] . ',
 | 
			
		||||
"steps": ' . $range['scale'] . '
 | 
			
		||||
}
 | 
			
		||||
}]
 | 
			
		||||
},
 | 
			
		||||
"tooltips": {
 | 
			
		||||
"enabled": false
 | 
			
		||||
},
 | 
			
		||||
"hover": {
 | 
			
		||||
"mode": null
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
}';*/
 | 
			
		||||
 | 
			
		||||
			$JSON = '
 | 
			
		||||
			{
 | 
			
		||||
	"type": "bar",
 | 
			
		||||
				"type": "' . $graphType . '",
 | 
			
		||||
				"data": {
 | 
			
		||||
					"datasets": [{
 | 
			
		||||
						"data": ' . $data . '
 | 
			
		||||
@@ -159,17 +118,7 @@ $JSON = '
 | 
			
		||||
						"mode": true
 | 
			
		||||
					}
 | 
			
		||||
				}
 | 
			
		||||
}
 | 
			
		||||
';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
*/
 | 
			
		||||
 | 
			
		||||
			}';
 | 
			
		||||
			echo $JSON;
 | 
			
		||||
			die();
 | 
			
		||||
		} else if (isset($_POST['action']) && $_POST['action'] == "getState") {
 | 
			
		||||
@@ -226,3 +175,42 @@ die();
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*$JSON = '{
 | 
			
		||||
"type": "line",
 | 
			
		||||
"data": {
 | 
			
		||||
"labels": ' . $data . ',
 | 
			
		||||
"datasets": [{
 | 
			
		||||
"data": ' . $data . ',
 | 
			
		||||
"backgroundColor": "#7522bf",
 | 
			
		||||
"lineTension": 0,
 | 
			
		||||
"radius": 5
 | 
			
		||||
}]
 | 
			
		||||
},
 | 
			
		||||
"options": {
 | 
			
		||||
"legend": {
 | 
			
		||||
"display": false
 | 
			
		||||
},
 | 
			
		||||
"scales": {
 | 
			
		||||
"xAxes": [{
 | 
			
		||||
"type": "time",
 | 
			
		||||
"time": {
 | 
			
		||||
"unit": "hour"
 | 
			
		||||
}
 | 
			
		||||
}],
 | 
			
		||||
"yAxes": [{
 | 
			
		||||
"ticks": {
 | 
			
		||||
"min": ' . $range['min'] . ',
 | 
			
		||||
"max": ' . $range['max'] . ',
 | 
			
		||||
"steps": ' . $range['scale'] . '
 | 
			
		||||
}
 | 
			
		||||
}]
 | 
			
		||||
},
 | 
			
		||||
"tooltips": {
 | 
			
		||||
"enabled": false
 | 
			
		||||
},
 | 
			
		||||
"hover": {
 | 
			
		||||
"mode": null
 | 
			
		||||
}
 | 
			
		||||
}
 | 
			
		||||
}';*/
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user