文档章节

android图表类

席道坤
 席道坤
发布于 2017/05/08 19:05
字数 754
阅读 87
收藏 0

一 开源库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);
        }
    }

}

© 著作权归作者所有

共有 人打赏支持
下一篇: unbuntu搭建ftp
席道坤
粉丝 13
博文 227
码字总数 183547
作品 0
徐汇
程序员
私信 提问
android电子书App、自定义图表、仿腾讯漫画App、仿淘宝优惠券、3D选择容器等源码

Android精选源码 仿支付宝记账本功能,饼状图;数字键盘(http://www.apkbus.com/thread-602144-1-1.html) android一款功能完善的电子书应用源码(http://www.apkbus.com/thread-602145-1-1.ht...

逆鳞龙
08/07
0
0
Android组件及UI框架大全

Android 是目前最流行的移动操作系统(还需要加之一吗?)。 随着新版本的不断发布, Android的功能也日益强大, 涌现了很多流行的应用程序, 也催生了一大批的优秀的组件。 本文试图将目前流行...

千山万水
2015/07/13
0
4
Android组件及UI框架大全

Android 是目前最流行的移动操作系统(还需要加之一吗?)。 随着新版本的不断发布, Android的功能也日益强大, 涌现了很多流行的应用程序, 也催生了一大批的优秀的组件。 本文试图将目前流行...

千山万水
2015/07/13
0
0
支持常用的图表、仪表和地图类型的图表控件TeeChart Java for Android

TeeChart Java for Android控件是针对Google的Android操作系统设计的图表控件,支持常用的图表、仪表和地图类型 具体功能: 默认支持Android v2.1以及更高的多点触摸版 提供了50多种图表样式...

baobiaokongjian
2014/06/21
0
1
Android Bluetooth opp package 学习笔记

一、背景及相关知识学习 1、Android Bluetooth SDK 首先,要操作蓝牙,先要在AndroidManifest.xml里加入权限 <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <use......

sflfqx
2013/03/12
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Mybatis 中$与#的区别,预防SQL注入

一直没注意Mybatis 中$与#的区别,当然也是更习惯使用#,没想到避免了SQL注入,但是由于要处理项目中安全渗透的问题,不可避免的又遇到了这个问题,特此记录一下。 首先是共同点: 在mybatis...

大雁南飞了
9分钟前
0
0
Cydia的基石:MobileSubstrate

在MAC与IOS平台上,动态库的后缀一般是dylid,而加载这些动态库的程序叫做dynamic linker(dyld)。这个程序有很多的环境变量来设置程序的一些行为,最为常用的一个环境变量叫做"DYLD_INSERT_...

HeroHY
12分钟前
0
0
Spring Clould负载均衡重要组件:Ribbon中重要类的用法

Ribbon是Spring Cloud Netflix全家桶中负责负载均衡的组件,它是一组类库的集合。通过Ribbon,程序员能在不涉及到具体实现细节的基础上“透明”地用到负载均衡,而不必在项目里过多地编写实现...

Ala6
21分钟前
0
0
让 linux 删除能够进入回收站

可以参考这个贴子 https://blog.csdn.net/F8qG7f9YD02Pe/article/details/79543316 从那个git地址 把saferm.sh下载下来 把saferm.sh复制到 /usr/bin 目录下 在用~/目下 的.bashrc 下加一句这...

shzwork
31分钟前
0
0
Qt那些事0.0.9

关于QThread,无F*k说的。文档说的差不多,更多的是看到很多人提到Qt开发者之一的“你TM的做错了(You're doing it wrong...)”,这位大哥2010年写的博客,下面评论很多,但主要还是集中在2...

Ev4n
今天
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部