我正在使用树莓派pi3灯服务器来显示来自一个AM2302传感器的数据。我有一个php文件,它从传感器读取值并以json格式显示。
我需要有关如何使用Google区域图显示历史数据的帮助,数据是从python程序读取并每分钟存储到mysql数据库中的
这是读取数据的文件,称为:read_sensors.php(从传感器收集数据的文件)。
<?php
// Settings
// host, user and password settings
$host = "localhost";
$user = "logger";
$password = "password";
$database = "temperatures";
// make connection to database
$connectdb = mysqli_connect($host,$user,$password)
or die ("Cannot reach database");
// select db
mysqli_select_db($connectdb,$database)
or die ("Cannot select database");
// sql command that selects all entires from current time and X hours backward
$sql= "SELECT temperature, humidity, sensor, dateandtime FROM temperaturedata where sensor = 'Exterior' order by dateandtime desc LIMIT 10";
// set query to variable
$temperatures = mysqli_query($connectdb,$sql);
// create content to web page
?>
<?php
// loop all the results that were read from database and "draw" to web page
$temps = [];
while($temperature = mysqli_fetch_assoc($temperatures)) {
$temps[] = $temperature;
}
echo json_encode($temps);;
?>
它向我显示了以下结果:
[
{"temperature":"26.8","humidity":"82.2","sensor":"Exterior","dateandtime":"2019-03-11 09:21:02"},
{"temperature":"26.8","humidity":"83.7","sensor":"Exterior","dateandtime":"2019-03-11 09:20:01"},
{"temperature":"26.8","humidity":"82.8","sensor":"Exterior","dateandtime":"2019-03-11 09:19:01"},
{"temperature":"26.8","humidity":"82.7","sensor":"Exterior","dateandtime":"2019-03-11 09:18:01"},
{"temperature":"27","humidity":"82.6","sensor":"Exterior","dateandtime":"2019-03-11 09:17:01"},
{"temperature":"27.2","humidity":"83","sensor":"Exterior","dateandtime":"2019-03-11 09:16:02"},
{"temperature":"27.2","humidity":"83.1","sensor":"Exterior","dateandtime":"2019-03-11 09:15:01"},
{"temperature":"27.1","humidity":"82.8","sensor":"Exterior","dateandtime":"2019-03-11 09:14:02"},
{"temperature":"27.1","humidity":"82.9","sensor":"Exterior","dateandtime":"2019-03-11 09:13:01"},
{"temperature":"27","humidity":"82.8","sensor":"Exterior","dateandtime":"2019-03-11 09:12:01"}
]
这是我的index.php文件的代码:
<html>
<head>
<title>Google Charts Tutorial</title>
<script type = "text/javascript" src =
"https://www.gstatic.com/charts/loader.js">
</script>
<script type = "text/javascript">
google.charts.load('current', {packages: ['corechart']});
</script>
</head>
<body>
<div id = "container" style = "width: 550px; height: 400px; margin: 0 auto">
</div>
<script language = "JavaScript">
function drawChart() {
// Define the chart to be drawn.
var dataArea = new google.visualization.DataTable();
dataArea.addColumn('string', 'Date');
dataArea.addColumn('number', 'Temperature');
dataArea.addColumn('number', 'Humidity');
var options = {title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: '#333'}},
vAxis: {minValue: 0}
};
$.ajax({
url: "read_sensors.php",
dataType: 'json'
}).done(function (data) {
$.each(data, function (index, row) {
dataArea.addRow([
row.dateandtime,
parseFloat(row.temperature),
parseFloat(row.humidity)
]);
});
});
var chartArea = new
google.visualization.AreaChart(document.getElementById("chart_area"));
chartArea.draw(dataArea);
}
google.charts.setOnLoadCallback(drawChart);
</script>
</body>
</html>
此时,当我访问index.php时,页面显示为空白(无)
下面是带有代码的代码:
<html>
<head>
<title>Google Charts Tutorial</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
</script>
<script type = "text/javascript">
</script>
</head>
<body>
<div id = "chart_area" style = "width: 550px; height: 400px; margin: 0 auto">
</div>
<script language = "JavaScript">
function drawChart() {
// Define the chart to be drawn.
var dataArea = new google.visualization.DataTable();
dataArea.addColumn('string', 'Date');
dataArea.addColumn('number', 'Temperature');
dataArea.addColumn('number', 'Humidity');
var options = {title: 'Company Performance',
hAxis: {title: 'Year', titleTextStyle: {color: '#333'}},
vAxis: {minValue: 0}
};
$.ajax({
url: "read_sensors.php",
dataType: 'json',
}).done(function (data) {
complete: function(data) {
$.each(data, function(index, row) {
dataArea.addRow([
row.dateandtime,
parseFloat(row.temperature),
parseFloat(row.humidity)
]);
});
var chartArea = new google.visualization.AreaChart(document.getElementById("chart_area"));
chartArea.draw(dataArea);
}
});
}
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);
</script>
</body>
</html>
答案 0 :(得分:2)
在添加数据之前绘制图表。
将图表绘制代码移动到ajax完成的回调中...
$.ajax({
url: "read_sensors.php",
dataType: 'json'
}).done(function (data) {
$.each(data, function (index, row) {
dataArea.addRow([
row.dateandtime,
parseFloat(row.temperature),
parseFloat(row.humidity)
]);
});
var chartArea = new
google.visualization.AreaChart(document.getElementById("chart_area"));
chartArea.draw(dataArea, options);
});
此外,<div>
元素的ID必须与提供给图表的ID相匹配。
在此处将ID更改为"chart_area"
<div id="chart_area" style="width: 550px; height: 400px; margin: 0 auto">
</div>
您可能还需要将load
语句移至与回调相同的部分...
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);
还需要向页面添加jquery,您可以在google图表库上方添加...
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
以下是建议的修改...
<html>
<head>
<title>Google Charts Tutorial</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://www.gstatic.com/charts/loader.js"></script>
</head>
<body>
<div id="chart_area" style="width: 550px; height: 400px; margin: 0 auto"></div>
<script>
function drawChart() {
var dataArea = new google.visualization.DataTable();
dataArea.addColumn('string', 'Date');
dataArea.addColumn('number', 'Temperature');
dataArea.addColumn('number', 'Humidity');
var options = {
title: 'Company Performance',
hAxis: {
title: 'Year',
titleTextStyle: {color: '#333'}
},
vAxis: {minValue: 0}
};
$.ajax({
url: "read_sensors.php",
dataType: 'json',
}).done(function (data) {
$.each(data, function(index, row) {
dataArea.addRow([
row.dateandtime,
parseFloat(row.temperature),
parseFloat(row.humidity)
]);
});
var chartArea = new google.visualization.AreaChart(document.getElementById("chart_area"));
chartArea.draw(dataArea);
});
}
google.charts.load('current', {packages: ['corechart']});
google.charts.setOnLoadCallback(drawChart);
</script>
</body>
</html>
答案 1 :(得分:0)
对于jQuery.ajax
,您应该尝试使用complete
选项而不是完成操作:
$.ajax({
url: "read_sensors.php",
dataType: 'json',
// NOTE: callback for $.ajax.done(...) goes here
complete: function(data) {
$.each(data, function(index, row) {
dataArea.addRow([
row.dateandtime,
parseFloat(row.temperature),
parseFloat(row.humidity)
]);
});
var chartArea = new google.visualization.AreaChart(document.getElementById("chart_area"));
chartArea.draw(dataArea);
}
});
如果这不起作用,则应检查CSS {inline或其他)display: none
,或者检查JavaScript中的hide
/ fadeOut
/ slideDown
。
如果这不起作用,并且您正在使用jQuery 2(或3),则应使用jQuery.when
。
例如:
$.when(function() {
// code that you need to wait for
}).done(function() {
// code that runs after code above is done
});
祝你好运。