Better Graphs #4

This commit is contained in:
JonatanRek 2019-09-29 10:18:02 +02:00
parent fdf94f8b8d
commit 92f8486f91
2 changed files with 52 additions and 35 deletions

View File

@ -41,4 +41,54 @@ class Utilities
$str = substr($str, 0, $pos) . $insertstr . substr($str, $pos);
return $str;
}
/**
* [generateGraphJson description]
* @param string $type [line/bar]
* @param array $data [description]
* @param array $options [description]
* @return [type] [description]
*/
function generateGraphJson(string $type = 'line', array $data = [], array $options = []){
$array = [
'type' => $type,
'data' => [
'datasets' => [
[
'data' => $data
]
]
],
'options' => [
'scales' => [
'xAxes' => [
[
'type' => 'time',
'distribution' => 'linear',
]
],
'yAxes' => [
[
'ticks' => [
'min' => $options['min'],
'max' => $options['max'],
'steps' => $options['scale']
]
]
]
],
'legend' => [
'display' => false
],
'tooltips' => [
'enabled' => true
],
'hover' => [
'mode' => true
]
]
];
return json_encode($array, JSON_PRETTY_PRINT);
}
}

View File

@ -74,7 +74,7 @@ class Ajax extends Template
}
$data = json_encode($output);
$data = $output;
$arrayTimeStamps = array_column($records, 'time');
foreach ($arrayTimeStamps as $key => $value) {
$arrayTimeStamps[$key] = (new DateTime($value))->format(TIMEFORMAT);
@ -86,40 +86,7 @@ class Ajax extends Template
header('Content-Type: application/json');
$JSON = '
{
"type": "' . $graphType . '",
"data": {
"datasets": [{
"data": ' . $data . '
}]
},
"options": {
"scales": {
"xAxes": [{
"type": "time",
"distribution": "linear"
}],
"yAxes": [{
"ticks": {
"min": ' . $range['min'] . ',
"max": ' . $range['max'] . ',
"steps": ' . $range['scale'] . '
}
}]
},
"legend": {
"display": false
},
"tooltips": {
"enabled": true
},
"hover": {
"mode": true
}
}
}';
echo $JSON;
echo Utilities::generateGraphJson($range['graph'], $data, $range);
die();
} else if (isset($_POST['action']) && $_POST['action'] == "getState") {
//State Update