diff --git a/app/class/Utilities.php b/app/class/Utilities.php index 87767bb..f0ec0d3 100644 --- a/app/class/Utilities.php +++ b/app/class/Utilities.php @@ -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); + } } diff --git a/app/views/Ajax.php b/app/views/Ajax.php index 485e4d1..0ece2a7 100644 --- a/app/views/Ajax.php +++ b/app/views/Ajax.php @@ -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