android图表类
android图表类
席道坤 发表于7个月前
android图表类
  • 发表于 7个月前
  • 阅读 83
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

一 开源库MPChart

github开源地址:https://github.com/PhilJay/MPAndroidChart

gradle配置:

项目级build.gradle配置:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
    }
}

工程级build.gradle配置:

compile 'com.github.PhilJay:MPAndroidChart:v3.0.2'

 

二 自定义饼形图

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.base.library.R;

/**
 * @author:cary
 * @date:2017/5/2
 * @descript:饼形图
 */
public class PieChartWidget extends View {

    private Context mContext;

    private float mParentingPre;//亲子所占百分比
    private float mRestaurantPre;//餐饮
    private float mRetailPre;//零售
    private float mEntertainmentPre;//娱乐
    private float mSportsPre;//运动

    private int mParentingColor;//绘制颜色
    private int mRestaurantColor;
    private int mRetailColor;
    private int mEntertainmentColor;
    private int mSportsColor;

    private Paint mArcPaint;//圆弧画笔
    private Paint mLinePaint;//画折线画笔

    private float mCenterX;
    private float mCenterY;
    private float mRadius;//半径
    private float mStorkeWidth;
    private float mLineWidth;

    public PieChartWidget(Context context) {
        super(context);
    }

    public PieChartWidget(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public PieChartWidget(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, @Nullable AttributeSet attrs){
        this.mContext = context;

        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.PieChart);
        mParentingPre = mTypedArray.getFloat(R.styleable.PieChart_parentingPre ,0)/100f * 360f;
        mRestaurantPre = mTypedArray.getFloat(R.styleable.PieChart_restaurantPre ,0)/100f * 360f;
        mRetailPre = mTypedArray.getFloat(R.styleable.PieChart_retailPre ,0)/100f * 360f;
        mEntertainmentPre = mTypedArray.getFloat(R.styleable.PieChart_entertainmentPre ,0)/100f *360f;
        mSportsPre = mTypedArray.getFloat(R.styleable.PieChart_sportsPre ,0)/100f * 360f;

        mParentingColor = mTypedArray.getColor(R.styleable.PieChart_parentingColor, Color.WHITE );
        mRestaurantColor = mTypedArray.getColor(R.styleable.PieChart_restaurantColor, Color.WHITE );
        mRetailColor = mTypedArray.getColor(R.styleable.PieChart_retailColor, Color.WHITE );
        mEntertainmentColor = mTypedArray.getColor(R.styleable.PieChart_entertainmentColor, Color.WHITE );
        mSportsColor = mTypedArray.getColor(R.styleable.PieChart_sportsColor, Color.WHITE );

        mRadius = mTypedArray.getDimension(R.styleable.PieChart_radius ,0);
        mStorkeWidth = mTypedArray.getDimension(R.styleable.PieChart_arcStrokeWidth ,0);
        mLineWidth = mTypedArray.getDimension(R.styleable.PieChart_lineStrokeWidth ,0);

        mArcPaint = new Paint();
        mArcPaint.setAntiAlias(true);
        mArcPaint.setDither(true);
        mArcPaint.setStyle(Paint.Style.STROKE);
        mArcPaint.setStrokeWidth(mStorkeWidth);

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setDither(true);
        mLinePaint.setStyle(Paint.Style.STROKE);
        mLinePaint.setStrokeWidth(mLineWidth);

        mTypedArray.recycle();
        postInvalidate();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec));
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mCenterX = w/2;
        mCenterY = h/2;
    }

    /**
     * 设置亲子百分比
     * @param present
     */
    public void setParentingPre(float present){
        this.mParentingPre = 360f * (present/100f);
    }

    public void setRestaurantPre(float present){
        this.mRestaurantPre = 360f * (present/100f);
    }

    public void setRetailPre(float present){
        this.mRetailPre = 360f * (present/100f);
    }

    public void setEntertainmentPre(float present){
        this.mEntertainmentPre = 360f * (present/100f);
    }

    public void setSportsPre(float present){
        this.mSportsPre = 360f * (present/100f);
    }

    /**
     * 设置宽度
     * @param width
     */
    public void setStrokeWidth(int width){
        mArcPaint.setStrokeWidth(width);
    }

    /**
     * 半径
     * @param radius
     */
    public void setRadius(float radius){
        mRadius = radius;
    }


    /**
     * 设置亲子色值
     * @param color
     */
    public void setParentingColor(int color){
        this.mParentingColor = color;
    }

    public void setRestaurantColor(int color){
        this.mRestaurantColor = color;
    }

    public void setRetailColor(int color){
        this.mRetailColor = color;
    }

    public void setEntertainmentColor(int color){
        this.mEntertainmentColor = color;
    }

    public void setSportsColor(int color){
        this.mSportsColor = color;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        RectF mRectF = new RectF(mCenterX - mRadius, mCenterY - mRadius, mCenterX + mRadius, mCenterY + mRadius);

        //绘制亲子
        mArcPaint.setColor(mParentingColor);
        mLinePaint.setColor(mParentingColor);
        canvas.drawArc(mRectF, -90f, -mParentingPre, false, mArcPaint);
        drawLines(canvas, -60f-mParentingPre, mLinePaint);

        //绘制餐饮
        mArcPaint.setColor(mRestaurantColor);
        mLinePaint.setColor(mRestaurantColor);
        canvas.drawArc(mRectF, -90f-mParentingPre, -mRestaurantPre, false, mArcPaint);
        drawLines(canvas, -60f-mParentingPre-mRestaurantPre, mLinePaint);

        //绘制零售
        mArcPaint.setColor(mRetailColor);
        mLinePaint.setColor(mRetailColor);
        canvas.drawArc(mRectF, -90f-mParentingPre-mRestaurantPre, -mRetailPre, false, mArcPaint);
        drawLines(canvas, -60f-mParentingPre-mRestaurantPre-mRetailPre, mLinePaint);

        //娱乐
        mArcPaint.setColor(mEntertainmentColor);
        mLinePaint.setColor(mEntertainmentColor);
        canvas.drawArc(mRectF, -90f-mParentingPre-mRestaurantPre-mRetailPre, -mEntertainmentPre, false, mArcPaint);
        drawLines(canvas, -60f-mParentingPre-mRestaurantPre-mRetailPre-mEntertainmentPre, mLinePaint);

        //运动
        mArcPaint.setColor(mSportsColor);
        mLinePaint.setColor(mSportsColor);
        canvas.drawArc(mRectF, -90f-mParentingPre-mRestaurantPre-mRetailPre-mEntertainmentPre, -mSportsPre, false, mArcPaint);
        drawLines(canvas, -60f-mParentingPre-mRestaurantPre-mRetailPre-mEntertainmentPre-mSportsPre, mLinePaint);

    }

    private void drawLines(Canvas canvas, float angle, Paint paint){

        float absAngle = Math.abs(angle + 90);//取绝对值

        float x1 = (float) (mCenterX-(mRadius+mArcPaint.getStrokeWidth()/2)*Math.sin(Math.toRadians(absAngle)));
        float y1 =(float) (mCenterY- (mRadius+mArcPaint.getStrokeWidth()/2)*Math.cos(Math.toRadians(absAngle)));
        float x2 = (float) (x1 - 80*Math.sin(Math.toRadians(absAngle)));
        float y2 = (float) (y1 - 80*Math.cos(Math.toRadians(absAngle)));

        if(absAngle < 90){
            canvas.drawLine(x1, y1, x2, y2, paint);
            canvas.drawLine(x2, y2, x2-80, y2, paint);//x值增加
        } else if(absAngle == 90){
            canvas.drawLine(x1, y1, x1-80, y1, paint);
        } else if(absAngle>90 && absAngle<180){
            canvas.drawLine(x1, y1, x2, y2, paint);
            canvas.drawLine(x2, y2, x2-80, y2, paint);
        } else if(absAngle == 180){
            canvas.drawLine(x1, y1, x1, y1+80, paint);
        } else if(absAngle>180 && absAngle<270){
            canvas.drawLine(x1, y1, x2, y2, paint);
            canvas.drawLine(x2, y2, x2+80, y2, paint);//x值增加
        } else if(absAngle == 270){
            canvas.drawLine(x1, y1, x1+80, y1, paint);
        } else if(absAngle>270 && absAngle<360){
            canvas.drawLine(x1, y1, x2, y2, paint);
            canvas.drawLine(x2, y2, x2+80, y2, paint);//x值增加
        } else if(absAngle == 360){
            canvas.drawLine(x1, y1, x1, y1-80, paint);
        }
    }

}
共有 人打赏支持
粉丝 12
博文 206
码字总数 164046
×
席道坤
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: