【发布时间】:2012-03-20 17:35:01
【问题描述】:
我正在一些网页中显示 Google 的图表。但我不能保证我的客户可以通过网络访问 Google:客户端计算机将与我的网络服务器(可以访问 Google)在同一个 LAN 中,但我不能保证所有客户端都可以访问 LAN 外.
我想向可以访问数据的客户展示使用 Google 图表的数据,向不能访问的客户展示纯 HTML 表格。
我尝试将变量设置为 false,并在加载 Google Visualization API 时调用的方法中将其更改为 true:
var canAccessGoogleVisualizationVar = false;
google.load('visualization', '1', {packages: ['corechart'], callback: canAccessGoogleVisualization});
function canAccessGoogleVisualization()
{
canAccessGoogleVisualizationVar = true;
}
但它似乎不起作用。
我如何从客户端知道 Google 可视化是否可以访问?
更新:上面的代码不起作用,因为下面的代码(我之前没有发布,因为我认为没有意义):
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 == true 不采用 if 分支,如果它的 false 不会执行 function drawVisualization()。
所以我在函数外进行了 if-test:
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()。
在尝试执行对google.load(...); 的调用之后,我如何确定我正在读取canAccessGoogleVisualizationVar 的值?
【问题讨论】:
-
用更多信息更新了问题。
-
对于它的价值,可以使用 gviz js 和 css,然后从这个内部应用程序的任何位置提供它。许多图表都是在客户端完成的,因此可以以这种方式正常工作(我已经完成了)。只需查看正常加载 api 时发出的网络调用,即可看到所需的文件。值得注意的是a)代码将被缩小,b)如果不手动更改代码,您将无法获得任何更新,但它可能是一个选项:)
-
谢谢@peter,我正在试一试。如果我不手动更改代码就不会更新代码这一事实是一个加号(在这种情况下)。
-
对不起...我在上一条评论中提到了@oli,而不是 (@)peter。
-
请注意,在本地提供 Google 的 JS 违反了 TOS。
标签: javascript google-visualization