英文:
How to customize pie chart using MPAndroidChart in Android?
问题
我所做的: 我正在使用 MPAndroidChart,并且我已经能够根据我的要求进行自定义,并尝试进一步的功能来移除描述标签,增加字体大小并自定义图例。我现在拥有的是:
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal">
</com.github.mikephil.charting.charts.PieChart>
public class PFrag extends Fragment {
// ...(其他代码部分)
private void setupPieChart(){
// ...(其他代码部分)
// 设置图表文本
pieChart.setCenterText("50%\n");
pieChart.setDrawEntryLabels(false);
pieChart.setContentDescription("");
// 设置中心孔半径和图例属性
pieChart.setHoleRadius(75);
Legend legend = pieChart.getLegend();
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setTextSize(12);
legend.setFormSize(20);
legend.setFormToTextSpace(2);
}
}
我正在寻找的: 尽管我尝试过,但似乎无法找到编辑以下功能的方法。
- 如何移除右下角的“描述标签”?
- 如何增加图表的文本大小?
- 如何移除图例中的蓝色项目,以使其成为默认的剩余值?
简单来说,我想要的是一个如下的图表。
为了实现这一点,我在一些搜索之后使用了 MPAndroidChart 库,但目前卡在这里。我正在使用 Android Studio 3.6.1。非常感谢对此的任何建议。
谢谢!
我已经成功解决以下两个问题:
- 如何移除右下角的“描述标签”:
pieChart.getDescription().setEnabled(false);
- 如何增加图表的文本大小?添加:
data.setValueTextSize(10);
英文:
What I have done: I'm using the MPAndroidChart and I was able to customize it to my requirement and tried further functionalities to remove Description Label, and to increase the font and customize the legend. What I have is now ;
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/chart"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
>
</com.github.mikephil.charting.charts.PieChart>
public class PFrag extends Fragment {
float time[] = {55, 95, 30 , 360 - (55+95+30)};
String activity[] ={"Jan","Feb","March",""};
PieChart pieChart;
CircularProgressIndicator circularProgress;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.panorama_frag,container,false);
pieChart = view.findViewById(R.id.chart);
setupPieChart();
//circularProgress = view.findViewById(R.id.circular_progress);
// circularProgress.setMaxProgress(10000);
// circularProgress.setCurrentProgress(5000);
return view;
}
private void setupPieChart(){
//pupulating list of PieEntires
List<PieEntry> pieEntires = new ArrayList<>();
for( int i = 0 ; i<time.length;i++){
pieEntires.add(new PieEntry(time[i],activity[i]));
}
PieDataSet dataSet = new PieDataSet(pieEntires,"");
dataSet.setColors(ColorTemplate.MATERIAL_COLORS);
PieData data = new PieData(dataSet);
//Get the chart
pieChart.setData(data);
pieChart.invalidate();
pieChart.setCenterText("50% \n ");
pieChart.setDrawEntryLabels(false);
pieChart.setContentDescription("");
//pieChart.setDrawMarkers(true);
//pieChart.setMaxHighlightDistance(34);
pieChart.setEntryLabelTextSize(12);
pieChart.setHoleRadius(75);
//legend attributes
Legend legend = pieChart.getLegend();
legend.setForm(Legend.LegendForm.CIRCLE);
legend.setTextSize(12);
legend.setFormSize(20);
legend.setFormToTextSpace(2);
}
}
What I'm looking for: Though I tried it seems unable to find a way to edit the below functionalities.
- How to remove the "Description Label" in the right-left corner?
- How to increase the text size of the chart?
- How to remove the blue item from the legend so that it will be the default remaining value?
Simply what I'm looking for is a graph like below.
To achieve that I used MPAndoridChart library after some searching and stuck here. I'm using Android Studio 3.6.1. I would really appreciate any suggestion on this.
Thank you!
I was able to solve below two queries:
-
How to remove the "Description Label" in the right-left corner
pieChart.getDescription().setEnabled(false);
-
How to increase the text size of the chart? > add
data.setValueTextSize(10);
答案1
得分: 2
要移除数据内部的标签,只需使用以下代码:
dataSet.setDrawValues(false)
英文:
To remove label inside data just use
dataSet.setDrawValues(false)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论