Google图表API无法正常运行

时间:2012-01-06 12:15:58

标签: javascript android webview google-visualization

我正在尝试在webView中加载谷歌图表API的html代码。它显示图表的数据,但不显示图形图像。我还允许清单中的互联网许可。是否有我在清单中缺少的任何内容或我必须在webview中进行的某些更改。请帮忙。

<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>

4 个答案:

答案 0 :(得分:2)

这个问题已经很老了,但我在WebView中显示一些图表和图表时遇到了困难,我偶然发现了这个问题。就像来自dev api的TryTryAgain一样,WebView默认情况下不启用JavaScript,但他没有提到你可以启用它,这样用户就可以通过简单的调用与 WebView中的内容进行交互to setJavaScriptEnabled();

webview.getSettings().setJavaScriptEnabled(true);

供参考:http://developer.android.com/reference/android/webkit/WebSettings.html

这对我有所帮助,我希望它会帮助别人!

答案 1 :(得分:1)

我并不真正从事移动应用程序开发,但是在使用wkhtmltopdf(使用WebKit)时遇到了类似的问题。

经过一段时间的努力,结果发现由于某种原因setOnLoadCallback函数被忽略,因此drawChart从未被调用。

我没有找到任何合适的解决方案,必须使用setTimeOut来调用绘图函数,这是一个非常糟糕的选择。...

答案 2 :(得分:0)

您是否知道默认情况下未启用Javascript?

http://developer.android.com/reference/android/webkit/WebView.html

基本使用

  

默认情况下,WebView不提供类似浏览器的小部件   启用JavaScript和网页错误将被忽略。如果你的目标是   只显示一些HTML作为UI的一部分,这可能很好;   除了阅读之外,用户不需要与网页进行交互,   并且网页不需要与用户交互。如果你真的   想要一个成熟的网络浏览器,那么你可能想要调用   具有URL Intent的浏览器应用程序而不是使用   的WebView。例如:

 Uri uri = Uri.parse("http://www.example.com");
 Intent intent = new Intent(Intent.ACTION_VIEW, uri);
 startActivity(intent);

看起来这可能是你的问题。

当然可以肯定:

<uses-permission android:name="android.permission.INTERNET" />

在您的manifest.xml中

答案 3 :(得分:0)

如果您在Android操作系统上测试的谷歌图表少于Honeycomb,那么您的测试将失败,因为Google图表是使用SVG呈现的,您会在此处发现问题http://code.google.com/p/android/issues/detail?id=1376

  

因此,Android 2.x默认浏览器本身不支持SVG   无法呈现图表

     

Android 3+默认浏览器支持SVG。   所以可以渲染图表