2012-03-20 51 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

相关问题