Android 图表开源框架之MPAndroidChart PieChart扇形图(一)
Android 图表开源框架之MPAndroidChart PieChart扇形图(注意版本是3.0以下的2.0.9),使用jar包依赖试试mpandroidchartlibrary-2-0-9来实现PieChart扇形图。
项目下载地址
mpandroidchartlibrary-2-0-9.jar下载地址:Jar包下载地址
https://www.kumapai.com/open/11125-MPAndroidChart/v2-2-5
在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:
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 {@Overrideprotected 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 touchpieChart.setRotationEnabled(true); // 可以手动旋转
// display percentage valuespieChart.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;}}
监听事件:
//监听事件pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {@Overridepublic void onValueSelected(Entry entry, int i, Highlight highlight) {Log.e("SGF",""+i+entry+highlight); //打印日志}@Overridepublic void onNothingSelected() {//整体扇形的监听}});
2.相关属性:
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"));//设置图例标签文本的颜色
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"));//设置图例标签文本的颜色
//饼状图数据集 PieDataSetPieDataSet pieDataSet = new PieDataSet(pieEntryList, "图表名称");pieDataSet.setSliceSpace(3f); //设置饼状Item之间的间隙pieDataSet.setSelectionShift(30f); //设置饼状Item被选中时变化的距离pieDataSet.setColors(colors); //为DataSet中的数据匹配上颜色集(饼图Item颜色)//最终数据 PieDataPieData pieData = new PieData(pieDataSet);pieData.setDrawValues(true); //设置是否显示数据实体(百分比,true:以下属性才有意义)pieData.setValueTextColor(Color.BLUE); //设置所有DataSet内数据实体(百分比)的文本颜色pieData.setValueTextSize(12f); //设置所有DataSet内数据实体(百分比)的文本字体大小pieData.setValueFormatter(new PercentFormatter());//设置所有DataSet内数据实体(百分比)的文本字体格式
设置颜色:
ArrayList pieEntryList = new ArrayList();//数据列表ArrayList 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)));
}
mChart.setData(pieData);mChart.highlightValues(null);mChart.invalidate(); //将图表重绘以显示设置的属性和数据
3.布局代码:
4.参考案例:
https://github.com/PhilJay/MPAndroidChart
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
