2
有谁知道是否有可能向谷歌图表添加实时更新功能?我不想用按钮来更新数据;相反,我想让图形在数据库数据发生变化时自动更改。这是我用来显示折线图的代码,它的工作原理是正确的。谷歌图表实时更新ajax
的index.php
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<script type="text/javascript">
google.load('visualization', '1', {'packages':['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "getData.php",
dataType:"json",
async: false
}).responseText;
var data = new google.visualization.DataTable(jsonData);
var options = {
width: '100%',
height: 500,
legend: 'none',
colors: ['orange','red','green'],
vAxis: {textStyle: {color: 'white'}},
hAxis: {textStyle: {color: 'white'}}
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<div id="chart_div"></div>
</body>
访问getdata.php 我从我的数据库中的数据采用以下格式:
$db_conn_pyr = connect();
function getOrderrader($datum){
// gets order rows ...
}
$pastDate = date('Ymd', strtotime("-1 month"));
$sqlTrans = "SELECT DISTINCT D3611 FROM PUPROTRA WHERE D3625 = 'U' AND D3601 = 'O' AND
D3611 >= $pastDate ORDER BY D3611 ASC";
$resultTrans = odbc_exec($db_conn_pyr, $sqlTrans)or die(odbc_errormsg());
$data = array(
'cols' => array(
array('id' => '', 'label' => 'datum', 'type' => 'string'),
array('id' => '', 'label' => 'Target', 'type' => 'number'),
array('id' => '', 'label' => 'Target', 'type' => 'number'),
array('id' => '', 'label' => 'Orderrader', 'type' => 'number')
)
);
while($row = odbc_fetch_array($resultTrans)){
$datum = utf8_encode($row['D3611']);
$ordernr = getOrderrader($datum);
$data['rows'][] = array(
'c' => array(
array('v' => $datum),
array('v' => 240),
array('v' => 160),
array('v' => $ordernr)
)
);
}
$string = json_encode($data);
echo $string;
千恩万谢 琳达
优秀 - 工程很棒 - 非常感谢 – user626342 2013-02-14 13:07:56