Reputation: 147
Does anyone know if it is possible to add a live update function to Google charts? I do not want to use a button to update the data; instead I would like graph to automatically change when the database data has changed. This is the code that I am using to show the line chart and it works correct.
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 I get the data from my database in the following format:
$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;
many thanks Linda
Upvotes: 2
Views: 2836
Reputation: 3288
use a javascript timer to recall your graph creation function after xxx seconds and it should work for you.
Upvotes: 1