我有两个php文件data.php和gaschart.php,data.php从sql中提取数据,gaschart.php绘制 Highcharts 。我想做到实时,这意味着每秒更新图表,数据库也每秒更新。任何人请帮我解决这个代码。谁能告诉我如何在这里为我的代码执行此操作..??
这是data.php
<?php
$con = mysql_connect("localhost","shsas","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("shsas", $con);
$sth = mysql_query("SELECT Gaslevel FROM gas");
$rows = array();
$rows['name'] = 'Gaslevel';
while($r = mysql_fetch_array($sth)) {
$rows['data'][] = $r['Gaslevel'];
}
$result = array();
array_push($result,$rows);
print json_encode($result, JSON_NUMERIC_CHECK);
mysql_close($con);
?>
这是gaschart.php
<script type="text/javascript">
$(function () {
var chart;
$(document).ready(function() {
$.getJSON("data.php", function(json) {
chart = new Highcharts.Chart({
chart: {
renderTo: 'container',
type: 'line',
marginRight: 130,
marginBottom: 25,
},
title: {
text: 'Gas Level Detection',
x: -20 //center
},
subtitle: {
text: '',
x: -20
},
xAxis: {
title: {
text: 'EventID'
},
},
yAxis: {
title: {
text: 'Level of gas'
},
},
tooltip: {
formatter: function() {
return '<b>'+ this.series.name +'</b><br/>'+
this.x +': '+ this.y;
}
},
legend: {
layout: 'vertical',
align: 'right',
verticalAlign: 'top',
x: -10,
y: 100,
borderWidth: 0
},
series: json
});
});
});
});
</script>
最佳答案
我强烈建议您尝试使用套接字。
您可以查看socket.io 。虽然它是为了聊天而开发的。我确信,您可以将它用于您的用例。
另外,请检查elephant.io 。它更通用。一个轻量且易于使用的库,旨在带来一些实时功能。您也可以在您的项目中尝试此操作。在内部,它使用socket.io
关于php - 从sql数据库实时更新 Highcharts ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38910024/