Android统计图表_Android自定义柱形图Demo下载
2016-12-27 14:09:45  By: dwtedx

记得之前也分享过一些关于统计图的Demo、之前分享都有很炫酷的效果、但是是通过SDK来做的、看不到里面的源代码、而今天分享的是一个继承至View的一个自定义View、大家可以通过这个Demo充分了解统计图的实现方式、以便于学习和改造自己的特定统计图

android柱形图


绘制统计图代码

public void drawChart(Canvas canvas) {
    if (flag == 1) {
        paint.setColor(Color.GREEN) ;
        int temp_screen = 30 ;
        for (int i = 0; i < 4; i  ) {
            chart.setH(data_screen[i]) ;
            chart.setX(temp_screen   20 * 2   margin) ;
            chart.drawSelf(canvas, paint) ;
            margin = 20 ;
            temp_screen = chart.getX() ;
        }

        margin = 0 ;

        paint.setColor(Color.BLUE) ;
        int temp_power = 50 ;
        for (int i = 0; i < 4; i  ) {
            chart.setH(data_power[i]) ;
            chart.setX(temp_power   20 * 2   margin) ;
            chart.drawSelf(canvas, paint) ;
            margin = 20 ;
            temp_power = chart.getX() ;
        }

        drawHighLines(canvas) ;
    } else if (flag == 2) {
        paint.setColor(Color.GREEN) ;
        int temp = 40 ;
        for (int i = 0; i < 4; i  ) {
            chart.setH(data_total[i]) ;
            chart.setX(temp   20 * 2   margin) ;
            chart.drawSelf(canvas, paint) ;
            margin = 20 ;
            temp = chart.getX() ;
        }
    }
}


绘制线条统计图

public void drawHighLines(Canvas canvas) {
    int[][] highPoints = new int[4][2] ;
    highPoints[0][0] = 90 ;
    highPoints[0][1] = data_total[0] ;
    highPoints[1][0] = 150 ;
    highPoints[1][1] = data_total[1] ;
    highPoints[2][0] = 210 ;
    highPoints[2][1] = data_total[2] ;
    highPoints[3][0] = 270 ;
    highPoints[3][1] = data_total[3] ;
    paint.setColor(Color.RED) ;
    for (int i = 0; i < 4; i  ) {
        canvas.drawPoint(highPoints[i][0], 300 - highPoints[i][1], paint) ;
        canvas.drawText(data_total[i]   "", highPoints[i][0] - 10, 300 - highPoints[i][1] - 10,
                paint) ;
    }

    float[] pts = new float[16] ;
    for (int i = 0; i < 12; i  ) {
        pts[0] = 32 ;
        pts[1] = 300 - highPoints[0][1] ;
        pts[2] = highPoints[0][0] ;
        pts[3] = 300 - highPoints[0][1] ;
        pts[4] = highPoints[0][0] ;
        pts[5] = 300 - highPoints[0][1] ;
        pts[6] = highPoints[1][0] ;
        pts[7] = 300 - highPoints[1][1] ;
        pts[8] = highPoints[1][0] ;
        pts[9] = 300 - highPoints[1][1] ;
        pts[10] = highPoints[2][0] ;
        pts[11] = 300 - highPoints[2][1] ;
        pts[12] = highPoints[2][0] ;
        pts[13] = 300 - highPoints[2][1] ;
        pts[14] = highPoints[3][0] ;
        pts[15] = 300 - highPoints[3][1] ;
    }
    canvas.drawLines(pts, paint) ;
}


android统计图表代码下载链接: android柱形图 密码: djjp

若资源对你有帮助、浏览后有很大收获、不妨小额打赏我一下、你的鼓励是维持我不断写博客最大动力

想获取DD博客最新代码、你可以扫描下方的二维码、关注DD博客微信公众号(ddblogs)

或者你也可以关注我的新浪微博、了解DD博客的最新动态:DD博客官方微博(dwtedx的微博)

如对资源有任何疑问或觉得仍然有很大的改善空间、可以对该博文进行评论、希望不吝赐教

为保证及时回复、可以使用博客留言板给我留言: DD博客留言板(dwtedx的留言板)

感谢你的访问、祝你生活愉快、工作顺心、欢迎常来逛逛


猜你喜欢的

快速评论


技术评论

  • 该技术还没有评论、赶快抢沙发吧...
DD记账
top
+