我正在尝试使用google api创建美国的强度地图。我从公共数据集中获取python中的数据,然后将其传递给UI端。这是它的代码
query_string = 'SELECT state, count(*) FROM [{0}] GROUP by state;'.format(_DATABASE_NAME)
births = self.run_query(query_string, filename='data/states.json')
rows = births[u'rows']
states = []
for row in rows:
name = row[u'f'][0][u'v']
num = row[u'f'][1][u'v']
if name == None: name = u'None'
state = {'state':unicode.encode(name), 'total':int(num)}
states = states + [state]
states.insert(0, ('State','No of users'))
logging.info(json.encode(states))
context = {"states": json.encode(states)}
self.render_response('index.html', context)
这就是json.encode(状态)的样子 -
[["State","No of users"],{"state":"AL","total":1957392},{"state":"AK","total":324183},{"state":"AZ","total":1837630},{"state":"AR","total":1062588},{"state":"CA","total":13794114},...........
这是index.html文件 -
<html>
<head>
<script type='text/javascript' src='https://www.google.com/jsapi'></script>
<script type='text/javascript'>
google.load('visualization', '1', {'packages': ['geomap']});
google.setOnLoadCallback(drawMap);
function drawMap() {
console.log("In draw map function");
var test = {{states|safe}};
console.log(test); console.log(test);
var data = google.visualization.arrayToDataTable({{states|safe}});
var options = {};
options['region'] = 'US'
options['dataMode'] = 'regions';
var container = document.getElementById('map_canvas');
var geomap = new google.visualization.GeoMap(container);
geomap.draw(data, options);
};
</script>
</head>
<body>
<div id='map_canvas'></div>
</body>
</html>
为什么我会收到这种错误?我在浏览器控制台中看到以下内容 - 未捕获错误:第0行的行类型无效 LDA TP drawMap(索引:12)
答案 0 :(得分:0)
一般来说,google.visualization.arrayToDataTable
需要一个数组数组(python中的列表列表)。您的列表中有一堆dict
,我认为不应该在那里。
你可能应该:
state = [unicode.encode(name), int(num)]
states.append(state) # this is better than states = states + [state]