Android 图表开源框架之MPAndroidChart PieChart扇形图(注意版本是3.0一下的2.0.9),使用jar包依赖试试mpandroidchartlibrary-2-0-9来实现PieChart扇形图。
mpandroidchartlibrary-2-0-9.jar下载地址:
https://www.kumapai.com/open/11125-MPAndroidChart/v2-2-5
1 2 3 4 5 6 7 8 9 10 11 12 13 | 在Project即工程下的build.gradle文件里添加 maven { url "https://jitpack.io" } 添加下来是这个样子的: allprojects { repositories { jcenter() maven { url "https://jitpack.io" } google() } } 然后在项目下的build.gradle文件里添加 //compile 'com.github.PhilJay:MPAndroidChart:v3.0.1' implementation 'com.github.PhilJay:MPAndroidChart:v3.0.1' |
效果图1:

效果图2:

效果图3:

效果图4:

一.具体实现:
1.主函数代码:
Chartctivity.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.DisplayMetrics; import com.example.qd.douyinwu.R; import com.github.mikephil.charting.charts.PieChart; import com.github.mikephil.charting.components.Legend; import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.PieData; import com.github.mikephil.charting.data.PieDataSet; import java.util.ArrayList; /** * 图表制作 饼形图 * https://github.com/PhilJay/MPAndroidChart * * 安卓 图表开源框架 * https://www.jianshu.com/p/51aa1e46cf18 * https://github.com/lecho/hellocharts-android * https://github.com/developerHaoz/HelloChartDemo * * https://www.ctolib.com/luweibin3118-PieChartView.html * * https://github.com/developerHaoz 一个不错的项目 */ public class Chartctivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_chart); initPieChart(); } private void initPieChart() { PieChart mChart = (PieChart) findViewById(R.id.spread_pie_chart); PieData mPieData = getPieData(4, 100); showChart(mChart, mPieData); } private void showChart(PieChart pieChart, PieData pieData) { pieChart.setHoleColorTransparent(true); pieChart.setHoleRadius(60f); //半径 pieChart.setTransparentCircleRadius(64f); // 半透明圈 //pieChart.setHoleRadius(0); //实心圆 pieChart.setDescription("测试饼状图"); // mChart.setDrawYValues(true); pieChart.setDrawCenterText(true); //饼状图中间可以添加文字 pieChart.setDrawHoleEnabled(true); pieChart.setRotationAngle(90); // 初始旋转角度 // draws the corresponding description value into the slice // mChart.setDrawXValues(true); // enable rotation of the chart by touch pieChart.setRotationEnabled(true); // 可以手动旋转 // display percentage values pieChart.setUsePercentValues(true); //显示成百分比 // mChart.setUnit(" €"); // mChart.setDrawUnitsInChart(true); // add a selection listener // mChart.setOnChartValueSelectedListener(this); // mChart.setTouchEnabled(false); // mChart.setOnAnimationListener(this); pieChart.setCenterText("Quarterly Revenue"); //饼状图中间的文字 //设置数据 pieChart.setData(pieData); // undo all highlights // pieChart.highlightValues(null); // pieChart.invalidate(); Legend mLegend = pieChart.getLegend(); //设置比例图 mLegend.setPosition(Legend.LegendPosition.RIGHT_OF_CHART); //最右边显示 // mLegend.setForm(LegendForm.LINE); //设置比例图的形状,默认是方形 mLegend.setXEntrySpace(7f); mLegend.setYEntrySpace(5f); // Legend l = mChart.getLegend(); // l.setEnabled(true); //是否启用图列(true:下面属性才有意义 // l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); // l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); // l.setOrientation(Legend.LegendOrientation.VERTICAL); // l.setForm(Legend.LegendForm.DEFAULT); //设置图例的形状 // l.setFormSize(10); //设置图例的大小 // l.setFormToTextSpace(10f); //设置每个图例实体中标签和形状之间的间距 // l.setDrawInside(false); // l.setWordWrapEnabled(true); //设置图列换行(注意使用影响性能,仅适用legend位于图表下面) // l.setXEntrySpace(10f); //设置图例实体之间延X轴的间距(setOrientation = HORIZONTAL有效) // l.setYEntrySpace(8f); //设置图例实体之间延Y轴的间距(setOrientation = VERTICAL 有效) // l.setYOffset(0f); //设置比例块Y轴偏移量 // l.setTextSize(14f); //设置图例标签文本的大小 // l.setTextColor(Color.parseColor("#333333"));//设置图例标签文本的颜色 pieChart.animateXY(1000, 1000); //设置动画 // mChart.spin(2000, 0, 360); } /** * @param count 分成几部分 * @param range */ private PieData getPieData(int count, float range) { ArrayList xValues = new ArrayList(); //xVals用来表示每个饼块上的内容 for (int i = 0; i < count; i++) { xValues.add("Quarterly" + (i + 1)); //饼块上显示成Quarterly1, Quarterly2, Quarterly3, Quarterly4 } ArrayList yValues = new ArrayList(); //yVals用来表示封装每个饼块的实际数据 // 饼图数据 /** * 将一个饼形图分成四部分, 四部分的数值比例为14:14:34:38 * 所以 14代表的百分比就是14% */ float quarterly1 = 14; float quarterly2 = 14; float quarterly3 = 34; float quarterly4 = 38; yValues.add(new Entry(quarterly1, 0)); yValues.add(new Entry(quarterly2, 1)); yValues.add(new Entry(quarterly3, 2)); yValues.add(new Entry(quarterly4, 3)); //y轴的集合 PieDataSet pieDataSet = new PieDataSet(yValues, "Quarterly Revenue 2014"/*显示在比例图上*/); pieDataSet.setSliceSpace(0f); //设置个饼状图之间的距离 ArrayList colors = new ArrayList(); // 饼图颜色 colors.add(Color.rgb(205, 205, 205)); colors.add(Color.rgb(114, 188, 223)); colors.add(Color.rgb(255, 123, 124)); colors.add(Color.rgb(57, 135, 200)); pieDataSet.setColors(colors); DisplayMetrics metrics = getResources().getDisplayMetrics(); float px = 5 * (metrics.densityDpi / 160f); pieDataSet.setSelectionShift(px); // 选中态多出的长度 PieData pieData = new PieData(xValues, pieDataSet); return pieData; } } |
监听事件:
1 2 3 4 5 6 7 8 9 10 11 12 | //监听事件 pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() { @Override public void onValueSelected(Entry entry, int i, Highlight highlight) { Log.e("SGF",""+i+entry+highlight); //打印日志 } @Override public void onNothingSelected() { //整体扇形的监听 } }); |
2.相关属性:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | mChart.getDescription().setEnabled(false); //设置pieChart图表的描述 mChart.setBackgroundColor(Color.WHITE); //设置pieChart图表背景色 mChart.setRotationEnabled(true);//可以手动旋转 mChart.setDragDecelerationFrictionCoef(0.95f);//设置pieChart图表转动阻力摩擦系数[0,1] mChart.setHighlightPerTapEnabled(true); //设置piecahrt图表点击Item高亮是否可用 mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);// 设置pieChart图表展示动画效果 Legend l = mChart.getLegend(); l.setEnabled(true); //是否启用图列(true:下面属性才有意义 l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); l.setOrientation(Legend.LegendOrientation.VERTICAL); l.setForm(Legend.LegendForm.DEFAULT); //设置图例的形状 l.setFormSize(10); //设置图例的大小 l.setFormToTextSpace(10f); //设置每个图例实体中标签和形状之间的间距 l.setDrawInside(false); l.setWordWrapEnabled(true); //设置图列换行(注意使用影响性能,仅适用legend位于图表下面) l.setXEntrySpace(10f); //设置图例实体之间延X轴的间距(setOrientation = HORIZONTAL有效) l.setYEntrySpace(8f); //设置图例实体之间延Y轴的间距(setOrientation = VERTICAL 有效) l.setYOffset(0f); //设置比例块Y轴偏移量 l.setTextSize(14f); //设置图例标签文本的大小 l.setTextColor(Color.parseColor("#333333"));//设置图例标签文本的颜色 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | mChart.getDescription().setEnabled(false); //设置pieChart图表的描述 mChart.setBackgroundColor(Color.WHITE); //设置pieChart图表背景色 mChart.setRotationEnabled(true);//可以手动旋转 mChart.setDragDecelerationFrictionCoef(0.95f);//设置pieChart图表转动阻力摩擦系数[0,1] mChart.setHighlightPerTapEnabled(true); //设置piecahrt图表点击Item高亮是否可用 mChart.animateY(1400, Easing.EasingOption.EaseInOutQuad);// 设置pieChart图表展示动画效果 Legend l = mChart.getLegend(); l.setEnabled(true); //是否启用图列(true:下面属性才有意义 l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP); l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT); l.setOrientation(Legend.LegendOrientation.VERTICAL); l.setForm(Legend.LegendForm.DEFAULT); //设置图例的形状 l.setFormSize(10); //设置图例的大小 l.setFormToTextSpace(10f); //设置每个图例实体中标签和形状之间的间距 l.setDrawInside(false); l.setWordWrapEnabled(true); //设置图列换行(注意使用影响性能,仅适用legend位于图表下面) l.setXEntrySpace(10f); //设置图例实体之间延X轴的间距(setOrientation = HORIZONTAL有效) l.setYEntrySpace(8f); //设置图例实体之间延Y轴的间距(setOrientation = VERTICAL 有效) l.setYOffset(0f); //设置比例块Y轴偏移量 l.setTextSize(14f); //设置图例标签文本的大小 l.setTextColor(Color.parseColor("#333333"));//设置图例标签文本的颜色 |
1 2 3 4 5 6 7 8 9 10 11 | //饼状图数据集 PieDataSet PieDataSet pieDataSet = new PieDataSet(pieEntryList, "图表名称"); pieDataSet.setSliceSpace(3f); //设置饼状Item之间的间隙 pieDataSet.setSelectionShift(30f); //设置饼状Item被选中时变化的距离 pieDataSet.setColors(colors); //为DataSet中的数据匹配上颜色集(饼图Item颜色) //最终数据 PieData PieData pieData = new PieData(pieDataSet); pieData.setDrawValues(true); //设置是否显示数据实体(百分比,true:以下属性才有意义) pieData.setValueTextColor(Color.BLUE); //设置所有DataSet内数据实体(百分比)的文本颜色 pieData.setValueTextSize(12f); //设置所有DataSet内数据实体(百分比)的文本字体大小 pieData.setValueFormatter(new PercentFormatter());//设置所有DataSet内数据实体(百分比)的文本字体格式 |
设置颜色:
1 2 3 4 5 6 7 8 9 10 | ArrayList<PieEntry> pieEntryList = new ArrayList();//数据列表 ArrayList<Integer> colors = new ArrayList();//颜色列表 for (int i = 0; i < list.size(); i++) {//list为数据列表 Random random = new Random(); int r = random.nextInt(256); int g = random.nextInt(256); int b = random.nextInt(256); colors.add(Color.rgb(r,g,b)); pieEntryList.add(new PieEntry(Float.parseFloat(townsChartEntity.getRecordSet0().getR().get(i).getC().get(2)) * 100 / all, townsChartEntity.getRecordSet0().getR().get(i).getC().get(1))); } |
1 2 3 | mChart.setData(pieData); mChart.highlightValues(null); mChart.invalidate(); //将图表重绘以显示设置的属性和数据 |
3.布局代码:
1 2 3 4 5 6 7 8 9 10 11 12 | <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" android:orientation="vertical"> <com.github.mikephil.charting.charts.PieChart android:id="@+id/spread_pie_chart" android:layout_margin="20dp" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> |
4.参考案例:
https://github.com/PhilJay/MPAndroidChart