应开发的需要,需要画各种各种图表,发现MPAndroidChart这个宝藏。
MPAndroidChart的Github链接
这里说明一下饼状图的画法,以及自己遇到的坑。
效果图
1.添加依赖
在build.gradle中添加依赖。
注意:依赖很重要,网上有很多MPAndroidChart的教程,你们会发现他们会有一定的差异。因为MPAndroidChart是在不断更新,所以他的使用方法也会发生变化。所以在看别人使用的教程之前,记得看看自己的依赖是否博主相同,我就遇到了这个坑(;´д`)ゞ
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'
}
2. XML
如果是使用constraintlayout布局,记得添加约束。
<com.github.mikephil.charting.charts.PieChart
android:id="@+id/contextPieChart"
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintTop_toBottomOf="@+id/context_analysis" />
3. 代码实现
//饼状图
PieChart contextPieChart = (PieChart) view.findViewById(R.id.contextPieChart);
// 添加数据
ArrayList<PieEntry> contextPieData = new ArrayList<>();
contextPieData.add(new PieEntry(25f,"Part1"));
contextPieData.add(new PieEntry(25f,"Part2"));
contextPieData.add(new PieEntry(5f,"Part3"));
contextPieData.add(new PieEntry(5f,"Part4"));
contextPieData.add(new PieEntry(30f,"Part5"));
contextPieData.add(new PieEntry(10f,"Part6"));
// label出现在饼状图的右下角,不想显示的话直接设置为空
PieDataSet context_pieDataSet = new PieDataSet(contextPieData, "");
// 设置饼状图的颜色,有几个组成成分,就需要添加几个颜色
List<Integer> colors = new ArrayList<Integer>();
colors.add(getResources().getColor(R.color.bewitched_tree));
colors.add(getResources().getColor(R.color.light_heart_blue));
colors.add(getResources().getColor(R.color.magic_powder));
colors.add(getResources().getColor(R.color.mustard_addicted));
colors.add(getResources().getColor(R.color.true_blush));
colors.add(getResources().getColor(R.color.merry));
// 设置饼状图颜色
context_pieDataSet.setColors(colors);
// 各个饼直接的空白距离
context_pieDataSet.setSliceSpace(2);
// 使用百分比
contextPieChart.setUsePercentValues(true);
// 取消饼状图的中心圆
contextPieChart.setDrawHoleEnabled(false);
// 旋转动画
contextPieChart.animateXY(1400, 1400);
// 让饼状图往左移动30
contextPieChart.setExtraLeftOffset(-30);
PieData context_pieData = new PieData(context_pieDataSet);
// 显示数值
context_pieData.setDrawValues(true);
// 设置饼状图上的字的大小
context_pieData.setValueTextSize(12);
// 设置饼状图上的字的颜色
context_pieData.setValueTextColor(WHITE);
// 使用setValueFormatter可以自定义如何显示数值,这里是显示成 数值%
context_pieData.setValueFormatter(new IValueFormatter() {
@Override
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setMaximumFractionDigits(2);
return nf.format(value) + "%";
}
});
contextPieChart.setData(context_pieData);
// 去掉description(即图表描述,自己看需不需要)
Description description = new Description();
description.setText("");
contextPieChart.setDescription(description);
// Legend 是 提示(显示各个颜色分别代表的是什么
Legend context_legend = contextPieChart.getLegend();
// 将 Legend 的位置设置到右上,垂直显示
context_legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);
context_legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);
context_legend.setOrientation(Legend.LegendOrientation.VERTICAL);
// legend内的各个部分的竖直间距
context_legend.setYEntrySpace(5);
// 对lgend的位置进行调整
context_legend.setXOffset(4);
// 设置 legend 的大小
context_legend.setTextSize(12);
Note:
这个是MPAndroidChart讲得比较好的博主写的教程MPAndroidChart 教程,但是因为是15年写的,有一些功能现在已经发生了变化。但是大体和原理没有什么变化,可以参考这个教程。