Because the request is asynchronous, you'll have to restructure how your program works:
function getData(callback) {
// code for IE7+, Firefox, Chrome, Opera, Safari
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else { // code for IE6, IE5
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
xhr.open("GET", "/submit222", true);
xhr.onreadystatechange = function(){
if (xhr.readyState == 4 && xhr.status == 200) {
callback(JSON.parse(xhr.responseText));
}
};
xhr.send();
}
function drawChart(myData) {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Number of Requests');
var rows = new Array(5);
for (var i = 0; i < rows.length; i++) {
row[i] = [ new Date(myData[i][0]), myData[i][1] ];
}
data.addRows(rows);
var chart = new google.visualization.AnnotationChart(document.getElementById('chart_div'));
var options = {
displayAnnotations: true,
};
chart.draw(data, options);
}
</script>
</head>
<body onload="getData(drawChart)">
<div id='chart_div' style='width: 900px; height: 500px;'></div>
</body>
</html>