文档章节

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);
        }
    }

}

© 著作权归作者所有

共有 人打赏支持
席道坤
粉丝 13
博文 226
码字总数 183155
作品 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

没有更多内容

加载失败,请刷新页面

加载更多

redis 系列一 -- 简介及安装

1.简介 redis -- remote dictionary server 远程字典服务 使用 C 语言编写; 高性能的 key-value数据库; 内存数据库,支持数据持久化。 Redis 是一个开源(BSD许可)的,内存中的数据结构存...

imbiao
37分钟前
1
0
nginx log记录请求响应时间

有时为了方便分析接口性能等,需要记录请求的时长,通过修改nginx的日志格式可以做到,如 添加一个新的log_format log_format timed_combined '$remote_addr - $remote_user [$time_local] "...

swingcoder
今天
2
0
Spring MVC之RequestMappingHandlerMapping匹配

对于RequestMappingHandlerMapping,使用Spring的同学基本都不会陌生,该类的作用有两个: 通过request查找对应的HandlerMethod,即当前request具体是由Controller中的哪个方法进行处理; 查...

爱宝贝丶
今天
2
0
Java Web--增删改查之二界面后台java代码(转载参考)

/** *  *//** * @author Administrator * */package dao; import java.sql.*;public class DBConn {/** * 链接数据库 * @return */  ...

小橙子的曼曼
今天
4
0
Redis源码阅读笔记-对象及其类型和编码

总结之《Redis设计与实现》 对象 Redis中是使用对象来便是数据库中的键和值。 结构 // server.h...#define LRU_BITS 24...typedef struct redisObject { unsigned type:4; ...

Jian_Ming
今天
2
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部