我一直在尝试实时更新我的谷歌仪表图表。
我的代码如下。
<script type='text/javascript'>
google.load('visualization', '1', {packages:['gauge']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var json = $.ajax({
url: 'graph.php', // make this url point to the data file
dataType: 'json',
async: false
}).responseText;
//alert(json);
var data = google.visualization.arrayToDataTable(json);
var options = {
width: 400, height: 120,
redFrom: 0, redTo: 3,
greenFrom:<?php echo $inactivecount['inactive_count']-3;?>, greenTo: <?php echo $inactivecount['inactive_count'];?>,
minorTicks: 0,
min:0,
max:<?php echo $inactivecount['inactive_count'];?>,
'majorTicks': ["",""],
'animation.duration':100
};
var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
//setInterval(drawChart(12,10),1000);
chart.draw(data, options);
setInterval(drawChart, 1000);
}
</script>Ajax文件如下所示。
$table = array();
$table=array(0=>array('Label','Value'),1=>array('Likes',$like));
// encode the table as JSON
$jsonTable = json_encode($table);
// set up header; first two prevent IE from caching queries
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Oct 2013 05:00:00 GMT');
header('Content-type: application/json');
// return the JSON data
echo $jsonTable;如果在数据中对json进行硬编码,那么它可以很好地工作,但是当我以相同的json格式从ajax返回json时,它不会显示仪表。
发布于 2013-10-01 22:37:11
首先,在绘图函数结束时调用setInterval(drawChart, 1000);并不是您想要做的事情--这会在每次调用时产生一个新的间隔(在现有间隔之上),因此间隔会呈指数级增长,每秒增加一倍(粗略地说,考虑到AJAX调用和代码的执行时间,这会稍微长一点)。这将很快锁定您的浏览器和/或用传入的请求淹没您的服务器。试着这样做:
function drawChart() {
var data;
var options = {
width: 400,
height: 120,
redFrom: 0,
redTo: 3,
greenFrom: <?php echo $inactivecount['inactive_count']-3;?>,
greenTo: <?php echo $inactivecount['inactive_count'];?>,
minorTicks: 0,
min: 0,
max: <?php echo $inactivecount['inactive_count'];?>,
majorTicks: ["",""],
animation: {
duration: 100
}
};
var chart = new google.visualization.Gauge(document.getElementById('chart_div'));
function refreshData () {
var json = $.ajax({
url: 'graph.php', // make this url point to the data file
dataType: 'json',
async: false
}).responseText;
data = google.visualization.arrayToDataTable(json);
chart.draw(data, options);
}
refreshData();
setInterval(refreshData, 1000);
}如果这不起作用,那么在浏览器中转到graph.php并发布它的输出,这样我就可以测试它了。
https://stackoverflow.com/questions/19112437
复制相似问题