2012-03-20 53 views
8

我在某些網頁上顯示Google的圖表。但我不能保證我的客戶可以訪問Google網絡:客戶端計算機與我的網絡服務器(可以訪問Google)位於同一LAN中,但我不能保證所有客戶端都可以訪問LAN外部。如何知道Google可視化是否已加載

我想使用谷歌圖表向那些可以訪問它的客戶端顯示數據,並且向那些不能訪問的客戶端顯示純HTML表格。

我試過一個變量設置爲false,並在加載谷歌可視化API時調用的方法將其更改爲真:

var canAccessGoogleVisualizationVar = false; 
google.load('visualization', '1', {packages: ['corechart'], callback: canAccessGoogleVisualization}); 
function canAccessGoogleVisualization() 
{ 
    canAccessGoogleVisualizationVar = true; 
} 

但它似乎並沒有工作。

如何從客戶端知道Google Visualization是否可訪問?


更新:上面的代碼並沒有因爲下面的代碼工作(我沒有,因爲我覺得之前的職位是沒有意義):

google.setOnLoadCallback(drawVisualization); 

function drawVisualization() 
{ 
    // Check if Google Visualization is loaded 
    if (!canAccessGoogleVisualizationVar) { 
     alert('Can't access Google Visualization'); 
    } 

    // The following code can be any of the samples from Google (see http://code.google.com/apis/ajax/playground/?type=visualization#pie_chart). 
    var data = new google.visualization.DataTable(); 
    // Add columns and values to data 
    ... 
    // Call new google.visualization.AnyChartBuilderFromTheAPI(<element>).draw(data); 
} 

我發現我的代碼沒不工作,因爲如果canAccessGoogleVisualizationVar == trueif分支未被採用,並且它的false,function drawVisualization()不會被執行。

於是我拿着如果測試之外的功能:

google.setOnLoadCallback(drawVisualization); 

function drawVisualization() 
{ 
    // Any drawVisualization unchanged from the samples from Google (see http://code.google.com/apis/ajax/playground/?type=visualization#pie_chart). 
} 

// Check if Google Visualization is loaded at the end of this <script> </script> 
if (!canAccessGoogleVisualizationVar) { 
    alert('Can't access Google Visualization'); 
} 
</script> 

但現在因爲評估if (!canAccessGoogleVisualizationVar)正在執行以前google.load(?, ?, canAccessGoogleVisualization);調用該方法canAccessGoogleVisualization()行它不工作。

如何確定我正在讀取canAccessGoogleVisualizationVar的值試圖執行對google.load(...);的呼叫?

+0

更新的問題更多的信息。 – 2012-03-21 11:54:50

+0

對於它的價值,可以採用gviz js和css,並且可以從該內部應用程序的任何位置提供服務。許多圖表完成客戶端,因此將以這種方式正常工作(我已經完成了)。正常情況下加載api時,只需查看網絡呼叫即可,您可以看到所需的文件。值得注意的是a)代碼會被縮小,b)如果不手動更改代碼,您將無法獲得代碼的任何更新,但它可能是一個選項:) – oli 2012-03-22 05:44:28

+0

謝謝@peter,我正在嘗試。如果我不手動更改代碼將不會被更新的事實是一個加號(在這種情況下)。 – 2012-03-22 09:22:10

回答

8

您可以嘗試

function canAccessGoogleVisualization() 
{ 
    if ((typeof google === 'undefined') || (typeof google.visualization === 'undefined')) { 
     return false; 
    } 
    else{ 
    return true; 
    } 
} 
+1

它應該是被接受的答案。 – C0ZEN 2017-05-15 22:45:14

相關問題