I am using mpandroidchart library for creating a pie chart.
I wanted to format text labels on the pie chart but I don't know how to do it. I tried it using
data.setValueTextColor(ContextCompat.getColor(getActivity(),R.color.black));
but, it only changes the value of data, not the labels. also, I wanted to be the label inside the pie chart I also tried that using the following code,
PieEntry entry2=new PieEntry(50-scratches,"Remaining \nScratches");
But, it didn't work.
My code is as follows:
private void setPiechart(float scratches) {
List<PieEntry> values = new ArrayList<>();
PieEntry entry1=new PieEntry(scratches,"Scratches");
PieEntry entry2=new PieEntry(50-scratches,"Remaining \nScratches");
values.add(entry1);
values.add(entry2);
PieDataSet dataSet = new PieDataSet(values,"");
dataSet.setColors(ContextCompat.getColor(getActivity(),R.color.color_veridoc_gradient1),
ContextCompat.getColor(getActivity(),R.color.colorBgChat));
dataSet.setHighlightEnabled(true);
dataSet.setAutomaticallyDisableSliceSpacing(true);
dataSet.setSliceSpace(10);
PieData data=new PieData(dataSet);
data.setValueTextSize(20f);
data.setValueTextColor(ContextCompat.getColor(getActivity(),R.color.black));
pieChart.setData(data);
Description description=new Description();
description.setText("Scratches ");
pieChart.setDescription(description);
pieChart.setDrawHoleEnabled(true);
pieChart.setHoleRadius(10f);
pieChart.animateY(500);
Legend legend = pieChart.getLegend();
legend.setEnabled(true);
legend.setTextColor(Color.BLACK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
pieChart.setElevation(10);
}
}
What I want to do is in nutshell
- change the label color inside the pie chart (from white to black)
- Prevent the label from going outside the pie chart. (e.g. Remaining Scratches as shown in image)
Can anybody help me with it?
I also tried these solutions mentioned in followed links.
but none of them seems working.