Thursday, 6 June 2013

Draw Piechart in Android using achartengine

Here Below CreatePieChart() method will be create piechart using Achartengine in Android.
In  CreatePieChart()  method org.achartengine.GraphicalActivity class is Responsable to create PieChart.


Android Activity(PieChart.java)

package com.androidsurya.piechart;

import org.achartengine.ChartFactory;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;

public class PieChart extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CreatePieChart();
}

private void CreatePieChart() {

// Pie Chart Section Names
String[] code = new String[] { "IOS", "ANDROID" };

// Pie Chart Section Value
double[] distribution = { 40, 60 };

// Color of each Pie Chart Sections
int[] colors = { Color.GRAY, Color.GREEN };

// Instantiating CategorySeries to plot Pie Chart
CategorySeries distributionSeries = new CategorySeries(
"Mobile Platforms");
for (int i = 0; i < distribution.length; i++) {
// Adding a slice with its values and name to the Pie Chart
distributionSeries.add(code[i], distribution[i]);
}
// Instantiating a renderer for the Pie Chart
DefaultRenderer defaultRenderer = new DefaultRenderer();
for (int i = 0; i < distribution.length; i++) {
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
seriesRenderer.setColor(colors[i]);
seriesRenderer.setDisplayChartValues(true);
// Adding a renderer for a slice
defaultRenderer.addSeriesRenderer(seriesRenderer);
}
defaultRenderer.setLegendTextSize(30);
defaultRenderer.setChartTitle("Mobile Platforms");
defaultRenderer.setChartTitleTextSize(20);
defaultRenderer.setZoomButtonsVisible(true);
defaultRenderer.setBackgroundColor(45454545);

// Creating an intent to plot bar chart using dataset and
// multipleRenderer
Intent intent = ChartFactory.getPieChartIntent(getBaseContext(),
distributionSeries, defaultRenderer,
"PieChart");

// Start Activity
startActivity(intent);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

Android Manifest file

 <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.androidsurya.piechart.PieChart"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="org.achartengine.GraphicalActivity" />
    </application>

Note: 
 Don't Forget Register this Below Activity In Manifest File
   <activity android:name="org.achartengine.GraphicalActivity" />

Output Screenshot:





















11 comments:

  1. Can please post whole code Three Class files are missing

    ReplyDelete
    Replies
    1. hi janardhan u can download full source using above Download Source Button. if u r unable to download let me know..

      Delete
  2. hello, i need your help i having error on this few import
    import org.achartengine.ChartFactory;
    import org.achartengine.model.CategorySeries;
    import org.achartengine.renderer.DefaultRenderer;
    import org.achartengine.renderer.SimpleSeriesRenderer;

    can i know how to solve it? please email me at: jiunhaw924@hotmail.com
    need ur help urgently
    thank you in advance

    ReplyDelete
    Replies
    1. Hi Chan

      Please include "achartengine-1.0.0.jar" in your project .then your problem will be resolve. if you unable to resolve above issue let me know.

      Delete
    2. tq, good work!
      problem solved

      Delete
  3. This comment has been removed by a blog administrator.

    ReplyDelete
  4. hi surya..i want to how create dynamic pie chart using sqlite database..my example is i have data like moods stored in my database like happy,sad,angry,bored etc a person one day so many times happy and so many times sad etc.i want to retrive that happy sad etc data and draw a pie chart using that data how?thanks in advance

    ReplyDelete
    Replies

    1. Hi krishna

      Just pass your sqlite data to distribution array it's enough.
      //example data passing.

      // Pie Chart Section Names
      String[] code = new String[] { "HAPPY", "SAD" };

      // Pie Chart Section Value---70& 30 are getting from the sqlite database
      double[] distribution = { 70, 30};

      // Color of each Pie Chart Sections
      int[] colors = { Color.GRAY, Color.GREEN };

      If you need any help let me know.

      Delete
  5. how could i create dynamic slice of piechart

    ReplyDelete
    Replies
    1. Hi Manoj

      Just pass your dynamic values to the objects like code , distribution ,colors you can achieve.

      Let me know the scenario i will try to give best option.

      Delete

Android SQLite Database Viewer or Debuging with Stetho

Every Android Developer uses SQLite Database to store data into the Android Application data. But to view the data in SQLite have a lot of...