如何设置饼图的标签颜色?
How to set pie chart's label color?
我正在使用 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);
}
}
我得到的结果是:
我想做的事情一言以蔽之
- 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)
有人可以帮我吗?
我也尝试了以下链接中提到的这些解决方案。
但其中 none 似乎有效。
要更改饼图中标签的颜色,请使用以下代码:
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
对于值文本颜色,您已经通过以下代码更改了它的颜色:
data.setValueTextColor(ContextCompat.getColor(getActivity(),R.color.black));
或者你可以像这样使用 data.setValueTextColor(Color.BLACK);
现在,关于两行显示标签文本:
我认为这是不可能的,因为图表是在 canvas 上绘制的,而 canvas 不支持多线。如果你想要多行,你必须做一些技巧,如 describe here。为此,您必须修改 MPAndroidChartLibrary。
因此,作为替代方案,您可以更改标签的文本大小,如下所示:
pieChart.setEntryLabelTextSize(12f); // You can increase or decrease value as per your need in argument
此外,您可以将 TypeFace(Font) 应用于该标签,如下所示:
pieChart.setEntryLabelTypeface(yourTypeFace);
使用
pieChart.setEntryLabelColor(Color.BLACK);
我正在使用 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);
}
}
我得到的结果是:
我想做的事情一言以蔽之
- 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)
有人可以帮我吗?
我也尝试了以下链接中提到的这些解决方案。
但其中 none 似乎有效。
要更改饼图中标签的颜色,请使用以下代码:
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 withpieChart
对于值文本颜色,您已经通过以下代码更改了它的颜色:
data.setValueTextColor(ContextCompat.getColor(getActivity(),R.color.black));
或者你可以像这样使用 data.setValueTextColor(Color.BLACK);
现在,关于两行显示标签文本:
我认为这是不可能的,因为图表是在 canvas 上绘制的,而 canvas 不支持多线。如果你想要多行,你必须做一些技巧,如 describe here。为此,您必须修改 MPAndroidChartLibrary。
因此,作为替代方案,您可以更改标签的文本大小,如下所示:
pieChart.setEntryLabelTextSize(12f); // You can increase or decrease value as per your need in argument
此外,您可以将 TypeFace(Font) 应用于该标签,如下所示:
pieChart.setEntryLabelTypeface(yourTypeFace);
使用
pieChart.setEntryLabelColor(Color.BLACK);