Loading google datatable using ajax/json

Posted by puff on Stack Overflow See other posts from Stack Overflow or by puff
Published on 2010-05-20T21:01:26Z Indexed on 2010/05/20 21:10 UTC
Read the original article Hit count: 410

Filed under:
|
|
|

I can't figure out how to load a datable using ajax/json. Here is my json code in a remote file (pie.json)

{ 
   cols: [{id: 'task',  label: 'Task',          type: 'string'}, 
          {id: 'hours', label: 'Hours per Day', type: 'number'}], 
   rows: [{c:[{v: 'Work'},     {v: 11}]}, 
          {c:[{v: 'Eat'},      {v: 2}]}, 
          {c:[{v: 'Commute'},  {v: 2}]}, 
          {c:[{v: 'Watch TV'}, {v:2}]}, 
          {c:[{v: 'Sleep'},    {v:7, f:'7.000'}]} 
         ] 
  } 

This is what I have tried so far but it doesn't work.

<script type="text/javascript" src="http://www.google.com/jsapi"></script> 

<script type="text/javascript"> 
google.load("visualization", "1", {packages:["piechart"]}); 

function ajaxjson() { 
   jsonreq=GetXmlHttpObject(); 
   jsonreq.open("GET", "pie.json", true); 
   jsonreq.onreadystatechange = jsonHandler; 
   jsonreq.send(null); 
  } 


function jsonHandler() { 
 if (jsonreq.readyState == 4) 
   { 
   var res = jsonreq.responseText; 
   google.setOnLoadCallback(drawChart); 
   function drawChart() { 
   var data = new google.visualization.DataTable(res, 0.6) 
   var chart = new google.visualization.PieChart(document.getElementByI('chart_div')); 
   chart.draw(data, {width: 400, height: 240, is3D: true}); 
   }     // end drawChart 
   } // end if 
} // end jsonHandler 


function GetXmlHttpObject() 
  { 
   var xmlHttp=null; 
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); 
   return xmlHttp; 
  } 

Things work perfectly if I replace the 'res' variable with the actual code in pie.json.

Any help would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about AJAX

Related posts about JSON