Android自定义条形对比统计图
程序员文章站
2022-04-09 16:57:21
本文实例为大家分享了android自定义条形对比统计图的具体代码,供大家参考,具体内容如下一、测试截图二、实现方法package com.xtravel.widget; import java.uti...
本文实例为大家分享了android自定义条形对比统计图的具体代码,供大家参考,具体内容如下
一、测试截图
二、实现方法
package com.xtravel.widget; import java.util.timer; import java.util.timertask; import android.annotation.suppresslint; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.view.view; /** * @name 自定义数据中心旅客分析变化趋势图 * @descripation <br> * 1、根据用户提供的数据(两组float[]数)智能绘制数据的对比条形图。<br> * 2、绘制图表信息:边框、表名、建立二维坐标系、刻度数量、刻度值、单位、网络线、图例、数据系列。<br> * 3、其中y轴的最大刻度值是用户所提供数据中float[]的最大值,分度值是最大刻度值与刻度数目的比。<br> * 4、绘制用户数据对比条形图:启动线程,遍历数组值,不断在原图上刷新。<br> * @author freedoman * @date 2014-9-17 * @version 1.0 */ public class datacentercustombarchart extends view { // 框架起点坐标、中心坐标、宽高 private final int frame_x = 20; private final int frame_y = 20; private final int frame_width = 1000; private final int frame_height = 350; private final int frame_center_x = frame_width / 2 + frame_x; private final int frame_center_y = frame_height / 2 + frame_y; // 二维坐标系原点坐标 private final int origin_x = frame_x + 100; private final int origin_y = frame_y + frame_height - 100; // xy轴终点坐标 private final int xaxis_x = frame_x + frame_width - 200; private final int xaxis_y = origin_y; private final int yaxis_x = origin_x; private final int yaxis_y = frame_y + 50; // xy轴刻度数 private int countx; private int county; // xy轴分度值、真实数据分度值 private float intervalx; private float intervaly; private float intervalpress; // 单位名称 private string namex; private string namey; // 图表名称 private string charttitle; // 用户数据 private int[] data1; private int[] data2; private int currentposition; /** * 用户建立图表 * * @param context * @param charttitle * 表名称 * @param namexaxis * x轴单位 * @param nameyaxis * y轴单位 * @param county * y轴刻度数目 * @param thisyearweekperson * 用户数据 * @param lastyearweekperson * 用户数据 */ public datacentercustombarchart(context context, string charttitle, string namexaxis, string nameyaxis, int county, int[] thisyearweekperson, int[] lastyearweekperson) { super(context); this.charttitle = charttitle; // x轴刻度数量以用户数据最大数据为依据,y轴刻度数量由用户来指定 this.countx = thisyearweekperson.length > lastyearweekperson.length ? thisyearweekperson.length : lastyearweekperson.length; this.county = county; this.namex = namexaxis; this.namey = nameyaxis; this.data1 = thisyearweekperson; this.data2 = lastyearweekperson; // 计算xy轴分度值 = 轴长/刻度数 this.intervalx = (xaxis_x - origin_x) / countx; this.intervaly = (origin_y - yaxis_y) / county; // 计算用户数据分度值 = 用户数据最大值/ 刻度数 float max1 = findmaxdata(thisyearweekperson); float max2 = findmaxdata(lastyearweekperson); this.intervalpress = (max1 > max2 ? max1 : max2) / county; // startdrawdynomicbar(); } /** * 动态绘制任务 */ public void freshdynomicbar() { final timer timer = new timer(); timertask timertask = new timertask() { @override public void run() { currentposition++; postinvalidate(); if (currentposition == countx) { timer.cancel(); } } }; timer.schedule(timertask, 100, 800); } /** * 绘制图表 */ @suppresslint("drawallocation") public void ondraw(canvas canvas) { super.ondraw(canvas); canvas.drawcolor(color.white); paint paint = new paint(); initaxis(canvas, paint, charttitle); drawdynamicbar1(canvas, paint, data1); drawdynamicbar2(canvas, paint, data2); } /** * 初始化图表基本信息<br> * 表名、坐标系、刻度数量、刻度值、网络线、图例、数据系列 * * @param canvas */ private void initaxis(canvas canvas, paint paint, string charttitle) { // 画边框 paint.setcolor(color.gray); paint.setstyle(paint.style.stroke); paint.setstrokewidth(6); canvas.drawrect(frame_x, frame_y, frame_width, frame_height, paint); // 画坐标轴 paint.setcolor(color.black); paint.setstyle(paint.style.stroke); paint.setstrokewidth(5); // x轴及方向箭头 canvas.drawline(origin_x, origin_y, xaxis_x, xaxis_y, paint); canvas.drawline(xaxis_x, xaxis_y, xaxis_x - 10, xaxis_y - 10, paint); canvas.drawline(xaxis_x, xaxis_y, xaxis_x - 10, xaxis_y + 10, paint); // y轴及方向箭头 canvas.drawline(origin_x, origin_y, yaxis_x, yaxis_y, paint); canvas.drawline(yaxis_x, yaxis_y, yaxis_x - 10, yaxis_y + 10, paint); canvas.drawline(yaxis_x, yaxis_y, yaxis_x + 10, yaxis_y + 10, paint); // 图表名称(2014年五月第一周) paint.setcolor(color.black); paint.setstyle(paint.style.fill); paint.settextsize(20);// 设置字体大小 paint.setstrokewidth(2); canvas.drawtext(charttitle, frame_center_x - 100, frame_y + 30, paint); // 绘制数据系列20*20矩形(今年、去年) paint.setcolor(color.cyan); canvas.drawrect(frame_width - 100, frame_center_y - 30, frame_width - 70, frame_center_y, paint); canvas.drawtext("今年", frame_width - 60, frame_center_y, paint); paint.setcolor(color.magenta); canvas.drawrect(frame_width - 100, frame_center_y, frame_width - 70, frame_center_y + 30, paint); canvas.drawtext("去年", frame_width - 60, frame_center_y + 30, paint); // 画网格线 paint.setcolor(color.gray); paint.setstyle(paint.style.stroke); paint.setstrokewidth(2); // 横线(从x轴依次向上画) for (int i = 0; i <= county; i++) { canvas.drawline(origin_x, origin_y - i * intervaly, xaxis_x, origin_y - i * intervaly, paint); } // 竖线(从y轴依次向右画) for (int i = 0; i <= countx; i++) { canvas.drawline(origin_x + i * intervalx, origin_y, origin_x + i * intervalx, yaxis_y, paint); } // x轴刻度值(沿x轴方向1、2、3...),轴名称 paint.setcolor(color.blue); paint.setstyle(paint.style.fill); paint.setstrokewidth(2); paint.settextsize(30); for (int i = 0; i <= countx; i++) { canvas.drawtext("" + i, origin_x + i * intervalx, origin_y + 50, paint); } paint.settextsize(20); // y轴刻度值(沿y轴方向,用户提供的数据)轴名称 for (int i = 1; i <= county; i++) { canvas.drawtext("" + (int) (i * intervalpress), origin_x - 80, origin_y - i * intervaly + 5, paint); } paint.settextsize(20); canvas.drawtext("(" + namex + ")", xaxis_x + 40, xaxis_y + 50, paint); canvas.drawtext("(" + namey + ")", yaxis_x - 30, yaxis_y - 20, paint); } /** * 绘制data1变化趋势线 <br> */ @suppresslint("resourceascolor") private void drawdynamicbar1(canvas canvas, paint paint, int[] data) { float currectx_data1 = origin_x + intervalx - 30; float currecty_data1; for (int i = 1; i < currentposition; i++, currectx_data1 += intervalx) { // 绘制data1的动态条形 paint.setcolor(color.cyan); currecty_data1 = data[i - 1] / intervalpress * intervaly; canvas.drawrect(currectx_data1, origin_y - currecty_data1, currectx_data1 + 30, origin_y, paint); } } /** * 绘制data2变化趋势线 */ @suppresslint("resourceascolor") private void drawdynamicbar2(canvas canvas, paint paint, int[] data) { float currectx_data2 = origin_x + intervalx; float currecty_data2; for (int i = 1; i < currentposition; i++, currectx_data2 += intervalx) { // 绘制data2的动态条形 paint.setcolor(color.magenta); currecty_data2 = data[i - 1] / intervalpress * intervaly; canvas.drawrect(currectx_data2, origin_y - currecty_data2, currectx_data2 + 30, origin_y, paint); } } /** * 查找数组的最大值 * * @param data * @return float */ private int findmaxdata(int[] data) { int max = data[0]; for (int i = 1; i < data.length; i++) { if (data[i] > max) { max = data[i]; } } return max; } /** * 计算今年游客数相对去年的增长率 * * @return float 增长率百分数 */ public float getgrowthrate() { float sumyear = 0, sumlastyear = 0; for (int i = 0; i < data1.length; i++) { sumyear += data1[i]; sumlastyear += data2[i]; } return (sumyear - sumlastyear) / sumlastyear * 100; } /** * 统计一周总人数 * * @return int 人数 */ public int getsumweek() { int sum = 0; for (int i = 0; i < data1.length; i++) { sum += data1[i]; } return sum; } /** * 统计一周平均每天旅客数量 * * @return int */ public int getaverageweek() { return getsumweek() / data1.length; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
下一篇: 微信小程序点击滚动到指定位置的实现