MPAndroidChart之饼图PieChart基础使用

本文详细介绍了如何在Android应用中使用MPAndroidChart库创建饼状图。从引入依赖到XML布局,再到代码实现,包括数据绑定、动画效果、图例定制及事件监听,提供了丰富的配置选项和示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MPAndroidChart的GitHub地址:https://github.com/PhilJay/MPAndroidChart

一:引入依赖

//在需要使用的build.gradle——>dependencies下引入
api 'com.github.PhilJay:MPAndroidChart:v3.1.0-alpha'

二:xml中使用

 <com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pc_charts"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

三:在代码中使用,注解中是我查找资料加上个人理解的,如有解释不到位的地方,多多包涵(使用了dataBinding数据绑定):

 private ArrayList<PieEntry> entries = new ArrayList<PieEntry>();
  
  private void initView() {
        entries.clear();//清除数据
        //添加数据
        entries.add(new PieEntry(10, "人性的弱点"));
        entries.add(new PieEntry(12, "狼道"));
        entries.add(new PieEntry(17, "鬼谷子"));
        entries.add(new PieEntry(20, "YOUTH.度"));
        entries.add(new PieEntry(22, "週莫"));
        entries.add(new PieEntry(25, "墨菲定律"));

        mBinding.pcCharts.setUsePercentValues(true); //设置是否显示数据实体(百分比,true:以下属性才有意义)
        mBinding.pcCharts.getDescription().setEnabled(false);//设置pieChart图表的描述
        mBinding.pcCharts.setExtraOffsets(5, 5, 5, 5);//饼形图上下左右边距

        mBinding.pcCharts.setDragDecelerationFrictionCoef(0.95f);//设置pieChart图表转动阻力摩擦系数[0,1]

//        mBinding.pcCharts.setCenterTextTypeface(mTfLight);//设置所有DataSet内数据实体(百分比)的文本字体样式
        mBinding.pcCharts.setCenterText("饼状图");//设置PieChart内部圆文字的内容

        mBinding.pcCharts.setDrawHoleEnabled(true);//是否显示PieChart内部圆环(true:下面属性才有意义)
        mBinding.pcCharts.setHoleColor(Color.WHITE);//设置PieChart内部圆的颜色

        mBinding.pcCharts.setTransparentCircleColor(Color.WHITE);//设置PieChart内部透明圆与内部圆间距(31f-28f)填充颜色
        mBinding.pcCharts.setTransparentCircleAlpha(0);//设置PieChart内部透明圆与内部圆间距(31f-28f)透明度[0~255]数值越小越透明
        mBinding.pcCharts.setHoleRadius(0f);//设置PieChart内部圆的半径(这里设置0f,即不要内部圆)
        mBinding.pcCharts.setTransparentCircleRadius(31f);//设置PieChart内部透明圆的半径(这里设置31.0f)

        mBinding.pcCharts.setDrawCenterText(true);//是否绘制PieChart内部中心文本(true:下面属性才有意义)

        mBinding.pcCharts.setRotationAngle(0);//设置pieChart图表起始角度

        mBinding.pcCharts.setRotationEnabled(true);//设置pieChart图表是否可以手动旋转
        mBinding.pcCharts.setHighlightPerTapEnabled(true);//设置piecahrt图表点击Item高亮是否可用

        mBinding.pcCharts.animateY(1400, Easing.EaseInOutQuad);// 设置pieChart图表展示动画效果
//         mBinding.pcCharts.spin(2000, 0, 360);//旋转

        // 获取pieCahrt图列(图列的位置、是水平还是垂直显示)
        Legend l = mBinding.pcCharts.getLegend();
        l.setForm(Legend.LegendForm.LINE);//线性
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//上边
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.RIGHT);//右边(跟着TOP既是右上角,根据自己需求设置左上角、左下角……)
        l.setOrientation(Legend.LegendOrientation.VERTICAL);
        l.setDrawInside(false);
        l.setXEntrySpace(7f); //设置图例实体之间延X轴的间距(setOrientation = HORIZONTAL有效)
        l.setYEntrySpace(0f); //设置图例实体之间延Y轴的间距(setOrientation = VERTICAL 有效)
        l.setYOffset(0f);//设置比例块Y轴偏移量


        mBinding.pcCharts.setEntryLabelColor(Color.WHITE);//设置pieChart图表文本字体颜色
//        mBinding.pcCharts.setEntryLabelTypeface(mTfRegular);//设置pieChart图表文本字体样式
        mBinding.pcCharts.setEntryLabelTextSize(12f);//设置pieChart图表文本字体大小

        PieDataSet dataSet = new PieDataSet(entries, "数据说明");//右上角,依次排列

        dataSet.setDrawIcons(false);

        dataSet.setSliceSpace(0f);//设置饼状Item之间的间隙
        dataSet.setIconsOffset(new MPPointF(0, 40));

        dataSet.setSelectionShift(5f);//设置饼状Item被选中时变化的距离(为0f时,选中的不会弹起来)

        ArrayList<Integer> colors = new ArrayList<Integer>();

        for (int c : ColorTemplate.VORDIPLOM_COLORS) {
            colors.add(c);
        }

        for (int c : ColorTemplate.JOYFUL_COLORS) {
            colors.add(c);
        }

        for (int c : ColorTemplate.COLORFUL_COLORS) {
            colors.add(c);
        }

        for (int c : ColorTemplate.LIBERTY_COLORS) {
            colors.add(c);
        }

        for (int c : ColorTemplate.PASTEL_COLORS) {
            colors.add(c);
        }

        colors.add(ColorTemplate.getHoloBlue());

        dataSet.setColors(colors);

        PieData data = new PieData(dataSet);//设置饼图里面的百分比(eg: 20.8%)
        data.setDrawValues(true);            //设置是否显示数据实体(百分比,true:以下属性才有意义)
        data.setValueTextColor(Color.WHITE);  //设置所有DataSet内数据实体(百分比)的文本颜色
        data.setValueTextSize(11f);          //设置所有DataSet内数据实体(百分比)的文本字体大小
//        data.setValueTypeface(mTfLight);     //设置所有DataSet内数据实体(百分比)的文本字体样式
        data.setValueFormatter(new PercentFormatter());//设置所有DataSet内数据实体(百分比)的文本字体格式

        mBinding.pcCharts.setData(data);// //为图表添加 数据

        mBinding.pcCharts.highlightValues(null);//设置高亮显示
        mBinding.pcCharts.setDrawEntryLabels(true);// 设置pieChart是否只显示饼图上百分比不显示文字
        mBinding.pcCharts.invalidate();//将图表重绘以显示设置的属性和数据

        mBinding.pcCharts.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {//点击事件
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                // e.getX()方法得到x数据
                PieEntry pieEntry = (PieEntry) e;
                Toast.makeText(ChartsActivity.this,"->value:" + pieEntry.getValue()+ "->lable:" + pieEntry.getLabel(),Toast.LENGTH_LONG).show();
            }

            @Override
            public void onNothingSelected() {

            }
        });

    }

四:效果图:

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值