关闭

Android自定义图表:ChartView

标签: 自定义ViewCharViewCharAndroidPaint
1715人阅读 评论(1) 收藏 举报
分类:

效果图
Design images :
这里写图片描述

View Code:

/**
 * Created by JackWaiting on 2016/6/24.
 */
public class CharView extends View {

    private  Paint mRowLinePaint,mColLinePaint;  //横线、竖线的画笔
    private Paint redPaint,greenPaint,yellowPaint;
    private Paint mColTextPaint;  //横坐标画笔
    private int mTopPadding, mLeftPadding, mRightPadding, mBottomPadding;  //图标距离上下左右的边距
    private int mFontHeight, mFontTopMargin;  //字体的高度与字体上方的边距
    private int mRowNumber = 7, mColNumber = 30;  //行与列
    private int[] mRowYs,mColYs,mColTextYs;  //装动态行列坐标的数组
    private int mRowHeight, mColWidth;  //行高与列高
    private int mRowLineWidth , mColLineWidth ; //行线与列线的宽度
    private int mRowStartX, mRowEndX,mColStartY,mColEndY;
    private int mColTextWidth,mColTextHeight;
    private float[] lifeTimeData = new float[mColNumber+1];  //每月最多31个值
    private int mLifeTimeWidth;
    private float lifeTimeHeight;  //由于高度的精确度比较高,建议使用float
    private float mLifeTimeStartX[],mLifeTimeEndX[],mLifeTimeStartY[],mLifeTimeEndY[];
    private float mMaxLifeTimeData = 8f,mOneRowTip = 7.45f,mTwoRowTip = 6.34f,mThreeRowTip = 6.18f,mFourRowTip = 5.97f;
    private float mRowTipData[] = new float[]{0f,7.45f,0f,0f,6.34f,6.18f,5.97f,0f}; //不显示则用0f表示
    private String[] chars = new String[]{"0","5","10","15","20","25","30"};
    private  Path path;
    private int mCircleRadius = 20;
    private PathEffect effects;
    private int mRowTipWidth =150,mRowTipHeight = 60;

    public CharView(Context context) {
        super(context);
        init(context,null,0);
    }

    public CharView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,null,0);
    }

    public CharView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context,attrs,defStyleAttr);
    }

    public void getLifeTimeData(float [] data){
        this.lifeTimeData = data;
    }

    private void init(Context context,AttributeSet attrs,int defStyle) {
        mTopPadding =  PixelUtil.dp2px(50, getContext());
        mRowLineWidth = PixelUtil.dp2px(1, getContext());
        mColLineWidth = PixelUtil.dp2px(1, getContext());
        mLeftPadding = PixelUtil.dp2px(20, getContext());
        mRightPadding = PixelUtil.dp2px(20, getContext());
        mBottomPadding = PixelUtil.dp2px(55, getContext());
        mFontHeight = PixelUtil.dp2px(10, getContext());
        mFontTopMargin = PixelUtil.dp2px(15, getContext());
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.MyCharView);
        effects= new DashPathEffect(new float[]{5,5,5,5},10);
        path = new Path();
        mRowLinePaint = new Paint();
        mRowLinePaint.setColor(a.getColor(R.styleable.MyCharView_cv_row,getResources().getColor(R.color.row)));
        mRowLinePaint.setStrokeWidth(mRowLineWidth);
        mColLinePaint = new Paint();
        mColLinePaint.setColor(a.getColor(R.styleable.MyCharView_cv_col,getResources().getColor(R.color.col)));
        mColLinePaint.setStrokeWidth(mColLineWidth);
        mColLinePaint.setStyle(Paint.Style. STROKE);
        mColLinePaint.setAntiAlias( true);
        redPaint = new Paint();
        redPaint.setColor(a.getColor(R.styleable.MyCharView_cv_red,getResources().getColor(R.color.red)));
        redPaint.setStrokeWidth(PixelUtil.dp2px(2, getContext()));
        greenPaint = new Paint();
        greenPaint.setColor(a.getColor(R.styleable.MyCharView_cv_green,getResources().getColor(R.color.green)));
        greenPaint.setStrokeWidth(PixelUtil.dp2px(2, getContext()));
        yellowPaint = new Paint();
        yellowPaint.setColor(a.getColor(R.styleable.MyCharView_cv_row,getResources().getColor(R.color.yellow)));
        yellowPaint.setStrokeWidth(PixelUtil.dp2px(2, getContext()));

        mColTextPaint = new Paint();
        mColTextPaint.setColor(a.getColor(R.styleable.MyCharView_cv_row,Color.WHITE));
        mColTextPaint.setTextSize(50);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i= 0;i<mColYs.length;i++){
            if(i%5==0){
                canvas.drawLine(mColYs[i], mColStartY, mColYs[i],mColEndY, mRowLinePaint);
                canvas.drawText(chars[i/5]+"",mColTextYs[i/5]-(mColWidth/2), mColTextHeight,mColTextPaint);
            }
            else{
                //canvas.drawLine(mColYs[i], mColStartY, mColYs[i],mColEndY, mColLinePaint);
                path.moveTo(mColYs[i], mColStartY);
                path.lineTo(mColYs[i],mColEndY);
                mColLinePaint.setPathEffect(effects);
                canvas.drawPath(path, mColLinePaint);
            }
        }

        for (int i= 0;i<mRowYs.length;i++){
            canvas.drawLine(mRowStartX, mRowYs[i], mRowEndX, mRowYs[i], mRowLinePaint);
        }

        for (int i= 0;i<lifeTimeData.length-1;i++){
            if(lifeTimeData[i] >mOneRowTip){
                canvas.drawLine(mLifeTimeStartX[i], mLifeTimeStartY[i], mLifeTimeEndX[i], mLifeTimeEndY[i], redPaint);
                drawCircleView(i,redPaint,canvas);
            }
            else if(lifeTimeData[i] >mTwoRowTip && lifeTimeData[i] <= mOneRowTip){
                canvas.drawLine(mLifeTimeStartX[i], mLifeTimeStartY[i], mLifeTimeEndX[i], mLifeTimeEndY[i], greenPaint);
                drawCircleView(i,greenPaint,canvas);
            }
            else if(lifeTimeData[i]  >mThreeRowTip && lifeTimeData[i] < mTwoRowTip){
                canvas.drawLine(mLifeTimeStartX[i], mLifeTimeStartY[i], mLifeTimeEndX[i], mLifeTimeEndY[i], greenPaint);
                drawCircleView(i,greenPaint,canvas);
            }
            else if(lifeTimeData[i]  >mFourRowTip && lifeTimeData[i] <= mThreeRowTip){
                canvas.drawLine(mLifeTimeStartX[i], mLifeTimeStartY[i], mLifeTimeEndX[i], mLifeTimeEndY[i], yellowPaint);
                drawCircleView(i,yellowPaint,canvas);
            }
            else if(lifeTimeData[i]  >=0 && lifeTimeData[i] <= mFourRowTip){
                canvas.drawLine(mLifeTimeStartX[i], mLifeTimeStartY[i], mLifeTimeEndX[i], mLifeTimeEndY[i], redPaint);
                drawCircleView(i,redPaint,canvas);
            }
            else{
                canvas.drawLine(mLifeTimeStartX[i], mLifeTimeStartY[i], mLifeTimeEndX[i], mLifeTimeEndY[i], redPaint);
            }
        }
        //最后一点无法循环到,手动赋值
        canvas.drawCircle(mLifeTimeStartX[30],mLifeTimeStartY[30], mCircleRadius,redPaint);
        for(int i=0;i< mRowYs.length;i++){

            if(i==0 ||i==2 ||i == 3 ||i == 7){
                continue;
            }
            else{
                canvas.drawRect(0, mRowYs[i]-mRowTipHeight/2,mRowTipWidth, mRowYs[i]+mRowTipHeight/2, mRowLinePaint);  // 矩形
                canvas.drawText(mRowTipData[i]+"",20, mRowYs[i]+20,mColTextPaint);  //给每个矩形进行参数赋值
            }

        }
    }


    //每隔5个点画圆
    private void  drawCircleView(int i,Paint paint,Canvas canvas){
        if(i%5==0){
            canvas.drawCircle(mLifeTimeStartX[i],mLifeTimeStartY[i], mCircleRadius,paint);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        int viewWidth = w - mLeftPadding - mRightPadding;
        int viewHeight = h - mTopPadding - mBottomPadding - mFontHeight - mFontTopMargin;
        mColTextHeight = h - mTopPadding  - mFontTopMargin;
        mRowStartX = mLeftPadding - mRowLineWidth/2;
        mRowEndX = w - mRightPadding  - (mRowLineWidth*mRowNumber)+mRowLineWidth/2;
        mColStartY = mTopPadding -mColLineWidth/2;
        mColEndY  =  h-mBottomPadding - mFontHeight - mFontTopMargin - (mRowNumber+1);
        mRowYs = new int[mRowNumber + 1];
        mColYs = new int[mColNumber + 1];
        mColTextYs = new int[chars.length];
        mRowHeight = (viewHeight-(mRowNumber+1)*mRowLineWidth)/mRowNumber;
        mColWidth  = (viewWidth - (mColNumber+1)*mColLineWidth)/mColNumber;
        for (int i = 0; i < mRowYs.length ; i++){
            mRowYs[i] = mTopPadding + i * (mRowHeight+mRowLineWidth) ;
        }
        for (int i = 0; i < mColYs.length ; i++){
            mColYs[i] = mLeftPadding + i * (mColWidth+mColLineWidth);
        }
        for (int i = 0; i < mColTextYs.length ; i++){
            mColTextYs[i] = mLeftPadding + i * (mColWidth+mColLineWidth)*5;
        }

        //具体画线
        mLifeTimeStartX = new float[mColNumber+1];
        mLifeTimeStartY = new float[mColNumber+1];
        mLifeTimeEndX = new float[mColNumber+1];
        mLifeTimeEndY = new float[mColNumber+1];
        for (int i=0;i<mLifeTimeStartX.length;i++){
            mLifeTimeStartX[i] =  mLeftPadding + i * (mColWidth+mColLineWidth);
        }
        for (int i=0;i<mLifeTimeEndX.length;i++){
            mLifeTimeEndX[i] =  mLeftPadding +mColWidth+mColLineWidth+ i * (mColWidth+mColLineWidth);
        }
        getLifeTime();
    }

    //获取每个点进行连接
    private void getLifeTime() {

        //计算Y轴的坐标
        for (int i = 0;i<lifeTimeData.length;i++){
            if(lifeTimeData[i] >=mOneRowTip){
                mLifeTimeStartY[i] = mTopPadding + (((mMaxLifeTimeData-mOneRowTip)-(lifeTimeData[i]-mOneRowTip))/(mMaxLifeTimeData-mOneRowTip))*(mRowHeight+mRowLineWidth);
            }
            else if(lifeTimeData[i] >mTwoRowTip && lifeTimeData[i] < mOneRowTip){
                mLifeTimeStartY[i] = mTopPadding + mRowHeight+mRowLineWidth+ (((mOneRowTip-mTwoRowTip)-(lifeTimeData[i]-mTwoRowTip))/(mOneRowTip-mTwoRowTip))*3*(mRowHeight+mRowLineWidth);
            }
            else if(lifeTimeData[i]  >=mThreeRowTip && lifeTimeData[i] < mTwoRowTip){
                mLifeTimeStartY[i] = mTopPadding + ((mRowNumber+1)/2)*(mRowHeight+mRowLineWidth) + (((mTwoRowTip-mThreeRowTip)-(lifeTimeData[i]-mThreeRowTip))/(mTwoRowTip-mThreeRowTip))*(mRowHeight+mRowLineWidth);
            }
            else if(lifeTimeData[i]  >mFourRowTip && lifeTimeData[i] < mThreeRowTip){
                mLifeTimeStartY[i] = mTopPadding + ((mRowNumber+1)-3)*(mRowHeight+mRowLineWidth)+ (((mThreeRowTip-mFourRowTip)-(lifeTimeData[i]-mFourRowTip))/(mThreeRowTip-mFourRowTip))*(mRowHeight+mRowLineWidth);
            }
            else if(lifeTimeData[i]  >=0 && lifeTimeData[i] <= mFourRowTip){
                mLifeTimeStartY[i] = mTopPadding + ((mRowNumber+1)-2)*(mRowHeight+mRowLineWidth)+ ((mFourRowTip-lifeTimeData[i])/mFourRowTip)*(mRowHeight+mRowLineWidth);
            }
            else{
                mLifeTimeStartY[i] = mTopPadding + mRowNumber * ( mRowHeight + mRowLineWidth );
            }
        }
        //把Y轴连在一起
        for (int i=1;i<mLifeTimeStartY.length;i++){
            mLifeTimeEndY[i-1] = mLifeTimeStartY[i];
        }
        //mLifeTimeEndY[mLifeTimeStartX.length-1 ] = mLifeTimeStartY[mLifeTimeStartX.length-1];
    }
}

——————————————————————————–

Update

之前的代码只是对图表进行绘制,并未做点击事件,正常的图表是在点击某项数据的时候,会展现出来数据,本次更新已解决此问题。
效果图:

这里写图片描述

update view code:

@Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCharts(canvas);
        if(mSpot >= 0 && mSpot <= 30){
             if(mLifeTimeStartY[mSpot] >=mTopPadding +mRowHeight+mRowLineWidth &&mLifeTimeStartY[mSpot] <=mTopPadding +5*(mRowHeight+mRowLineWidth)){
                drawTouchData(canvas,greenPaint);
            }
            else if(mLifeTimeStartY[mSpot] >=mTopPadding +5*(mRowHeight+mRowLineWidth) &&mLifeTimeStartY[mSpot] <=mTopPadding +6*(mRowHeight+mRowLineWidth)){
                drawTouchData(canvas,yellowPaint);
            }
            else{
                drawTouchData(canvas,redPaint);
             }
        }

    }

        @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if(ignoreTouch(event.getX(),event.getY())){
                    mSpot = getTouchSpot(event.getX());
                    Log.i("进来了","ACTION_DOWN已处理"+mSpot);
//                    setPressed(true);
                   invalidate();
                    return true;
                }
                else{
                    mSpot =-1;
                }
                break;
            case MotionEvent.ACTION_MOVE:
                break;
            case MotionEvent.ACTION_UP:
//                setPressed(false);
                break;
            case MotionEvent.ACTION_CANCEL:
//                setPressed(false);

                break;
        }

        return super.onTouchEvent(event);
    }


    //根据点击的范围获取到当前点击的点
    private int  getTouchSpot(float x) {
        int index = 0;
        for (int i = 0;i<mLifeTimeStartX.length;i++){
            if(x>mLifeTimeStartX[i]-mColWidth/2 && x<mLifeTimeEndX[i] -mColWidth/2){
                index = i;
            }
        }
        return index;
    }


    //判断当前点击的范围是否需要处理
    private boolean ignoreTouch(float x , float y) {
        boolean ignore = false;
        if((x>=(mLeftPadding-mColWidth/2) &&x <=(mRowEndX+mColWidth/2))&&(y>=mTopPadding-mColWidth/2 && y<=mColEndY+mColWidth/2) ){
            ignore = true;
        }
        return  ignore;
    }


    //点击Touch后给出每个点的具体数据
    private void drawTouchData(Canvas canvas,Paint paint) {
        canvas.drawRect(mLifeTimeStartX[mSpot]-mRowTipWidth/2, mLifeTimeStartY[mSpot]-100-mRowTipHeight/2,mLifeTimeStartX[mSpot]+mRowTipWidth/2, mLifeTimeStartY[mSpot]+mRowTipHeight/2-100, paint);  // 矩形
        canvas.drawText(lifeTimeData[mSpot]+"",mLifeTimeStartX[mSpot]-mRowTipWidth/2+20, mLifeTimeStartY[mSpot]-80,mColTextPaint);  //查看任意一点的信息
    }

最新代码已更新至Github,以下是下载链接:

Code download:
Github最新代码下载地址:https://github.com/JackWaiting/ChartView

4
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

Android自定义绘制图表

最近项目中需要数据统计进行图表展示,无奈设计效果太奇特无法使用一些图表控件来达到效果,只能自己动手绘制,咬牙坚持三天终于有点效果了, 记录分享以备后用
  • xiaoxuanfeng1986
  • xiaoxuanfeng1986
  • 2015-08-04 17:28
  • 1780

Android自定义控件(状态提示图表)

前面分析那么多系统源码了,也该暂停下来休息一下,趁昨晚闲着看见一个有意思的需求就操练一下分析源码后的实例演练----自定义控件。
  • yanbober
  • yanbober
  • 2015-06-03 11:28
  • 13012

Android图表 MPAndroidChart折线图

1.介绍MPAndroidChart GitHub地址 MPAndroidChart的强大之处就不在多说了,目前最新的版本是3.0.1,在新版本中很多方法都被弃用了,这个要注意一下,在网上查到的大多...
  • kong_gu_you_lan
  • kong_gu_you_lan
  • 2016-12-14 09:40
  • 7834

Android强大的图表开源——MPAndroidChart

介绍在APP开发中遇到图表的样式,一般我们要先查询GitHub上比较火的开源框架,这种图标应用广泛,统计、游戏统计、人际关系图等等,用到今天的这个框架MPAndroidChart 点击查看Git...
  • qq_37293612
  • qq_37293612
  • 2017-02-09 23:54
  • 5176

Android图表超简单实现柱状图、折线图、饼状图(基于MpAndroidChart)

本文基于MpAndroidChart实现各种图标,目的是让同学们快速实现图标效果 实现效果: 柱状图 折线图 饼状图 实现步骤: 1.导入 先从git上克隆下来,下载之后将jar...
  • XieChaoAndroid
  • XieChaoAndroid
  • 2016-12-01 16:40
  • 2267

Android图表超简单实现柱状图、折线图、饼状图(基于MpAndroidChart)

Android图表超简单实现柱状图、折线图、饼状图(基于MpAndroidChart)
  • u013651026
  • u013651026
  • 2017-11-20 17:04
  • 176

android 7个点折线图 chartview类

自定义折线图,可以随手势移动查看选中的数据。
  • namesuper
  • namesuper
  • 2016-04-22 14:09
  • 2138

ChartView与LineSeries搭配实现曲线局部缩放功能

效果图: 上一篇文章实现的时候还不知道有QtChart这个模块......好好看了下资料就想做个例子实现一下这功能,比较了下代码量...恩,直接看代码: Rectangle { ...
  • w54a3teg64c7bd
  • w54a3teg64c7bd
  • 2017-02-24 16:11
  • 750

HelloChart--ColumnChartView(柱形图)

在XML中的定义: </lecho.lib.helloch
  • litian921226
  • litian921226
  • 2016-04-19 09:20
  • 3265

LineChartViewDemo

  • 2016-10-18 15:28
  • 22.67MB
  • 下载
    个人资料
    • 访问:254247次
    • 积分:2581
    • 等级:
    • 排名:第16304名
    • 原创:34篇
    • 转载:0篇
    • 译文:2篇
    • 评论:67条
    博客专栏
    文章分类
    最新评论