5

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);
    }

}

and the result I get is: enter image description here

What I want to do is in nutshell

  1. change the label color inside the pie chart (from white to black)
  2. 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.

link 1 link 2

but none of them seems working.

Riddhi Shah
  • 3,092
  • 5
  • 36
  • 56

2 Answers2

13

use

   pieChart.setEntryLabelColor(Color.BLACK);
user7176550
  • 1,501
  • 14
  • 19
4

To change the color of the label inside the pie chart use below code:

pieChart.setEntryLabelColor(Color.BLACK);

I assumed that pieChart is the view of your Pie chart graph view of XML i.e. pieChart = (PieChart) findViewById(R.id.chart1); If not then replace object of your view with pieChart

For value text color, you have already changed the color of it by the following code:

data.setValueTextColor(ContextCompat.getColor(getActivity(),R.color.black)); or you can use like data.setValueTextColor(Color.BLACK);

Now, regarding the display label text in two line:
I think it is not possible to do it because the chart is drawn on canvas and canvas doesn't support multiline. If you want multiline you have to do some trick as describe here. To do it you have to modify MPAndroidChartLibrary.

So as an alternative, you can change the text size of the label as below:

pieChart.setEntryLabelTextSize(12f);  // You can increase or decrease value as per your need in argument

Also, you can apply TypeFace(Font) to that label as below:

pieChart.setEntryLabelTypeface(yourTypeFace);
Viraj Patel
  • 2,113
  • 16
  • 23