我试图从我的Google应用程序的数据存储中读取数据,用它填充谷歌图表数据表,然后使用来自google图表示例的源代码将整个过程可视化成一个图形,该源代码使用嵌入在网页上的Javascript代码。
我的问题是获取数据。我想到了两种方法:要么直接在javascript代码中运行查询,要么从python代码运行查询,将该查询的结果作为模板值发送到html代码,过滤它以获取我感兴趣的值,然后以某种方式将整个过程传递给javascript代码,然后显示数据(看起来更复杂)。我试过第一种选择,但似乎行不通。由于我不确定数据存储的URL是什么,所以我认为它与使用它的服务器相同,所以我将我的appengine应用程序的URL作为参数传递给查询函数。我试图对此运行一个SQL查询,但得到了一个错误。下面是对应的JS代码(单独)和整个HTML代码
function drawVisualization() {
var query = new google.visualization.Query('http://davidfirstapp.appspot.com');
query.setQuery('SELECT ac_current1, ac_voltage1 ORDER BY ac_current1 LIMIT 10');
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
visualization = new google.visualization.LineChart(document.getElementById('visualization'));
visualization.draw(data, {legend: 'bottom'});
}<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>
Google Visualization API Sample
</title>
<script type="text/javascript" src="//www.google.com/jsapi"></script>
<script type="text/javascript">
google.load('visualization', '1', {packages: ['linechart']});
</script>
<script type="text/javascript">
var visualization;
function drawVisualization() {
var query = new google.visualization.Query('http://davidfirstapp.appspot.com');
query.setQuery('SELECT ac_current1, ac_voltage1 ORDER BY ac_current1 LIMIT 10');
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
visualization = new google.visualization.LineChart(document.getElementById('visualization'));
visualization.draw(data, {legend: 'bottom'});
}
google.setOnLoadCallback(drawVisualization);
</script>
</head>
<body style="font-family: Arial;border: 0 none;">
<div id="visualization" style="height: 400px; width: 400px;"></div>
</body>
</html>
发布于 2013-10-29 20:18:14
您不能查询自己的网页,并希望它能够连接到数据存储。您的网页包含您自己定义的html输出。阅读有关appengine / web应用程序和数据存储如何工作的更多信息。您需要从前端执行数据存储查询,而不是浏览器js。构建表并将其传递给浏览器,在浏览器中生成数据表和图表。
https://stackoverflow.com/questions/19667601
复制相似问题