In your application, you might want to represent data to the user using pie Chart. In this tip, I am going to show you how to create a pie chart using AChartEngine library. To use AChartEngine library, you need to download it from the link: AChartEngine download. Extract the zip file. Then copy the achartengine-1.1.0.jar file to the libs folder of your current working project. In Android Studio, you need to mark the jar file as library file by right-clicking the file and choose Add As Library…
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.LinearLayout;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.CategorySeries;
import org.achartengine.renderer.DefaultRenderer;
import org.achartengine.renderer.SimpleSeriesRenderer;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawPieChart();
}
private void drawPieChart(){
// Ceate CategorySeries
CategorySeries categorySeries=new CategorySeries("Pie chart categories");
// Add categories
categorySeries.add("Female",312000);
categorySeries.add("Male",21200);
// Add series render to each slide of the pie chart
int[] colors={Color.GREEN, Color.MAGENTA};
DefaultRenderer defaultRenderer=new DefaultRenderer();
for(int i = 0 ;i<categorySeries.getItemCount();i++){
SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
// Spcify render options
seriesRenderer.setColor(colors[i]);
seriesRenderer.setDisplayChartValues(true);
seriesRenderer.setDisplayChartValuesDistance(0);
seriesRenderer.setShowLegendItem(true);
defaultRenderer.addSeriesRenderer(seriesRenderer);
}
// Specify default render options
defaultRenderer.setPanEnabled(true);
defaultRenderer.setAntialiasing(true);
defaultRenderer.setShowLabels(true);
defaultRenderer.setShowLegend(true);
defaultRenderer.setChartTitle("Female and Male Garment Workers in 2015");
// Get pie chart view
GraphicalView chartView = ChartFactory.getPieChartView(this, categorySeries,defaultRenderer);
// Add the pie chart view to the layout to show
LinearLayout chartlayout=(LinearLayout)findViewById(R.id.llayout);
chartlayout.addView(chartView);
}
}
|
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.