Vahan
Vahan

Reputation: 3268

MPAndoirdChart Piechart how not to show bottom labels?

PieChart

How not to show the highlighted bottom labels in pie chart? And how to change entries text color?

Upvotes: 1

Views: 1827

Answers (1)

Mike
Mike

Reputation: 4570

You can do that by setting the setDrawLegend property to false.

Wherever you initialize your pieChart just add the following line:

pieChart.setDrawLegend(false);

[EDIT]

About changing the colors this is what you can do:

First of all this is happening when you add some data to your chart. When you add the data you add a PieData object to the chart. This PieData object takes 2 arguments, the names, and the values. The list of names is an ArrayList of Strings, but the values need to be an instance of a PieDataSet object. This is where you can add the colors and add other properties as well(such as the spacing between slices). Also, the PieDataSet object wraps up the Y values of a set and the label for this. And finally the PieDataSet's values are an ArrayList of Entry objects. One single Entry object takes the value to be shown, and it's index on the chart.

Here is a sample DEMO code that illustrates the above short description:

ArrayList<Entry> yChartValues = new ArrayList<Entry>();
int[] chartColorsArray = new int[] {
      R.color.clr1,
      R.color.clr2,
      R.color.clr3,
      R.color.clr4,
      R.color.clr5
};

// These are the 2 important elements to be passed to the chart
ArrayList<String> chartNames = new ArrayList<String>();
PieDataSet chartValues = new PieDataSet(yChartValues, "");

for (int i = 0; i < 5; i++) {
     yChartValues.add(new Entry((float) i*2, i));
     chartNames.add(String.valueOf(i));
}

chartValues.setSliceSpace(1f); // Optionally you can set the space between the slices
chartValues.setColors(ColorTemplate.createColors(this, chartColorsArray)); // This is where you set the colors. The first parameter is the Context, use "this" if you're on an Activity or "getActivity()" if you're on a fragment

// And finally add all these to the chart
pieChart.setData(new PieData(chartNames, chartValues));

Does this help?

Edit 2:

This is how you change the color of the text inside the pie-slices:

PieChart pieChart = ...;

// way 1, simply change the color:
pieChart.setValueTextColor(int color);

// way 2, acquire the whole paint object and do whatever you want    
Paint p = pieChart.getPaint(Chart.PAINT_VALUES);
p.setColor(yourcolor);

I know this is not an ideal solution, but it should work for now.

Upvotes: 2

Related Questions