2013-02-14 93 views
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; 

千恩萬謝 琳達

回答

1

使用JavaScript定時器在xxx秒後回想一下你的圖形創建功能,它應該適合你。

+0

優秀 - 工程很棒 - 非常感謝 – user626342 2013-02-14 13:07:56