Wednesday, 27 February 2013

Pie Chart in Android without using external jar

Simple Pie Chart example without using any external jar in android. Here We are creating chart using Canvas class in android.

This is the sample screenshot of our application.



 And this is also sample screenshot of our application.


It is easy to draw this chart in android.

First our xml will be.



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </LinearLayout>

</RelativeLayout>
Our Activity will be.
public class MainActivity extends Activity {

float values[] = { 700, 400, 100, 500,600 };
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

LinearLayout lv1 = (LinearLayout) findViewById(R.id.linear);

values = calculateData(values);
MyGraphview graphview = new MyGraphview(this, values);
lv1.addView(graphview);  
}

private float[] calculateData(float[] data) {
float total = 0;
for (int i = 0; i < data.length; i++) {
total += data[i];
}
for (int i = 0; i < data.length; i++) {
data[i] = 360 * (data[i] / total);
}
return data;
}

public class MyGraphview extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
RectF rectf = new RectF(120, 120, 380, 380);
float temp = 0;

public MyGraphview(Context context, float[] values) {
super(context);
value_degree = new float[values.length];
for (int i = 0; i &lt values.length; i++) {
value_degree[i] = values[i];
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Random r;
for (int i = 0; i < value_degree.length; i++) {
if (i == 0) {
r = new Random();
int color = Color.argb(100, r.nextInt(256), r.nextInt(256),
r.nextInt(256));
paint.setColor(color);
canvas.drawArc(rectf, 0, value_degree[i], true, paint);
} else {
temp += value_degree[i - 1];
r = new Random();
int color = Color.argb(255, r.nextInt(256), r.nextInt(256),
r.nextInt(256));
paint.setColor(color);
canvas.drawArc(rectf, temp, value_degree[i], true, paint);
}
}
}  
}
}
Explainations.
float values[] = { 700, 400, 100, 500,600 };
this line defines how many attributes the chart should have. You can chnge this values and also number of elements. It will working fine.
values = calculateData(values);
In this method we calculating percentage for each element to decide how much space each element should have in piechart. So here we are calculating it for 360.
MyGraphview graphview = new MyGraphview(this, values);
In this line we are generating a chart view and store it in varalbe graphview.
lv1.addView(graphview);  
Here we are add that view to our linearlayout using addview method. Generation of graphview
RectF rectf = new RectF(120, 120, 380, 380);
This line decide at which position our chart should appear.
public MyGraphview(Context context, float[] values) {
super(context);
value_degree = new float[values.length];
for (int i = 0; i &lt values.length; i++) {
value_degree[i] = values[i];
}
}
Here we just change MainActivity's values to MyGraphview class's values. Next on draw method. We are drawing our chart here only.
int color = Color.argb(255, r.nextInt(256), r.nextInt(256),r.nextInt(256));
This line is for generating a random color. By this line our application will show different color at each time we are opening it.
paint.setColor(color);
Here just we are apply that generated color to our paint object.
canvas.drawArc(rectf, temp, value_degree[i], true, paint);
This line is most importatnt in this application. Because by using this line only we are drawing arc for each elemnt. Note: We are drawing arc only, not circle. If one arc ended at on position we start drawing another arc from that position itself. Thats why it is visible as Pi Chart.
Related article PieChart using AChartEngine

Thank You

Tuesday, 26 February 2013

Multiple ArrayList in Java

Like Multiple array also we can create and use multiple arraylist.

Declaration of Multiple ArrayList in JAVA.

ArrayList<integer> sets[];

initialization of Multiple ArrayList in JAVA.

sets = new ArrayList[5];

Sample program using Multiple ArrayList in JAVA.

public class A {
static ArrayList<integer> numbers[];

public static void main(String[] args) throws ParseException {
numbers= new ArrayList[5];
numbers[0] = new ArrayList<integer>();
numbers[0].add(1);
// extending your code in order to use more than one value of the List array
numbers[1] = new ArrayList<integer>();
numbers[1].add(2);
numbers[1].add(3);
// if you want to get the values of first array.
for(int i = 0;i&ltnumbers[1].size();i++)
System.out.println(numbers[1].get(i));
// if you want to get the values of whole arraylist.
for(int j = 0; j&ltsets.length;j++)
for(int i = 0;i&ltnumbers[j].size();i++)
System.out.println(numbers[j].get(i));
}
}