Google 图表 API 无法正常工作

发布于 2024-12-25 03:55:11 字数 1622 浏览 5 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

染年凉城似染瑾 2025-01-01 03:55:11

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

webview.getSettings().setJavaScriptEnabled(true);

供参考: 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();

webview.getSettings().setJavaScriptEnabled(true);

For reference: http://developer.android.com/reference/android/webkit/WebSettings.html

This helped me I hope it will help someone else!

╰沐子 2025-01-01 03:55:11

我不太喜欢移动应用程序开发,但在使用 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 the drawChart 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....

旧竹 2025-01-01 03:55:11

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

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

基本用法

默认情况下,WebView 不提供类似浏览器的小部件,也不提供
启用 JavaScript 并忽略网页错误。如果你的目标是
仅将一些 HTML 显示为 UI 的一部分,这可能没问题;
用户除了阅读之外不需要与网页进行交互,
并且网页不需要与用户交互。如果你实际上
想要一个功能齐全的网络浏览器,那么您可能想要调用
带有 URL Intent 的浏览器应用程序,而不是用
网页视图。例如:

 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中

Are you aware Javascript is not enabled by default?

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

BASIC USAGE

By default, a WebView provides no browser-like widgets, does not
enable JavaScript and web page errors are ignored. If your goal is
only to display some HTML as a part of your UI, this is probably fine;
the user won't need to interact with the web page beyond reading it,
and the web page won't need to interact with the user. If you actually
want a full-blown web browser, then you probably want to invoke the
Browser application with a URL Intent rather than show it with a
WebView. For example:

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

Looks like that may be your problem.

and of course be sure:

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

is in your manifest.xml

累赘 2025-01-01 03:55:11

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

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

Android 3+ 默认浏览器支持 SVG。
这样就可以渲染图表了

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.

The Android 2.x default browser does not natively support SVG, hence
cannot render charts

The Android 3+ default browsers supports SVG.
So can render charts

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文