This code helps to draw a chart in your application using 'AChartEngine' charting library.
Download AChartEngine charting library from the below link.
http://code.google.com/p/achartengine/downloads/detail?name=achartengine-1.0.0.jar&can=2&q=
Now create a folder named "libs" in your project and copy paste the achartengine.jar file inside the
libs folder.
Now Right click on the project name -> Properties -> Java Build Path -> Under Libraries tab ->
Add Jar -> click on your project name -> libs -> achartengine.jar -> OK
Now you have successfully Imported achartengine.jar file into the application.
Create the main Activity
Charttest.java
package com.example.charttest;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.RelativeLayout;
public class Charttest extends Activity {
RelativeLayout LayoutToDisplayChart;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chattest);
LayoutToDisplayChart=(RelativeLayout)findViewById(R.id.relative);
Intent achartIntent = new Chart().execute(Charttest.this,LayoutToDisplayChart);
}
}
Chart.java
package com.example.charttest;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.widget.RelativeLayout;
public class Chart
{
private GraphicalView ChartView1;
private GraphicalView mChartView2;
static int count=5;
int[] Mycolors = new int[] { Color.RED, Color.parseColor("#FA58F4"),
Color.parseColor("#0B0B61"),
Color.parseColor("#800080"),Color.parseColor("#008000"),Color.GRAY };
public Intent execute(Context context,RelativeLayout parent) {
int[] colors = new int[count];
for(int i=0;i<count;i++)
{
colors[i]=Mycolors[i];
}
DefaultRenderer renderer = buildCategoryRenderer(colors);
renderer.setPanEnabled(false);// Disable User Interaction
renderer.setLabelsColor(Color.BLACK);
renderer.setShowLabels(true);
//renderer.setChartTitle("Total Assets");
renderer.setLabelsTextSize(12);
CategorySeries categorySeries = new CategorySeries("Fruits");
categorySeries.add("Apple", 36);
categorySeries.add("Banana", 23);
categorySeries.add("Grapes", 30);
categorySeries.add("Guava", 8);
categorySeries.add("Orange", 3);
ChartView1=ChartFactory.getPieChartView(context, categorySeries,renderer);
parent.addView(ChartView1);
return ChartFactory.getPieChartIntent(context, categorySeries, renderer,null);
}
protected DefaultRenderer buildCategoryRenderer(int[] colors)
{
DefaultRenderer renderer = new DefaultRenderer();
for (int color : colors) {
SimpleSeriesRenderer r = new SimpleSeriesRenderer();
r.setColor(color);
renderer.addSeriesRenderer(r);
}
return renderer;
}
}
activity_chattest.xml
<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" >
<RelativeLayout
android:id="@+id/relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</RelativeLayout>
</RelativeLayout>
Screen shot
Very simple and powerful contents. Your content was really useful for me. I only have a correction to tell you. You have to set main.xml in Charttest activity. Currently your adding activity_chattest.
ReplyDeleteThanks
ReplyDeleteThank u
ReplyDeleteunable to start activity componentinfo java lang nullpointerexception
ReplyDeletewounderful post !!helped a lot.:)Thank you Anushree:)
ReplyDeleteHI can u please tell how to remove the layout below the pie chart showing the series of fruits their..
ReplyDeleteThank you Anusree :-)
ReplyDeletehow about if ill going to get the items in the database?can you give some code so that i can try it?thanks
ReplyDeletehow to download this code
ReplyDeletehow to download this code
ReplyDeleteThank you for your Tutorial.
ReplyDeletewonderful post helped a lot.how to download this code please..........
ReplyDeleteThank you for your Tutorial very use full Tutorial..............