Better Graphs #4

This commit is contained in:
JonatanRek 2019-09-29 09:23:42 +02:00
parent 7e047ee0a3
commit fdf94f8b8d
3 changed files with 121 additions and 127 deletions

View File

@ -1,4 +1,10 @@
<?php <?php
class ChartScale{
const HOUR = 'info';
const DAY = 'warning';
const MONTH = 'warning';
const YEAR = 'error';
}
class ChartManager{ class ChartManager{
function generateChart($data, $min = 0, $max = 100) function generateChart($data, $min = 0, $max = 100)
{ {

View File

@ -18,7 +18,7 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge"> <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/font-awesome.min.css">
<link rel="stylesheet" href="./app/templates/css/modal.css"> <link rel="stylesheet" href="./app/templates/css/modal.css">
<link rel="stylesheet" href="./app/templates/css/pre.css"> <link rel="stylesheet" href="./app/templates/css/pre.css">

View File

@ -50,86 +50,45 @@ class Ajax extends Template
} else if (isset($_POST['subDevice']) && isset($_POST['action']) && $_POST['action'] == "chart") { } else if (isset($_POST['subDevice']) && isset($_POST['action']) && $_POST['action'] == "chart") {
//TODO lepe rozstrukturovat //TODO lepe rozstrukturovat
$subDeviceId = $_POST['subDevice']; $subDeviceId = $_POST['subDevice'];
$period = $_POST['period'];
$groupBy = $_POST['group'];
$subDevice = SubDeviceManager::getSubDevice($subDeviceId); $subDevice = SubDeviceManager::getSubDevice($subDeviceId);
$records = RecordManager::getAllRecordForGraph($subDeviceId); $records = RecordManager::getAllRecordForGraph($subDeviceId, $period, $groupBy);
$array = array_column($records, 'value'); $array = array_column($records, 'value');
$arrayTime = array_column($records, 'time'); $arrayTime = array_column($records, 'time');
$output = []; $output = [];
foreach ($array as $key => $value) { foreach ($array as $key => $value) {
/*if ($value == 1 || $value == 0)
{
$output[$key]['y'] = $value; $output[$key]['y'] = $value;
} if ($subDevice['type'] == 'light'){
if ($value > 810){ if ($value > 810){
$output[$key]['y'] = 1; $output[$key]['y'] = 1;
} else { } else {
$output[$key]['y'] = 0; $output[$key]['y'] = 0;
}*/ }
$output[$key]['y'] = $value; }
$timeStamp = new DateTime($arrayTime[$key]); $timeStamp = new DateTime($arrayTime[$key]);
$output[$key]['t'] = $timeStamp->format("Y-m-d") . 'T' . $timeStamp->format("H:i:s") . 'Z'; $output[$key]['t'] = $timeStamp->format("Y-m-d") . 'T' . $timeStamp->format("H:i:s") . 'Z';
} }
$data = json_encode($output);
$arrayTimeStamps = array_column($records, 'time');
$data = json_encode($output); foreach ($arrayTimeStamps as $key => $value) {
$arrayTimeStamps = array_column($records, 'time');
foreach ($arrayTimeStamps as $key => $value) {
$arrayTimeStamps[$key] = (new DateTime($value))->format(TIMEFORMAT); $arrayTimeStamps[$key] = (new DateTime($value))->format(TIMEFORMAT);
} }
$labels = json_encode($arrayTimeStamps); $labels = json_encode($arrayTimeStamps);
$range = RANGES[$subDevice['type']]; $range = RANGES[$subDevice['type']];
header('Content-Type: application/json'); $graphType = $range['graph'];
/*$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 = ' header('Content-Type: application/json');
{
"type": "bar", $JSON = '
{
"type": "' . $graphType . '",
"data": { "data": {
"datasets": [{ "datasets": [{
"data": ' . $data . ' "data": ' . $data . '
@ -159,20 +118,10 @@ $JSON = '
"mode": true "mode": true
} }
} }
} }';
'; echo $JSON;
die();
} else if (isset($_POST['action']) && $_POST['action'] == "getState") {
/*
*/
echo $JSON;
die();
} else if (isset($_POST['action']) && $_POST['action'] == "getState") {
//State Update //State Update
$roomsData = RoomManager::getAllRooms(); $roomsData = RoomManager::getAllRooms();
$subDevices = []; $subDevices = [];
@ -213,16 +162,55 @@ die();
} }
echo json_encode($subDevices); echo json_encode($subDevices);
die(); die();
} else if (isset($_POST['scene_id'])) { } else if (isset($_POST['scene_id'])) {
$sceneId = $_POST['scene_id']; $sceneId = $_POST['scene_id'];
if (isset($_POST['action']) && $_POST['action'] == 'delete') { if (isset($_POST['action']) && $_POST['action'] == 'delete') {
SceneManager::delete($sceneId); SceneManager::delete($sceneId);
}else { }else {
echo SceneManager::execScene($sceneId); echo SceneManager::execScene($sceneId);
} }
}
die();
}
} }
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
} }
} }
}';*/