0

我正在嘗試從Google App Engine應用程序的數據存儲中讀取數據,填充Google圖表Datatable,然後使用源代碼將整個東西可視化爲圖形來自使用嵌入在網頁上的Javascript代碼的google charts example將數據從數據存儲區讀入Google Charts Datatable來繪製圖表

我的問題是提取數據。我想到了這樣做的兩種方法:或者直接在javascript代碼中運行查詢,或者從python代碼運行查詢,將該查詢的結果作爲模板值發送給html代碼,過濾它以獲取值I' m感興趣,並以某種方式將整個事情傳遞給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> 

回答

1

你不能查詢自己的網頁,並以某種方式期望它連接到數據存儲。您的網頁包含您自定義的html輸出。閱讀更多關於appengine/web應用程序以及數據存儲如何工作的信息。 您需要從前端執行數據存儲查詢,而不是瀏覽器js。構建一個表並將其傳遞給構建數據表和圖表的瀏覽器。

+0

那麼你是否建議我使用第二個選項?這意味着我從我的python代碼運行查詢,然後將結果(又名錶)傳遞給html代碼?我覺得這一步有點複雜。你能通過它走過我嗎? – df611

+0

是的,多數民衆贊成在暗示。它真的是唯一的辦法比一個建議:) –

相關問題