欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

Android统计图表_Android自定义柱形图Demo下载

程序员文章站 2021-12-12 11:39:42
...

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

Android统计图表_Android自定义柱形图Demo下载


绘制统计图代码

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