我一直在尝试实时更新我的 Google Gauge Charts。
我的代码如下。
<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 进行硬编码,那么它可以正常工作,但是当我从 ajax 以相同的 json 格式返回 json 时,它不会绘制仪表