MPAndroidChart饼图如何不显示底部标签?

时间:2014-10-01 08:43:04

标签: android charts pie-chart mpandroidchart

PieChart

如何在饼图中显示突出显示的底部标签? 以及如何更改条目文本颜色?

1 个答案:

答案 0 :(得分:2)

您可以通过将setDrawLegend属性设置为false来实现此目的。

无论您在何处初始化您的pieChart,只需添加以下行:

pieChart.setDrawLegend(false);

[编辑]

关于更改颜色,您可以这样做:

首先,当您向图表添加一些数据时会发生这种情况。添加数据时,将PieData对象添加到图表中。此PieData对象有两个参数,名称和值。名称列表是字符串的ArrayList,但值必须是PieDataSet对象的实例。您可以在此处添加颜色并添加其他属性(例如切片之间的间距)。此外,PieDataSet对象包装集的Y值和此标签。最后,PieDataSet的值是Entry对象的ArrayList。一个Entry对象获取要显示的值,以及它在图表上的索引。

以下是一个示例DEMO代码,用于说明上述简短说明:

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

这有帮助吗?

编辑2:

这是更改饼图内文本颜色的方法:

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

我知道这不是一个理想的解决方案,但它现在应该有效。