When referring to this example at , it is noted that the method data.addRows()
requires a list of lists.
A URI (/data/mydata.json) can be accessed to retrieve the following data:
[["Canada", 66], ["Turkey", 10], ["Hungary", 23], ["Italy", 49]]
Despite attempting to use JQuery's $.parseJSON(), an error is encountered:
SyntaxError: JSON.parse: unexpected character
The question then arises of how to properly format and pass this data into the required method.
UPDATE
Further investigation showed that running just
alert($.parseJSON('/data/mydata.json'))
successfully parsed and displayed the data. The issue seems to lie with data.addRows() throwing the error.
Below is the complete code snippet:
google.load('visualization', '1.0', {'packages':['corechart']});
google.setOnLoadCallback(drawChart)
function drawChart(){
var data = new google.visualization.DataTable();
data.addColumn('string', 'Country');
data.addColumn('number', 'Node Count');
data.addRows($.parseJSON('/data/mydata.json'));
var options = {'title':'Tor Nodes by Country',
'width':800,
'height':600};
var chart = new google.visualization.PieChart(document.getElementById('nodes'));
chart.draw(data, options);
}
}