package com.Android3dPieChart;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Android3dPieChartActivity extends Activity {
final static String urlGoogleChart
= "http://chart.apis.google.com/chart";
final static String urlp3Api
= "?cht=p3&chs=400x150&chl=A|B|C&chd=t:";
EditText inputA, inputB, inputC;
Button generate;
ImageView pieChart;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputA = (EditText)findViewById(R.id.adata);
inputB = (EditText)findViewById(R.id.bdata);
inputC = (EditText)findViewById(R.id.cdata);
generate = (Button)findViewById(R.id.generate);
pieChart = (ImageView)findViewById(R.id.pie);
generate.setOnClickListener(generateOnClickListener);
}
Button.OnClickListener generateOnClickListener
= new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String A = inputA.getText().toString();
String B = inputB.getText().toString();
String C = inputC.getText().toString();
String urlRqs3DPie = urlGoogleChart
+ urlp3Api
+ A + "," + B + "," + C;
Bitmap bm3DPie = loadChart(urlRqs3DPie);
if(bm3DPie == null){
Toast.makeText(Android3dPieChartActivity.this,
"Problem in loading 3D Pie Chart",
Toast.LENGTH_LONG).show();
}else{
pieChart.setImageBitmap(bm3DPie);
}
}};
private Bitmap loadChart(String urlRqs){
Bitmap bm = null;
InputStream inputStream = null;
try {
inputStream = OpenHttpConnection(urlRqs);
bm = BitmapFactory.decodeStream(inputStream);
inputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bm;
}
private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream is = null;
URL url = new URL(strURL);
URLConnection urlConnection = url.openConnection();
try{
HttpURLConnection httpConn = (HttpURLConnection)urlConnection;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
is = httpConn.getInputStream();
}
}catch (Exception ex){
}
return is;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A "
/>
<EditText
android:id="@+id/adata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B "
/>
<EditText
android:id="@+id/bdata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C "
/>
<EditText
android:id="@+id/cdata"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</LinearLayout>
<Button
android:id="@+id/generate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Generate 3D Pie Chart"
/>
<ImageView
android:id="@+id/pie"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
Permission: "android.permission.INTERNET" is needed.
Related Post:
- Generate QR Code using Google Chart Tools APIs
hi, thanks for your blog, im a daily follower.
ReplyDeleteDear zuka,
ReplyDeleteThx for your support:)
hi,
ReplyDeletei want to name other than those a, b ,c,
how is it possible and thx for your blog
jus replace the line......"final static String urlp3Api
Delete= "?cht=p3&chs=400x150&chl=A|B|C&chd=t:"; to ."final static String urlp3Api
= "?cht=p3&chs=400x150&chl=changed1|changed2|changed3&chd=t:
Thanks for the post, it was useful
ReplyDeletei want to change the size of text a,b,c,d..
ReplyDeleteTry this: http://chart.apis.google.com/chart?chxs=0,000000,50&chxt=x&cht=p3&chs=400x150&chl=A|B|C&chd=t:456,123,599.
DeleteYou can play with it in https://google-developers.appspot.com/chart/image/docs/chart_wizard, select Editor.
Please note that its deprecated api.
HI,
ReplyDeleteI am not able to execute it, getting nulpointer exception when we try to load url. Can you please help me on that.
Don't know why emulator gives nullpointer exception, but in phone it working well.
DeleteHi, can anyone tell me how i change the backgroung white color to transparent or any other color.
ReplyDeletehi, It works fine up to four parameters that is A|BC|D. If I add 5th parameter, App goes crash.....
ReplyDeleteAny solution dear, and can we modify the colors of charts...??
hi.....it can be explode?......@@....help me
ReplyDeletecan i make the slices clickable?
ReplyDelete