数据格式对Google可视化无效

时间:2014-04-09 06:26:48

标签: python json google-visualization

我试图将一些数据可视化为一个简单的条形图。我在python中获取该数据并将其发送到客户端。这是它的python代码 -

query_string = 'SELECT state, count(*) FROM [{0}] GROUP by state;'.format(_DATABASE_NAME)
births = self.run_query(query_string, filename='data/states.json')
outcome = []
for row in births:
   age = row['mother_age']
   times = row['f0_']
   ip = {'age': int(age), 'times': int(times)}
   outcome = outcome + [ip]
outcome.insert(0, ('Age', 'No of occurunces'))
logging.info(json.encode(outcome))

这就是我在日志文件中看到的内容 -

[["Age","No of occurunces"],{"age":31,"times":6170247},{"age":30,"times":6876756},{"age":26,"times":8271245},..........]

这是index.html文件 -

html>
<head>
  <script type='text/javascript' src='https://www.google.com/jsapi'></script>
  <script type='text/javascript'>
   google.load('visualization', '1', {'packages': ['geochart']});
   google.setOnLoadCallback(drawMap);

    function drawMap() {
  // Create and populate the data table.
  var data = google.visualization.arrayToDataTable({{times|safe}});

  // Create and draw the visualization.
  new google.visualization.BarChart(document.getElementById('visualization')).
      draw(data,
           {title:"Yearly Coffee Consumption by Country",
            width:600, height:400,
            vAxis: {title: "Year"},
            hAxis: {title: "Cups"}}
      );
}

  </script>
</head>

<body>
  <div id='map_canvas'></div>

</body>

</html>

它给我一个错误说 - 未捕获错误:第0行的行类型无效 LDA TP 第10行的drawMap(即此行 - var data = google.visualization.arrayToDataTable({{times | safe}});)

我在谷歌可视化游乐场试了这个,它运行得很好。这是代码 -

function drawVisualization() {
  // Create and populate the data table.
  var data = google.visualization.arrayToDataTable([
    ['Year', 'Austria'],
    ['2003',  1336060],
    ['2004',  1538156],
    ['2005',  1576579],
    ['2006',  1600652],
    ['2007',  1968113],
    ['2008',  1901067]
  ]);

  // Create and draw the visualization.
  new google.visualization.BarChart(document.getElementById('visualization')).
      draw(data,
           {title:"Yearly Coffee Consumption by Country",
            width:600, height:400,
            vAxis: {title: "Year"},
            hAxis: {title: "Cups"}}
      );
}
​

我的代码有什么问题?

0 个答案:

没有答案