I've developed an android application to display a pie chart. I have used the achartengine
library to do this.
With this code below I get the pie chart output with 3 portions labeled as mentioned in categorySeries. I want to display the percentage values on the pie diagram. How can i do this?
public static final GraphicalView getPieChartView(Context context,
CategorySeries dataset, DefaultRenderer renderer) {
checkParameters(dataset, renderer);
PieChart chart = new PieChart(dataset, renderer);
return new GraphicalView(context, chart);
}
private static void checkParameters(CategorySeries dataset,
DefaultRenderer renderer) {
if (dataset == null
|| renderer == null
|| dataset.getItemCount() != renderer
.getSeriesRendererCount()) {
throw new IllegalArgumentException(
"Dataset and renderer should be not null and the dataset number of items should be equal to the number of series renderers");
}
}
and this is my onCreate method
Bundle bundle = getIntent().getExtras();
int highc = bundle.getInt("high");
int lowc = bundle.getInt("low");
int medc = bundle.getInt("medium");
RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativestatsLayout);
TextView message = (TextView) findViewById(R.id.statText);
message.setText("\n\n\tTickets Summary");
message.setGravity(50);
//layout.addView(message);
//setContentView(message);
int[] colors = new int[] { Color.rgb(255, 0, 0),Color.rgb(220, 51,51), Color.rgb(255, 191, 0) };
DefaultRenderer renderer = buildCategoryRenderer(colors);
CategorySeries categorySeries = new CategorySeries("Tickets Chart");
categorySeries.getTitle();
categorySeries.add("critical", highc);
categorySeries.add("major ", medc);
categorySeries.add("minor ", lowc);
layout.addView(getPieChartView(this, categorySeries, renderer));
}
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;
}
It is possible using achartengine. I have faced the same problem and found the answer now. Use the 1.1.0-rc2 version of achart engine. You can download it here: http://www.achartengine.org/download/
This tutorial (http://wptrafficanalyzer.in/blog/android-drawing-pie-chart-using-achartengine/) helps you to create pie chart using achartengine.
Add this line to your code to display the %of distribution in the Pie chart
defaultRenderer.setDisplayValues(true);
Reference: How to set different values for Legend and Labels in piechart While I am using Chart Engine in android
Im not sure if you still have this issue. But if your pie chart is fairly simple, you should consider drawing it your self rather using a library for it. This might help a bit.
https://github.com/blessenm/PieChartDemo
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With