我需要使用来自2个TCP命令的PHP生成2个JSON文件,以构建Highchart图。
第一个JSON文件将包含图形信息: 图形标题,图形字幕,Y轴标题和与要显示的图形类型相关的数字。
TCP字符串(来自IoT远程板)向服务器发送以下参数:
HTTP:MYSERVER /图形/补充json_info.php jsonfilename = S_007_nfo.json&安培; graphtitle = S_007_Sensor&安培; graphsubtitle =办公室-温度&安培; Yaxistitle =温度-传感器&安培; GraphType = 3
如何使用名为' make-json_info.php'的PHP文件制作JSON文件?从这个TCP字符串?
JSON文件应如下所示:
{
chart: {
type: '/*...*/'
},
xAxis: {/*...*/},
yAxis: {/*...*/},
plotOptions: {/*...*/}
/*...etc...*/
}
关于生成图表所需的2sd JSON文件,IoT板每分钟发送一个包含以下内容的TCP字符串:
HTTP:MYSERVER /图形/补充json_data.php S_007_data.json&安培; DATATIME = 1488271800000&安培;值= 22.5
预期的JSON文件应该是这样的: http://s529471052.onlinehome.fr/graphs/S_007_data.json
你能告诉我如何编写我的两个PHP文件,这些文件应该生成2个JSON文件,以便之后构建预期的Highcharts Graph吗?
到目前为止,我试图从JSON数据文件中提取信息,而JSON信息文件则缺少图形信息。
see jsfiddle below
答案 0 :(得分:0)
我不确定你问的很多事情,但我会尽我所能。
包含图形信息的JSON文件...它是否仅包含有关图形的元信息(标题,类型等),或者该文件是否还包含xAxis数据和yAxis数据......
从你问题的这两行看来,它似乎也可能包含实际数据:
xAxis: {/*...*/},
yAxis: {/*...*/},
道歉,如果我弄错了,但这就是我想出来的。 它没有经过测试,但应该让您知道该怎么做:
让我们先从第二个文件/脚本开始。
<?php
$file_name = $_GET['jsonfilename'];
$datatime = $_GET['datatime'];
$value = $_GET['value'];
$file_content = file_get_contents($file_name); //read data from json file
$temp_file_data = json_decode($file_content, true); //convert json string into php array
$temp_file_data['date'][] = [$datatime, $value]; //add your new values to the array
$new_file_content = json_encode($temp_file_data); //encode your new array as a json string
file_put_contents($file_name, $new_file_content); //save the new json string back to the file
?>
现在是第一个PHP脚本。此脚本将使用上面代码中的json文件创建图形将使用的完整json文件:
<?php
$file_name = $_GET['jsonfilename'];
$graphtitle = $_GET['graphtitle'];
$graphsubtitle= $_GET['graphsubtitle'];
$Yaxistitle= $_GET['Yaxistitle'];
$GraphType = $_GET['GraphType'];
$data_json = file_get_contents($file_name); //read data from json file
$data_array = json_decode($file_content, true); //convert json string into php array
$xAxis = []; //create array that will contain all xAxis values
$yAxis = []; //create array that will contain all yAxis values
for($i = 0; $i < sizeof($data_array['data']); $i++)
{
$xAxis[] = $data_array['data'][$i][0]; //add xAxis data from first json file to the xAxis array
$yAxis[] = $data_array['data'][$i][1]; //add yAxis data from first json file to the yAxis array
}
$json_array = []; //declare the array that will be converted to the final JSON string that the graph will use
$json_array['chart'] = ['type' => $GraphType];
$json_array['xAxis'] = $xAxis;
$json_array['yAxis'] = $yAxis;
$json_array['plotOptions'] = ['graphtitle' => $graphtitle,
'graphsubtitle' => $graphsubtitle,
'Yaxistitle' => $Yaxistitle];
?>