【发布时间】:2018-08-24 06:48:31
【问题描述】:
我正在使用mpandroidchart 库来创建饼图。
我想在饼图上设置文本标签的格式,但我不知道该怎么做。我试过用
data.setValueTextColor(ContextCompat.getColor(getActivity(),R.color.black));
但是,它只改变数据的值,而不是标签。 另外,我想成为饼图中的标签,我也尝试使用以下代码,
PieEntry entry2=new PieEntry(50-scratches,"Remaining \nScratches");
但是,它没有用。
我的代码如下:
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);
}
}
简而言之我想做的事
- 更改饼图中的标签颜色(从白色变为黑色)
- 防止标签超出饼图。 (例如,如图所示的剩余划痕)
谁能帮帮我?
我还尝试了以下链接中提到的这些解决方案。
但它们似乎都不起作用。
【问题讨论】:
-
@Riddhi,试试数据。 setEntryLabelColor 并检查。
-
@NileshRathod 你提到的所有解决方案都不适合我。
-
@Riddhi 让你检查一下这个
pieChart.getLegend().setTextColor(Color.WHITE); -
@NiravBhavsar 没有这样的方法“data.setEntryLabelColor”