I am using MPAndroidChart and I am trying to create a pie chart that looks like a donut using mpandroidchart lib, but somehow i am unable to do that. Below is my code-
ArrayList<Entry> entries = new ArrayList<>();
entries.add(new Entry((float) 20.0, 0));
entries.add(new Entry((float) 30.0, 1));
int colors[] = {Color.parseColor("#DCDEE0"),Color.parseColor("#466A80"),Color.parseColor("#0078CA"),Color.parseColor("#5BC2E7"),Color.parseColor("#99E4FF")};
PieDataSet dataset = new PieDataSet(entries, "# of Calls");
dataset.setColors(colors);
dataset.setSliceSpace(3f);
ArrayList<String> labels = new ArrayList<String>();
labels.add("January");
labels.add("February");
/* labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");*/
PieChart chart1 = new PieChart(this);
chart1.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5f));
chart1.setHoleRadius(60f);
chart1.setHoleColorTransparent(false);
chart1.setDrawHoleEnabled(true);
chart1.setUsePercentValues(true);
chart1.setHoleColor(Color.WHITE);
PieChart chart2 = new PieChart(this);
chart2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.5f));
LinearLayout chart = (LinearLayout)findViewById(R.id.chart);
chart.addView(chart1);
chart.addView(chart2);
PieData data = new PieData(labels, dataset);
chart1.setData(data);
chart2.setData(data);
I am able to draw pie chart but can't make it look like a donut.Please refer the attached screen shot. Can someone please help.
Yes try this code:=
public class piechart extends ActionBarActivity {
RelativeLayout mainLayout;
PieChart mChart;
// we're going to display pie chart for smartphones martket shares
float[] yData = { 15, 10, 15, 8, 4,10 };
String[] xData = { "Technical excellence", "Nimble", "Innovation", "Integrity", "Colloboration","Passion" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_piechart);
mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
mChart = new PieChart(this);
mainLayout.addView(mChart);
mainLayout.setBackgroundColor(Color.parseColor("#ffffff"));
// configure pie chart
mChart.setUsePercentValues(true);
mChart.setDescription("Employee Analysis");
// enable hole and configure
mChart.setDrawHoleEnabled(true);
mChart.setHoleColorTransparent(true);
mChart.setHoleRadius(7);
mChart.setTransparentCircleRadius(10);
// enable rotation of the chart by touch
mChart.setRotationAngle(0);
mChart.setRotationEnabled(true);
// set a chart value selected listener
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
@Override
public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
// display msg when value selected
if (e == null)
return;
Intent i=new Intent(piechart.this,Nimble.class);
startActivity(i);
}
@Override
public void onNothingSelected() {
}
});
// add data
addData();
// customize legends
Legend l = mChart.getLegend();
l.setPosition(Legend.LegendPosition.RIGHT_OF_CHART);
l.setXEntrySpace(7);
l.setYEntrySpace(5);
}
private void addData() {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();
for (int i = 0; i < yData.length; i++)
yVals1.add(new Entry(yData[i], i));
ArrayList<String> xVals = new ArrayList<String>();
for (int i = 0; i < xData.length; i++)
xVals.add(xData[i]);
// create pie data set
PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);
// add many colors
ArrayList<Integer> colors = new ArrayList<Integer>();
for (int c : ColorTemplate.VORDIPLOM_COLORS)
colors.add(c);
for (int c : ColorTemplate.JOYFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.COLORFUL_COLORS)
colors.add(c);
for (int c : ColorTemplate.LIBERTY_COLORS)
colors.add(c);
for (int c : ColorTemplate.PASTEL_COLORS)
colors.add(c);
colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);
// instantiate pie data object now
PieData data = new PieData(xVals, dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GRAY);
mChart.setData(data);
// undo all highlights
mChart.highlightValues(null);
// update pie chart
mChart.invalidate();
mChart.getLegend().setEnabled(false);
}
This is a known issue that has been fixed in the latest commit. https://github.com/PhilJay/MPAndroidChart/issues/527
The v2.1.0 of the library that will contain the fix should be out within the next week!
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