MPchart 饼状图的简单封装与使用。

Android中实现常规的图标,就绕不过MPAndroidChart这个强大的开源库。

秉着不重复造轮子(偷懒+水平有限)的原则,在项目中引入了这个强大的图表库,并对其中饼状图的实现做了简单的封装。

package com.bjttsf.sale.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.SpannableString;

import com.bjttsf.sale.R;
import com.bjttsf.sale.bean.LegendBean;
import com.blankj.utilcode.util.ConvertUtils;
import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.PieData;
import com.github.mikephil.charting.data.PieDataSet;
import com.github.mikephil.charting.data.PieEntry;
import com.github.mikephil.charting.formatter.PercentFormatter;
import com.github.mikephil.charting.utils.MPPointF;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
public class MpChartFacts {
    //必填属性
    private Context mContext;
    private PieChart mPieChart;
    private List<Float> mFloatList;
    private CharSequence text;
    //可选属性

    //是否空心
    private boolean holeEnabled = true;
    //内圆百分比
    private float inRadius = 90f;
    //外圆百分比
    private float outRadius = 100f;
    //饼状图间隙
    private float sliceSpace = 0f;
    //是否显示方块标签
    private boolean isLegend = false;
    //是否在圆环上显示信息
    private boolean isText = true;
    //方块标签的位置设置信息
    private LegendBean mLegendBean = null;
    //顺时针圆环颜色
    List<Integer> colorList;
    //内部空心颜色
    private int holeColor;
    //内部空心字体颜色
    private int holeTextColor;
    //自定义饼状图每一项的数据
    private ArrayList<PieEntry> mPieEntries;

    public static class Builder {
        //必填属性
        private Context mContext;
        private PieChart mPieChart;
        private List<Float> mFloatList;
        private CharSequence text;
        //可选属性

        public Builder(Context context, PieChart pieChart, List<Float> floatList, CharSequence text) {
            mContext = context;
            mPieChart = pieChart;
            mFloatList = floatList;
            this.text = text;
            colorList.clear();
            colorList.add(mContext.getResources().getColor(R.color.ye_FFD449));
            colorList.add(mContext.getResources().getColor(R.color.green_50D78A));
            colorList.add(mContext.getResources().getColor(R.color.blue_24A3FF));
            holeColor = mContext.getResources().getColor(R.color.white);
            holeTextColor = mContext.getResources().getColor(R.color.black_2B2B2B);
        }

        //是否空心
        private boolean holeEnabled = true;
        //内圆百分比
        private float inRadius = 90f;
        //外圆百分比
        private float outRadius = 100f;
        //饼状图间隙
        private float sliceSpace = 0f;
        //是否显示方块标签
        private boolean isLegend = false;
        //是否在圆环上显示信息
        private boolean isText = true;
        //方块标签的位置设置信息
        private LegendBean mLegendBean = null;
        //顺时针圆环颜色
        private List<Integer> colorList = new ArrayList<>();
        //内部空心颜色
        private int holeColor;
        //内部空心字体颜色
        private int holeTextColor;
        //自定义饼状图每一项的数据
        private ArrayList<PieEntry> mPieEntries;

        public Builder pieEntries(ArrayList<PieEntry> pieEntries) {
            this.mPieEntries = pieEntries;
            return this;
        }

        public Builder holeTextColor(int holeTextColor) {
            this.holeTextColor = holeTextColor;
            return this;
        }

        public Builder holeColor(int holeColor) {
            this.holeColor = holeColor;
            return this;
        }

        public Builder colorList(List<Integer> colorList) {
            this.colorList = colorList;
            return this;
        }

        public Builder holeEnabled(boolean holeEnabled) {
            this.holeEnabled = holeEnabled;
            return this;
        }

        public Builder inRadius(float inRadius) {
            this.inRadius = inRadius;
            return this;
        }

        public Builder outRadius(float outRadius) {
            this.outRadius = outRadius;
            return this;
        }

        public Builder sliceSpace(float sliceSpace) {
            this.sliceSpace = sliceSpace;
            return this;

        }

        public Builder isLegend(boolean isLegend) {
            this.isLegend = isLegend;
            return this;
        }

        public Builder mLegendBean(LegendBean mLegendBean) {
            this.mLegendBean = mLegendBean;
            return this;
        }

        public Builder isText(Boolean isText) {
            this.isText = isText;
            return this;
        }

        public MpChartFacts build() {
            return new MpChartFacts(this);
        }

    }

    private MpChartFacts(Builder builder) {
        mContext = builder.mContext;
        mPieChart = builder.mPieChart;
        mFloatList = builder.mFloatList;
        text = builder.text;
        holeEnabled = builder.holeEnabled;
        inRadius = builder.inRadius;
        outRadius = builder.outRadius;
        sliceSpace = builder.sliceSpace;
        isLegend = builder.isLegend;
        mLegendBean = builder.mLegendBean;
        isText = builder.isText;
        colorList = builder.colorList;
        holeColor = builder.holeColor;
        holeTextColor = builder.holeTextColor;
        mPieEntries = builder.mPieEntries;

    }


    public void setPieChart() {
        //如果启用此选项,则图表中的值将以百分比形式绘制,而不是以原始值绘制
        mPieChart.setUsePercentValues(true);
        //如果这个组件应该启用(应该被绘制)FALSE如果没有。如果禁用,此组件的任何内容将被绘制默认
        mPieChart.getDescription().setEnabled(false);
        //图表外围偏移量设定
        // mPieChart.setExtraOffsets(5, 5, 5, 5);
        //较高的值表明速度会缓慢下降 例如如果它设置为0,它会立即停止。1是一个无效的值,并将自动转换为0.999f。
        mPieChart.setDragDecelerationFrictionCoef(1f);
        //mPieChart.setCenterTextTypeface(tfLight);
        //是否空心
        mPieChart.setDrawHoleEnabled(holeEnabled);
        if (holeEnabled) {
            //空心内部颜色
            mPieChart.setHoleColor(holeColor);
            //完全透明
            mPieChart.setTransparentCircleAlpha(0);
            //设置内圆占总体的百分比,默认为50%
            mPieChart.setHoleRadius(inRadius);
            //设置外圆占总体的百分比,默认为55%
            mPieChart.setTransparentCircleRadius(outRadius);
        }
        //是否绘制中心文字
        mPieChart.setDrawCenterText(true);
        //中间字体
        mPieChart.setCenterText(generateCenterSpannableText(text));
        //中部字体偏移量
        mPieChart.setCenterTextOffset(0, 0);
        mPieChart.setCenterTextSizePixels(ConvertUtils.sp2px(14));
        //中间字体颜色
        mPieChart.setCenterTextColor(holeTextColor);
        //设置绘制的起始角度  默认顶部顺时针绘制(270f)
        //   mPieChart.setRotationAngle(0);
        //是否有触摸反馈
        mPieChart.setRotationEnabled(false);
        //点击是否展示被点击部分
        mPieChart.setHighlightPerTapEnabled(true);
        // mPieChart.setUnit(" €");
        // mPieChart.setDrawUnitsInmPieChart(true);
        // add a selection listener
        //    mPieChart.setOnChartValueSelectedListener(this);
        //设置动画效果
        // mPieChart.animateY(1400, Easing.EaseInQuad);
        // mPieChart.spin(2000, 0, 360);
        //设置
        Legend l = mPieChart.getLegend();
        if (isLegend) {
            l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
            l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);
            l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
            if (mLegendBean != null) {
                l.setDrawInside(mLegendBean.isInside());
                l.setXEntrySpace(mLegendBean.getXEntrySpace());
                l.setYEntrySpace(mLegendBean.getYEntrySpace());
                l.setYOffset(mLegendBean.getYOffset());
            }
            //是否显示方块标签
            l.setEnabled(true);
        } else {
            l.setEnabled(false);
        }
        // entry label styling
        //显示自定义标签内容
        mPieChart.setDrawEntryLabels(isText);
        mPieChart.setEntryLabelColor(Color.WHITE);
        //设置自定义标签字体大小
        mPieChart.setEntryLabelTextSize(14);
        ArrayList<PieEntry> entries = new ArrayList<>();
        if (mPieEntries == null) {
            PieEntry pieEntry1 = new PieEntry(mFloatList.get(0), "已完成");
            PieEntry pieEntry2 = new PieEntry(mFloatList.get(1), "未完成");
            PieEntry pieEntry3 = null;
            if (mFloatList.size() > 2) {
                pieEntry3 = new PieEntry(mFloatList.get(2), "未完成");
            }
            entries.add(pieEntry1);
            entries.add(pieEntry2);
            if (pieEntry3 != null) {
                entries.add(pieEntry3);
            }
        } else {
            Float num = 0f;
            for (int i = 0; i < mFloatList.size(); i++) {
                num = num + mFloatList.get(i);
            }
            if (num == 0f) {
                //无数据的默认展示
                PieEntry pieEntry = new PieEntry(1f, "无数据");
                colorList.set(0,mContext.getResources().getColor(R.color.grey_999999));
                entries.add(pieEntry);
            } else {
                entries = mPieEntries;
            }

        }
        //"" 为方块标签的描述
        PieDataSet dataSet = new PieDataSet(entries, "");
        设置Y轴,这个数据集应该被绘制(左或右)。默认值:左
        dataSet.setAxisDependency(YAxis.AxisDependency.LEFT);
        //是否有空白间隙
        dataSet.setAutomaticallyDisableSliceSpacing(false);
        //是否在圆环上显示百分比数值
        dataSet.setDrawValues(isText);
        dataSet.setColors(colorList);
        dataSet.setDrawIcons(false);
        //设置百分比之间 间隙大小
        dataSet.setSliceSpace(sliceSpace);
        dataSet.setIconsOffset(new MPPointF(0, 40));
        //设置被点击后  伸出去的距离 (需要开启点击事件)
        dataSet.setSelectionShift(3f);
        PieData data = new PieData(dataSet);
        data.setValueFormatter(new PercentFormatter(mPieChart));
        //设置百分比的大小
        data.setValueTextSize(13);
        data.setValueTextColor(Color.WHITE);
     /*   Typeface tfLight = Typeface.create("6666",Typeface.NORMAL);
        data.setValueTypeface(tfLight);*/
        mPieChart.setData(data);
        // undo all highlights
        mPieChart.highlightValues(null);
        mPieChart.invalidate();

    }

    //设置空心圆内部字体内容
    private SpannableString generateCenterSpannableText(CharSequence text) {
        SpannableString s = new SpannableString(text);
        //   s.setSpan(new RelativeSizeSpan(2f), 3, s.length() - 5, 0);
        /*s.setSpan(new StyleSpan(Typeface.NORMAL), 2, 4, 0);
        s.setSpan(new ForegroundColorSpan(Color.GRAY), 4, s.length() - 4, 0);
        s.setSpan(new RelativeSizeSpan(.8f), 2, 4, 0);
        s.setSpan(new StyleSpan(Typeface.ITALIC), s.length() - 4, s.length(), 0);
        s.setSpan(new ForegroundColorSpan(ColorTemplate.getHoloBlue()), s.length() - 4, s.length(), 0);
      */
        return s;
    }
}

使用了工厂模式来处理各种可配置的属性。实现颜色和对应数值的灵活展示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值