Google 图表 API 无法正常工作
我正在尝试在 webView 中加载 google 图表 api 的 html 代码。它显示图表数据但不显示图形图像。我还在清单中允许了互联网许可。清单中是否缺少任何内容,或者我必须在网络视图中进行某种更改。请帮忙。
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':400,
'height':300};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
I am trying to load this html code for google chart api in webView. It shows the data of chart but doesn't show graphical image. I also allowed internet permission in manifest. Is there anything I am missing in manifest or some kind of changes I have to make in webview. please help.
<html>
<head>
<!--Load the AJAX API-->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addRows([
['Mushrooms', 3],
['Onions', 1],
['Olives', 1],
['Zucchini', 1],
['Pepperoni', 2]
]);
// Set chart options
var options = {'title':'How Much Pizza I Ate Last Night',
'width':400,
'height':300};
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
</head>
<body>
<!--Div that will hold the pie chart-->
<div id="chart_div"></div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个问题已经很老了,但我在 WebView 中显示一些图表和图形时遇到了困难,我偶然发现了这个问题。就像开发 API 中引用的 TryTryAgain 一样,WebView 默认情况下不启用 JavaScript,但他没有提到您可以启用它,以便用户可以通过使用简单的调用与 WebView 中的内容进行交互设置 JavaScriptEnabled();
供参考: http://developer.android.com/reference/android/webkit/ WebSettings.html
这对我有帮助,我希望它对其他人有帮助!
This question is pretty old, but I was struggling with displaying some charts and graphs in a WebView and I stumbled across this. Like TryTryAgain quoted from the dev api WebView doesn't enable JavaScript by default, but he didn't mention that you can enable it so that users can interact with the content in the WebView by using a simple call to setJavaScriptEnabled();
For reference: http://developer.android.com/reference/android/webkit/WebSettings.html
This helped me I hope it will help someone else!
我不太喜欢移动应用程序开发,但在使用 wkhtmltopdf (它使用 WebKit)时遇到了类似的问题。
经过一段时间的努力,发现由于某种原因,setOnLoadCallback 函数被忽略,因此,drawChart 从未被调用。
我没有找到任何合适的解决方案,并且必须使用
setTimeOut
来调用绘图函数,这是一个非常糟糕的选择......I'm not really into mobile app development, but I ran into a similar problem while using wkhtmltopdf (which uses the WebKit).
After struggling for a while, it came out that for some reason the
setOnLoadCallback
function is ignored, thus thedrawChart
is never invoked.I didn't find any proper solution, and has to use a
setTimeOut
to call the drawing function, which is a pretty bad option....您是否知道默认情况下未启用 Javascript?
http://developer.android.com/reference/android/webkit/WebView.html
基本用法
看起来这可能是你的问题。
当然,请确保:
在您的manifest.xml中
Are you aware Javascript is not enabled by default?
http://developer.android.com/reference/android/webkit/WebView.html
BASIC USAGE
Looks like that may be your problem.
and of course be sure:
is in your manifest.xml
如果您在低于 Honeycomb 的 Android 操作系统上测试 Google 图表,您的测试将会失败,因为 Google 图表是使用 SVG 渲染的,您会发现此处报告的问题 http://code.google.com/p/android/issues/detail?id=1376。
If you are testing Google charts on Android OS less than Honeycomb your test will fail because Google charts are rendered using SVG and you will find a issue reported here http://code.google.com/p/android/issues/detail?id=1376.