Dhanesh
Dhanesh

Reputation: 781

Google chart api is not working properly

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>

Upvotes: 1

Views: 4753

Answers (4)

zeroquaranta
zeroquaranta

Reputation: 412

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

Upvotes: 1

VinC
VinC

Reputation: 457

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!

Upvotes: 2

Vinayak Bevinakatti
Vinayak Bevinakatti

Reputation: 40513

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

Upvotes: 0

TryTryAgain
TryTryAgain

Reputation: 7830

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

Upvotes: 0

Related Questions