Ajax / JSON Redrawing google chart -
im using google visualization chart api here: https://developers.google.com/chart/interactive make server uptime graph seems working nicely.
however want users able select date range , redraw graph without having refresh browser. , have small problem this.
i first draw graph initial data, , if user changes date range graph should redrawn. tried redrawing sample data , works fine. cant seem work updated data.
now in php file fetch data db return both average uptime period total uptime period such:
/*mysql query striped*/ $uptime_result = mysql_query($query, $connection) or die(mysql_error()); $uptime_data = "['day', 'uptime'],"; while ($items = mysql_fetch_array($uptime_result)){ $uptime_data.="['{$items['date']}',{$items['uptime']}], "; } // average uptime /*mysql query striped*/ $uptime_result = mysql_query($query, $connection) or die(mysql_error()); $result_array = mysql_fetch_array($uptime_result); $avg_uptime = round($result_array['avg(uptime)'],2); echo "{\"data\":\"{$uptime_data}\",\"average\":$avg_uptime}"; which outputs like:
{"data":"['day', 'uptime'],['2012-05-31',100.00], ['2012-06-01',100.00], ['2012-05- 22',99.65], ['2012-05-21',99.65], ['2012-05-20',100.00], ['2012-05-31',100.00], ['2012-05-30',100.00], ['2012-05-29',100.00], ['2012-05-28',100.00], ['2012-05-27',100.00], ['2012-05-26',100.00], ['2012-05-25',100.00], ['2012-05-24',100.00], ['2012-05-23',100.00], ['2012-05-19',100.00], ['2012-05-18',100.00], ['2012-05-17',100.00], ['2012-05-16',100.00], ['2012-05-15',100.00], ['2012-05-14',100.00], ['2012-05-13',100.00], ['2012-05-12',100.00], ['2012-05-11',100.00], ['2012-05-10',100.00], ['2012-05-09',100.00], ['2012-05-08',100.00], ['2012-05-07',100.00], ['2012-06-02',100.00], ['2012-06-03',100.00], ['2012-06-04',100.00], ","average":99.98} i.e json array 2 variables data , average. able fetch 2 independently such:
$(function(){ $('#from,#to').focusout(function(){ var start=$('#from').val(); var end=$('#to').val(); $.ajax({ type: 'post', data: ({from : start, : end, id : <?php echo $id; ?>}), url: 'fetchuptime.php', success: function(data) { //7 reulst goes here //var json = data; var obj = jquery.parsejson(data); $('#uptime_span').html(obj.average +" %"); $('#test').html(data); chart_data = google.visualization.arraytodatatable([ obj.data ]); var ac = new google.visualization.areachart(document.getelementbyid('visualization')); ac.draw(chart_data, { colors : ['#00db00'], title : '', isstacked: false, width: 570, height: 400, 'chartarea': {'width': '88%', 'height': '90%'}, haxis: {minvalue: 0,showtextevery: 6, slantedtext: false}, vaxis: { viewwindowmode:'explicit', viewwindow:{ max:100, min:90.65 } }, pointsize: 3, legend: {position: 'none'} }); } }); }); }); eg. obj.average , obj.data gives me 2 string. not seem work, guess data doesn't passed along correctly.
i have tested actual output data (eg obj.data) formatted correct i've tried inserting statically.
so i'm doing wrong here, , assume it's because i'm passing string while google chart needs array, tried fix in various ways haven't found working yet.
can me one?
the format of json valid, not you're wanting:
{"data":"['day', 'uptime'],['2012-05-31',100.00] ... ['2012-06-04',100.00], ", "average":99.98} that represents object field named data value string (like said). want make array on server side. instead of double quotes, use square brackets. there trailing comma must removed.
{"data":[['day', 'uptime'],['2012-05-31',100.00] ... ['2012-06-04',100.00]], "average":99.98}
Comments
Post a Comment