首先,我正在尝试从使用json编码回显数据点的data.php文件中解析数据。每当请求data.php文件时,数据点都会更新,但不会在一系列数据点中更新。相反,它只是更改时间值并刷新其y内容。我还没有找到一种有效的方法来实际使php文件回显一系列数据点,而不更新单个数据点。
接下来,图表分析了data.php文件,它确实显示了数据点。但是,我想使此图表每秒更新一次,并在每次更新时添加新的数据点,以便获得有效的带宽图。
这是我的代码:
index.php:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="canvasjs.min.js"></script>
<script type="text/javascript" src="js/jquery-2.1.4.min.js"></script>
<script>
$(document).ready(function () {
$.getJSON("data.php", function (data_points) {
var dps = data_points
var chart = new CanvasJS.Chart("chartContainer", {
zoomEnabled: true,
panEnabled: true,
animateEnabled: true,
data: [ {
type: "splineArea",
xValueType: "label",
y: "y",
dataPoints: dps } ]
});
chart.render();
});
});
</script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width: 100%;">
</div>
</body>
</html>
data.php:
<?
session_start();
session_destroy();
session_start();
$rx[] = @file_get_contents("/sys/class/net/wlan0/statistics/rx_bytes");
sleep(1);
$rx[] = @file_get_contents("/sys/class/net/wlan0/statistics/rx_bytes");
$rbps = $rx[1] - $rx[0];
$round_rx=round($rbps/1, 2);
$time=date("Y-m-d H:i:s");
$_SESSION['rx'] = $round_rx;
$data_points['label'] = $time;
$data_points['y'] = $_SESSION['rx'];
echo json_encode([$data_points]);
?>
如果有人知道如何使此地图动态化,请给我一些帮助。data.php文件的输出示例(回显内容)如下:
[{"label":"2015-09-12 21:34:12","y":1500}]
预先感谢您提供的任何帮助。
为了以这种方式更新图表,您只需要创建一次图表(在ajax请求之外),并保持每秒通过ajax请求添加新的dataPoints,如下所示。
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" src="canvasjs.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function () {
var chart = new CanvasJS.Chart("chartContainer", {
zoomEnabled: true,
panEnabled: true,
animateEnabled: true,
data: [ {
type: "splineArea",
xValueType: "label",
y: "y",
dataPoints: []
} ]
});
function updateChart(){
$.getJSON("data.php", function (data_points) {
for(var i = 0; i < data_points.length; i++){
chart.options.data[0].dataPoints.push(data_points[i]);
}
chart.render();
});
}
var updateInterval = 1000;
setInterval(function(){
updateChart()
}, updateInterval);
});
</script>
</head>
<body>
<div id="chartContainer" style="height: 300px; width: 500px;"></div>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句