This example show how to use Google Charts to display pie chart on Android WebView. Google chart tools are powerful, simple to use, and free. It not only display a static graphic, but also provide user touch interactive operation, check the video on the bottom.
Display Google Charts (pie chart) displayed Android WebView
When User click on the "Show Chart" button, it will start another activity, ShowWebChartActivity.java, and pass user data. ShowWebChartActivity load a WebView with our HTML to display Google Charts with Javascript. We have to implement WebAppInterface, with methods of @JavascriptInterface, getNum1()...getNum5(). It will be called by Javascript inside HTML to retrieve user data.
package com.example.androidwebchart;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
@SuppressLint("SetJavaScriptEnabled")
public class ShowWebChartActivity extends ActionBarActivity {
WebView webView;
int num1, num2, num3, num4, num5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_webchart);
Intent intent = getIntent();
num1 = intent.getIntExtra("NUM1", 0);
num2 = intent.getIntExtra("NUM2", 0);
num3 = intent.getIntExtra("NUM3", 0);
num4 = intent.getIntExtra("NUM4", 0);
num5 = intent.getIntExtra("NUM5", 0);
webView = (WebView)findViewById(R.id.web);
webView.addJavascriptInterface(new WebAppInterface(), "Android");
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/chart.html");
}
public class WebAppInterface {
@JavascriptInterface
public int getNum1() {
return num1;
}
@JavascriptInterface
public int getNum2() {
return num2;
}
@JavascriptInterface
public int getNum3() {
return num3;
}
@JavascriptInterface
public int getNum4() {
return num4;
}
@JavascriptInterface
public int getNum5() {
return num5;
}
}
}
/res/layout/layout_webchart.xml, layout of ShowWebChartActivity.
/assets/chart.html, our HTML to load and display Google Charts. Actually it is modified from Google Quick Start example of pie chart. The main difference is it retrieve user data by calling Android JavascriptInterface methods (getNum1()...getNum5()), instead of fixed data.
<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', 'ITEM');
data.addColumn('number', 'VALUE');
data.addRows([
['Item 1', Android.getNum1()],
['Item 2', Android.getNum2()],
['Item 3', Android.getNum3()],
['Item 4', Android.getNum4()],
['Item 5', Android.getNum5()]
]);
// Set chart options
var options = {'title':'Android-er: Load Google Charts (pie chart) with WebView',
'width':600,
'height':600};
// 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" style="width:600; height:600"></div>
</body>
</html>
Finally, modify AndroidManifest.xml to add <activity> of ShowWebChartActivity, and <uses-permission> of "android.permission.INTERNET".
Here's a simple example of a page that displays a pie chart:
You can copy the snippet below to an .html file on your computer and open it in your browser to display
the pie chart shown above:
<html><head><!--Load the AJAX API--><scripttype="text/javascript"src="https://www.gstatic.com/charts/loader.js"></script><scripttype="text/javascript">// Load the Visualization API and the corechart package.
google.charts.load('current',{'packages':['corechart']});// Set a callback to run when the Google Visualization API is loaded.
google.charts.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 optionsvar 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--><divid="chart_div"></div></body></html>
How About a Bar Chart?
Convert the pie chart to a bar chart by replacing
google.visualization.PieChart with
google.visualization.BarChart in the code and reloading your browser. You
may notice that the "Slices" legend is truncated. To fix this, change width to 500
from 400, save the file, and reload your browser.