package com.myandroid.peichart;
2.Main.java file
package com.myandroid.peichart;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class Main extends Activity {
List<PieDetailsItem> piedata = new ArrayList<PieDetailsItem>(0);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PieDetailsItem item;
int maxCount = 0;
int itemCount = 0;
int items[] = { 20, 40, 10, 15, 5 };
int colors[] = { -6777216, -16776961, -16711681, -12303292, -7829368 };
String itemslabel[] = { " vauesr ur 100", " vauesr ur 200",
" vauesr ur 300", " vauesr ur 400", " vauesr ur 500" };
for (int i = 0; i < items.length; i++) {
itemCount = items[i];
item = new PieDetailsItem();
item.count = itemCount;
item.label = itemslabel[i];
item.color = colors[i];
piedata.add(item);
maxCount = maxCount + itemCount;
}
int size = 155;
int BgColor = 0xffa11b1;
Bitmap mBaggroundImage = Bitmap.createBitmap(size, size,
Bitmap.Config.ARGB_8888);
View_PieChart piechart = new View_PieChart(this);
piechart.setLayoutParams(new LayoutParams(size, size));
piechart.setGeometry(size, size, 2, 2, 2, 2, 2130837504 );
piechart.setSkinparams(BgColor);
piechart.setData(piedata, maxCount);
piechart.invalidate();
piechart.draw(new Canvas(mBaggroundImage));
piechart = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mImageView.setBackgroundColor(BgColor);
mImageView.setImageBitmap(mBaggroundImage);
LinearLayout finalLayout = (LinearLayout) findViewById(R.id.pie_container);
finalLayout.addView(mImageView);
}
}
========================================================================
3.PieDetailsItem.java file
package com.myandroid.peichart;
public class PieDetailsItem {
public int count, color;
public float percent;
public String label;
}
============================================================
4.View_PieChart .java file
package com.myandroid.peichart;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class View_PieChart extends View
{
public static final int WAIT = 0;
public static final int IS_READY_TO_DRAW = 1;
public static final int IS_DRAW = 2;
private static final float START_INC = 30;
private Paint mBagpaints = new Paint();
private Paint mLinePaints = new Paint();
private int mWidth;
private int mHeight;
private int mGapTop;
private int mGapBottm;
private int mBgcolor;
private int mGapleft;
private int mGapright;
private int mState = WAIT;
private float mStart;
private float mSweep;
private int mMaxConnection;
private List<PieDetailsItem> mdataArray;
public View_PieChart(Context context) {
super(context);
Log.w(" single cons ", " single cons");
}
public View_PieChart(Context context, AttributeSet attr) {
super(context, attr);
Log.w(" double cons ", " double cons");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mState != IS_READY_TO_DRAW) {
return;
}
canvas.drawColor(mBgcolor);
mBagpaints.setAntiAlias(true);
mBagpaints.setStyle(Paint.Style.FILL);
mBagpaints.setColor(0x88FF0000);
mBagpaints.setStrokeWidth(0.0f);
mLinePaints.setAntiAlias(true);
mLinePaints.setColor(0xff000000);
mLinePaints.setStrokeWidth(3.0f);
mLinePaints.setStyle(Paint.Style.STROKE);
RectF mOvals = new RectF(mGapleft, mGapTop, mWidth - mGapright, mHeight
- mGapBottm);
mStart = START_INC;
PieDetailsItem item;
for (int i = 0; i < mdataArray.size(); i++) {
item = (PieDetailsItem) mdataArray.get(i);
mBagpaints.setColor(item.color);
mSweep = (float) 360* ((float) item.count / (float) mMaxConnection);
canvas.drawArc(mOvals, mStart, mSweep, true, mBagpaints);
canvas.drawArc(mOvals, mStart, mSweep, true, mLinePaints);
mStart = mStart + mSweep;
}
mState = IS_DRAW;
}
public void setGeometry(int width, int height, int gapleft, int gapright,
int gaptop, int gapbottom, int overlayid) {
mWidth = width;
mHeight = height;
mGapleft = gapleft;
mGapright = gapright;
mGapBottm = gapbottom;
mGapTop = gaptop;
}
public void setSkinparams(int bgcolor) {
Log.w(" Set bg color : ", bgcolor + "");
mBgcolor = bgcolor;
}
public void setData(List<PieDetailsItem> data, int maxconnection) {
mdataArray = data;
mMaxConnection = maxconnection;
Log.w(" Max Connection ", maxconnection + " " + " Adataarray :"
+ data.toString());
mState = IS_READY_TO_DRAW;
}
public void setState(int state) {
mState = state;
}
public int getColorValues(int index) {
if (mdataArray == null) {
return 0;
}
else if (index < 0)
return ((PieDetailsItem) mdataArray.get(0)).color;
else if (index > mdataArray.size())
return ((PieDetailsItem) mdataArray.get(mdataArray.size() - 1)).color;
else
return ((PieDetailsItem) mdataArray.get(mdataArray.size() - 1)).color;
}
}
========================================================================
2.Main.java file
package com.myandroid.peichart;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class Main extends Activity {
List<PieDetailsItem> piedata = new ArrayList<PieDetailsItem>(0);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PieDetailsItem item;
int maxCount = 0;
int itemCount = 0;
int items[] = { 20, 40, 10, 15, 5 };
int colors[] = { -6777216, -16776961, -16711681, -12303292, -7829368 };
String itemslabel[] = { " vauesr ur 100", " vauesr ur 200",
" vauesr ur 300", " vauesr ur 400", " vauesr ur 500" };
for (int i = 0; i < items.length; i++) {
itemCount = items[i];
item = new PieDetailsItem();
item.count = itemCount;
item.label = itemslabel[i];
item.color = colors[i];
piedata.add(item);
maxCount = maxCount + itemCount;
}
int size = 155;
int BgColor = 0xffa11b1;
Bitmap mBaggroundImage = Bitmap.createBitmap(size, size,
Bitmap.Config.ARGB_8888);
View_PieChart piechart = new View_PieChart(this);
piechart.setLayoutParams(new LayoutParams(size, size));
piechart.setGeometry(size, size, 2, 2, 2, 2, 2130837504 );
piechart.setSkinparams(BgColor);
piechart.setData(piedata, maxCount);
piechart.invalidate();
piechart.draw(new Canvas(mBaggroundImage));
piechart = null;
ImageView mImageView = new ImageView(this);
mImageView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
mImageView.setBackgroundColor(BgColor);
mImageView.setImageBitmap(mBaggroundImage);
LinearLayout finalLayout = (LinearLayout) findViewById(R.id.pie_container);
finalLayout.addView(mImageView);
}
}
========================================================================
3.PieDetailsItem.java file
package com.myandroid.peichart;
public class PieDetailsItem {
public int count, color;
public float percent;
public String label;
}
============================================================
4.View_PieChart .java file
package com.myandroid.peichart;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
public class View_PieChart extends View
{
public static final int WAIT = 0;
public static final int IS_READY_TO_DRAW = 1;
public static final int IS_DRAW = 2;
private static final float START_INC = 30;
private Paint mBagpaints = new Paint();
private Paint mLinePaints = new Paint();
private int mWidth;
private int mHeight;
private int mGapTop;
private int mGapBottm;
private int mBgcolor;
private int mGapleft;
private int mGapright;
private int mState = WAIT;
private float mStart;
private float mSweep;
private int mMaxConnection;
private List<PieDetailsItem> mdataArray;
public View_PieChart(Context context) {
super(context);
Log.w(" single cons ", " single cons");
}
public View_PieChart(Context context, AttributeSet attr) {
super(context, attr);
Log.w(" double cons ", " double cons");
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mState != IS_READY_TO_DRAW) {
return;
}
canvas.drawColor(mBgcolor);
mBagpaints.setAntiAlias(true);
mBagpaints.setStyle(Paint.Style.FILL);
mBagpaints.setColor(0x88FF0000);
mBagpaints.setStrokeWidth(0.0f);
mLinePaints.setAntiAlias(true);
mLinePaints.setColor(0xff000000);
mLinePaints.setStrokeWidth(3.0f);
mLinePaints.setStyle(Paint.Style.STROKE);
RectF mOvals = new RectF(mGapleft, mGapTop, mWidth - mGapright, mHeight
- mGapBottm);
mStart = START_INC;
PieDetailsItem item;
for (int i = 0; i < mdataArray.size(); i++) {
item = (PieDetailsItem) mdataArray.get(i);
mBagpaints.setColor(item.color);
mSweep = (float) 360* ((float) item.count / (float) mMaxConnection);
canvas.drawArc(mOvals, mStart, mSweep, true, mBagpaints);
canvas.drawArc(mOvals, mStart, mSweep, true, mLinePaints);
mStart = mStart + mSweep;
}
mState = IS_DRAW;
}
public void setGeometry(int width, int height, int gapleft, int gapright,
int gaptop, int gapbottom, int overlayid) {
mWidth = width;
mHeight = height;
mGapleft = gapleft;
mGapright = gapright;
mGapBottm = gapbottom;
mGapTop = gaptop;
}
public void setSkinparams(int bgcolor) {
Log.w(" Set bg color : ", bgcolor + "");
mBgcolor = bgcolor;
}
public void setData(List<PieDetailsItem> data, int maxconnection) {
mdataArray = data;
mMaxConnection = maxconnection;
Log.w(" Max Connection ", maxconnection + " " + " Adataarray :"
+ data.toString());
mState = IS_READY_TO_DRAW;
}
public void setState(int state) {
mState = state;
}
public int getColorValues(int index) {
if (mdataArray == null) {
return 0;
}
else if (index < 0)
return ((PieDetailsItem) mdataArray.get(0)).color;
else if (index > mdataArray.size())
return ((PieDetailsItem) mdataArray.get(mdataArray.size() - 1)).color;
else
return ((PieDetailsItem) mdataArray.get(mdataArray.size() - 1)).color;
}
}
========================================================================
XML file Detail
main.xml
========
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:id="@+id/pie_container"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
<TextView android:id="@+id/tvAndroid" android:layout_width="110dip"
android:layout_height="wrap_content" android:layout_below="@+id/pie_container"
android:text="Android" android:padding="10dip" android:textStyle="bold"
android:background="#444544" android:layout_marginTop="200dip" />
<TextView android:id="@+id/tvIphone" android:layout_width="110dip"
android:layout_height="wrap_content" android:layout_below="@+id/pie_container"
android:layout_toRightOf="@+id/tvAndroid" android:text="IPhone"
android:padding="10dip" android:textStyle="bold" android:background="#878887"
android:layout_marginTop="200dip" />
<TextView android:id="@+id/tvBlackBerry" android:layout_width="110dip"
android:layout_height="wrap_content" android:layout_below="@+id/pie_container"
android:layout_toRightOf="@+id/tvIphone" android:text="BlackBerry"
android:padding="10dip" android:textStyle="bold" android:background="#98967F"
android:layout_marginTop="200dip" />
<TextView android:id="@+id/tvNokia" android:layout_width="160dip"
android:layout_height="wrap_content" android:layout_below="@+id/tvBlackBerry"
android:layout_toRightOf="@+id/tvSamsung" android:text="Others"
android:padding="10dip" android:textStyle="bold" android:background="#A1A1A1" />
<TextView android:id="@+id/tvSamsung" android:layout_width="160dip"
android:layout_height="wrap_content" android:layout_below="@+id/tvAndroid"
android:text="Nokia" android:padding="10dip" android:textStyle="bold"
android:background="#0000FF" />
</RelativeLayout>
==================================================================
5. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myandroid.peichart"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PeiChartActivity"
android:label="@string/app_name" >
</activity>
<activity android:name="Main"><intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter></activity>
<activity android:name="PeiChartActivity"></activity>
</application>
</manifest>
========================================================