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:
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: