尝试生成一个网页,该网页使用 API 来获取一些数据(在这种情况下,一些数据来自跟踪 twitter 上的热门话题标签的网站)并在 Google 图表中显示这些数据。
现在,我已经使用这个 API 调用成功地获取了条形图和饼图的数据,但无法让表格正常工作。
HTML 代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Top Tweets from Grand NCE</title>
<meta http-equiv="Content-Type" content="text/xhtml+xml; charset=UTF-8" />
<!-- Code to implement Google charts -->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["table"]});
</script>
<!-- jQuery AJAX code & DrawChart code -->
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
<script src="PageThree.js"></script>
</head>
<body>
<div id="chart_div" style="width: 900px; height: 500px; margin:0 auto">Loading... </div>
</body>
</html>
JavaScript 代码:
//calls the API on page load
$(function() {
$.ajax({
type: "GET",
cache: false,
url: "http://academiamap.com/content.php?call=getTrendingTopics&startTime=2013-9-05%2000:00:00&endTime=2013-10-11%2000:00:00&seedUserList=GRAND_NCE",
success: function(data) {
data = eval(data);
chartData = new Array();
chartData[0] = ['Hashtag', 'Tweets'];
for (i=1; i<=data.length; i++) {
chartData[i] = new Array();
chartData[i] = [data[i-1]['tag'], parseInt(data[i-1]['count'])];
}
console.log(chartData);
drawChart1(chartData);
}
});
});
function drawChart1(dataArray) {
var data = google.visualization.arrayToDataTable(dataArray);
var table = new google.visualization.Table(document.getElementById('table_div')); //draw chart to "table_div"
table.draw(data, options);
}
我是使用 Web API 和 javascript 的新手,因此不胜感激。
谢谢!