【发布时间】:2021-05-25 16:33:02
【问题描述】:
首先我道歉,我不是程序员(但我正在学习 - 慢慢地)。我对园艺应用的图表很感兴趣。根据 select 语句,我有一个每小时从传感器获取数据的数据库,查询获取最后 12 到 48 个读数。在您论坛的帮助下,我创建了 3 个文件,它们一起从 MySQL 中提取数据以绘制具有多个系列的图形:时间戳(具有各种显示选项)、温度和湿度。
我的工作基于小提琴 https://jsfiddle.net/gh/get/library/pure/highcharts/highcharts/tree/master/samples/highcharts/demo/line-basic 中的这个示例,它使用“在脚本中”提供的数据,但三周后我无法从 JSON 注入数据
我的第一个文件建立 MySQL db 连接,第二个文件提取数据并将其格式化为 JSON 数据,第三个文件应该创建图形但没有:-(。
this is what is produced 每行都有一个系列标签,而不是每个系列都有一行。
你能帮帮我吗?我想要一个显示温度和湿度的折线图。沿底部 x 轴的时间/日期,左侧 y 轴温度(以度为单位)和右侧 y 轴显示百分比湿度。我是不是要求太多了?
如果可能的话,最后我能否请求不使用 Ajax 或除 php、html、JSON 和 javascripts 之外的其他“东西”?
任何关于格式化我的问题的帮助将不胜感激:-)
<?php $json_data = include ('nw_database02.php');?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Line Graph</title>
</head>
<body>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<script src="https://code.highcharts.com/modules/series-label.js"></script>
<div id="container"></div>
<script type="text/javascript">
Highcharts.chart('container', {
title: {
text: 'Temperature and Humidity'
},
subtitle: {
text: 'Source: Greenhouse1'
},
yAxis: {
title: {
text: 'Temperature'
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'middle'
},
plotOptions: {
series: {
label: {
connectorAllowed: false
},
pointStart: 2010
}
},
series: <?= $json_data?> ,
responsive: {
rules: [{
condition: {
maxWidth: 500
},
chartOptions: {
legend: {
layout: 'horizontal',
align: 'center',
verticalAlign: 'bottom'
}
}
}]
}
});
</script>
</body>
</html>
...
如果我回显 $json_data 我会得到这种类型的结果,但请记住它是动态数据并且每小时都会更改,因此每次访问页面时都必须从 json_data 读取它:
[{"时间戳":"10:04 02/01/21","温度":"5","湿度":"66"},{"时间戳":"10:19 02/01/ 21","温度":"6","湿度":"65"},{"时间戳":"10:35 02/01/21","温度":"6","湿度":"64 "},{"时间戳":"10:50 02/01/21","温度":"6","湿度":"64"},{"时间戳":"11:06 02/01/21 ","温度":"6","湿度":"64"},{"时间戳":"11:21 02/01/21","温度":"7","湿度":"63" },{"时间戳":"11:34 02/01/21","温度":"10","湿度":"66"},{"时间戳":"04:21 02/01/21" ,"温度":"15","湿度":"64"},{"时间戳":"02/01/21","温度":"16","湿度":"61"} ,{"时间戳":"04:51 02/01/21","温度":"15","湿度":"59"},{"时间戳":"05:07 02/01/21", "温度":"15","湿度":"60"},{"时间戳":"05:22 02/01/21","温度":"14","湿度":"61"}]
【问题讨论】:
标签: php mysql json highcharts