柱状图以及饼图进行数据统计
2013-09-28 16:51
316 查看
分享一下图形统计的成果:
activity_statistics.xml
Activity文件:
StatisticsActivity.java
饼图统计的主要文件:
PieChart.java
BaseChart.java:
实体类:
TitleValueColorEntity.java
TitleValueEntity.java
activity_statistics.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f0f5f8" android:orientation="vertical" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="@dimen/title_bar_height" android:background="@drawable/pub_title_bg" > <Button android:id="@+id/btn_back_help" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:background="@drawable/com_back_btn_bg" android:onClick="onBack" android:text="@string/back" /> <TextView android:id="@+id/txt_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:gravity="center" android:text="数据统计" android:textColor="@android:color/white" android:textSize="@dimen/title_font_size" /> </RelativeLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:orientation="horizontal" > <TextView android:layout_width="30dp" android:layout_height="20dp" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:background="@android:color/holo_green_light" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text=" :已核查" android:textColor="@color/black" android:textSize="@dimen/text_size_18" /> <TextView android:layout_width="30dp" android:layout_height="20dp" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:background="@android:color/holo_blue_light" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text=" :新增" android:textColor="@color/black" android:textSize="@dimen/text_size_18" /> <TextView android:layout_width="30dp" android:layout_height="20dp" android:layout_gravity="center_vertical" android:layout_marginLeft="20dp" android:background="@android:color/holo_red_light" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:text=" :未核查" android:textColor="@color/black" android:textSize="@dimen/text_size_18" /> <TextView android:id="@+id/count_total" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_marginLeft="50dp" android:text="合计数据:" android:textColor="@color/black" android:textSize="@dimen/text_size_18" /> </LinearLayout> <ScrollView android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="none" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="20dp" android:orientation="horizontal" > <!-- /**************柱图 ********************* --> <RelativeLayout android:layout_width="435dp" android:layout_height="325dp" android:layout_marginTop="15dp" android:layout_weight="1" > <RelativeLayout android:id="@+id/left_line" android:layout_width="2dp" android:layout_height="match_parent" android:layout_alignParentLeft="true" android:background="@android:color/darker_gray" android:orientation="horizontal" > </RelativeLayout> <RelativeLayout android:id="@+id/bottom_line" android:layout_width="match_parent" android:layout_height="2dp" android:layout_alignParentBottom="true" android:background="@android:color/darker_gray" android:orientation="vertical" > </RelativeLayout> <TextView android:id="@+id/block_finish" android:layout_width="126dp" android:layout_height="300dp" android:layout_above="@id/bottom_line" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/left_line" android:background="@android:color/holo_green_light" /> <TextView android:id="@+id/count_finish" android:layout_width="126dp" android:layout_height="wrap_content" android:layout_above="@id/block_finish" android:layout_marginLeft="15dp" android:gravity="center" android:text="110" android:textColor="@color/black" android:textSize="@dimen/text_size_18" /> <TextView android:id="@+id/block_add" android:layout_width="126dp" android:layout_height="200dp" android:layout_above="@id/bottom_line" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/block_finish" android:background="@android:color/holo_blue_light" /> <TextView android:id="@+id/count_add" android:layout_width="126dp" android:layout_height="wrap_content" android:layout_above="@id/block_add" android:layout_marginLeft="156dp" android:gravity="center" android:text="110" android:textColor="@color/black" android:textSize="@dimen/text_size_18" /> <TextView android:id="@+id/block_undo" android:layout_width="126dp" android:layout_height="100dp" android:layout_above="@id/bottom_line" android:layout_marginLeft="15dp" android:layout_toRightOf="@id/block_add" android:background="@android:color/holo_red_light" /> <TextView android:id="@+id/count_undo" android:layout_width="126dp" android:layout_height="wrap_content" android:layout_above="@id/block_undo" android:layout_marginLeft="297dp" android:gravity="center" android:text="110" android:textColor="@color/black" android:textSize="@dimen/text_size_18" /> </RelativeLayout> <!-- **************柱图 *********************/ --> <com.landtool.gdsc.view.chart.PieChart android:id="@+id/piechart" android:layout_width="350dp" android:layout_height="350dp" android:layout_marginLeft="40dp" android:layout_weight="1" /> </LinearLayout> </ScrollView> </LinearLayout>
Activity文件:
StatisticsActivity.java
import java.util.ArrayList; import java.util.List; import com.landtool.gdsc.R; import com.landtool.gdsc.common.utils.ViewUtil; import com.landtool.gdsc.db.GeometryDao; import com.landtool.gdsc.db.WatComInfoNDao; import com.landtool.gdsc.db.WatComInfoYDao; import com.landtool.gdsc.view.chart.PieChart; import com.landtool.gdsc.view.chart.TitleValueColorEntity; import android.app.Activity; import android.content.res.Resources; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.TextView; /** * 图形统计 * @author cyj * */ public class StatisticsActivity extends Activity { public static final int MAX_RECT_HEIGHT = 300; private PieChart piechart; private int checkedNum; private int uncheckedNum ; private int addNum; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_statistics); checkedNum = countCheckedCompany(); uncheckedNum = countUnCheckedCompany(); addNum = countAddCompany(); initPieChart(); initRectChart(); } private void initRectChart(){ int sum = checkedNum + uncheckedNum + addNum; TextView totalCountView = (TextView) findViewById(R.id.count_finish); totalCountView.setText(checkedNum+""); TextView checkCountView = (TextView) findViewById(R.id.count_total); checkCountView.setText("合计数据:"+sum+" 笔"); TextView uncheckCountView = (TextView) findViewById(R.id.count_undo); uncheckCountView.setText(uncheckedNum+""); TextView addCountView = (TextView) findViewById(R.id.count_add); addCountView.setText(addNum+""); float checkedPer = 0.0F; float uncheckedPer = 0.0F; float addPer = 0.0F; if(0 != sum){ checkedPer = (float)checkedNum/sum; uncheckedPer = (float)uncheckedNum/sum; addPer = (float)addNum/sum; } TextView checkView = (TextView) findViewById(R.id.block_finish); LayoutParams paramas1 = checkView.getLayoutParams(); paramas1.height = Math.round(checkedPer*MAX_RECT_HEIGHT); checkView.setLayoutParams(paramas1); TextView uncheckView = (TextView) findViewById(R.id.block_undo); LayoutParams paramas2 = uncheckView.getLayoutParams(); paramas2.height = Math.round(uncheckedPer*MAX_RECT_HEIGHT); uncheckView.setLayoutParams(paramas2); TextView addView = (TextView) findViewById(R.id.block_add); LayoutParams paramas3 = addView.getLayoutParams(); paramas3.height = Math.round(addPer*MAX_RECT_HEIGHT); addView.setLayoutParams(paramas3); } private void initPieChart(){ piechart = (PieChart)findViewById(R.id.piechart); List<TitleValueColorEntity> data = new ArrayList<TitleValueColorEntity>(); Resources resources = getResources(); data.add(new TitleValueColorEntity("已核查",checkedNum,resources.getColor(android.R.color.holo_green_light))); data.add(new TitleValueColorEntity("未核查",uncheckedNum,resources.getColor(android.R.color.holo_red_light))); data.add(new TitleValueColorEntity("新增",addNum,resources.getColor(android.R.color.holo_blue_light))); piechart.setData(data); } private int countCheckedCompany(){ return new WatComInfoYDao().count(); } private int countUnCheckedCompany(){ return new WatComInfoNDao().count(); } private int countAddCompany(){ return new GeometryDao().count(); } public void onBack(View view) { ViewUtil.hideInput(this,view); onBackPressed(); } }
饼图统计的主要文件:
PieChart.java
import java.util.List; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Point; import android.graphics.RectF; import android.graphics.Paint.Style; import android.util.AttributeSet; public class PieChart extends BaseChart { /** * 默认图表标题 */ public static final String DEFAULT_TITLE = "Pie Chart"; /** * 默认是否显示经线 */ public static final boolean DEFAULT_DISPLAY_RADIUS = true; /** * 默认半径长度 */ public static final int DEFAULT_RADIUS_LENGTH = 80; /** * 默认经线颜色 */ public static final int DEFAULT_RADIUS_COLOR = Color.WHITE; /** * 默认圆弧的颜色 */ public static final int DEFAULT_CIRCLE_BORDER_COLOR = Color.WHITE; /** * 默认绘图中心位置 */ public static final Point DEFAULT_POSITION = new Point(0, 0); /** * 图表数据 */ private List<TitleValueColorEntity> data; /** * 图表标题 */ private String title = DEFAULT_TITLE; /** * 绘图中心位置 */ private Point position = DEFAULT_POSITION; /** * 半径长度 */ private int radiusLength = DEFAULT_RADIUS_LENGTH; /** * 经线颜色 */ private int radiusColor = DEFAULT_RADIUS_COLOR; /** * 圆弧的颜色 */ private int circleBorderColor = DEFAULT_CIRCLE_BORDER_COLOR; /** * 是否显示经线 */ private boolean displayRadius = DEFAULT_DISPLAY_RADIUS; public PieChart(Context context) { super(context); } public PieChart(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public PieChart(Context context, AttributeSet attrs) { super(context, attrs); } /** * 绘制图表时调用 */ @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // get safe rect int rect = super.getWidth() > super.getHeight() ? super.getHeight() : super.getWidth(); // calculate radius length radiusLength = (int) ((rect / 2f) * 0.90); // calculate position position = new Point((int) (getWidth() / 2f), (int) (getHeight() / 2f)); // draw this chart drawCircle(canvas); // draw data on chart drawData(canvas); } /** * 绘制一个圆 * @param canvas */ protected void drawCircle(Canvas canvas) { Paint mPaintCircleBorder = new Paint(); mPaintCircleBorder.setColor(Color.WHITE); mPaintCircleBorder.setStyle(Style.STROKE); mPaintCircleBorder.setStrokeWidth(2); mPaintCircleBorder.setAntiAlias(true); // draw a circle canvas.drawCircle(position.x, position.y, radiusLength, mPaintCircleBorder); } /** * 将数据绘制在图表上 * @param canvas */ protected void drawData(Canvas canvas) { if (null != data) { // sum all data's value float sum = 0; for (int i = 0; i < data.size(); i++) { sum = sum + data.get(i).getValue(); } Paint mPaintFill = new Paint(); mPaintFill.setStyle(Style.FILL); mPaintFill.setAntiAlias(true); Paint mPaintBorder = new Paint(); mPaintBorder.setStyle(Style.STROKE); mPaintBorder.setColor(radiusColor); mPaintBorder.setAntiAlias(true); int offset = -90; // draw arcs of every piece for (int j = 0; j < data.size(); j++) { TitleValueColorEntity e = data.get(j); // get color mPaintFill.setColor(e.getColor()); RectF oval = new RectF(position.x - radiusLength, position.y - radiusLength, position.x + radiusLength, position.y + radiusLength); int sweep = Math.round(e.getValue() / sum * 360f); canvas.drawArc(oval, offset, sweep, true, mPaintFill); canvas.drawArc(oval, offset, sweep, true, mPaintBorder); offset = offset + sweep; } float sumvalue = 0f; for (int k = 0; k < data.size(); k++) { TitleValueColorEntity e = data.get(k); float value = e.getValue(); sumvalue = sumvalue + value; float rate = (sumvalue - value / 2) / sum; mPaintFill.setColor(Color.BLUE); // percentage float percentage = (int) (value / sum * 10000) / 100f; float offsetX = (float) (position.x - radiusLength * 0.5 * Math.sin(rate * -2 * Math.PI)); float offsetY = (float) (position.y - radiusLength * 0.5 * Math.cos(rate * -2 * Math.PI)); Paint mPaintFont = new Paint(); mPaintFont.setColor(Color.BLACK); mPaintFont.setTextSize(20); // draw titles String title = e.getTitle(); float realx = 0; float realy = 0; // TODO title position if (offsetX < position.x) { realx = offsetX - mPaintFont.measureText(title) - 5; } else if (offsetX > position.x) { realx = offsetX + 5; } if (offsetY > position.y) { if (value / sum < 0.2f) { realy = offsetY + 10; } else { realy = offsetY + 5; } } else if (offsetY < position.y) { if (value / sum < 0.2f) { realy = offsetY - 10; } else { realy = offsetY + 5; } } canvas.drawText(title, realx, realy, mPaintFont); canvas.drawText(String.valueOf(percentage) + "%", realx, realy + 18, mPaintFont); } } } public List<TitleValueColorEntity> getData() { return data; } public void setData(List<TitleValueColorEntity> data) { this.data = data; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Point getPosition() { return position; } public void setPosition(Point position) { this.position = position; } public int getRadiusLength() { return radiusLength; } public void setRadiusLength(int radiusLength) { this.radiusLength = radiusLength; } public int getRadiusColor() { return radiusColor; } public void setRadiusColor(int radiusColor) { this.radiusColor = radiusColor; } public int getCircleBorderColor() { return circleBorderColor; } public void setCircleBorderColor(int circleBorderColor) { this.circleBorderColor = circleBorderColor; } public boolean isDisplayRadius() { return displayRadius; } public void setDisplayRadius(boolean displayRadius) { this.displayRadius = displayRadius; } }
BaseChart.java:
import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; public class BaseChart extends View { /* * (non-Javadoc) * * @param context * * @see android.view.View#BaseChart(Context) */ public BaseChart(Context context) { super(context); } /* * (non-Javadoc) * * @param context * * @param attrs * * @see android.view.View#BaseChart(Context, AttributeSet) */ public BaseChart(Context context, AttributeSet attrs) { super(context, attrs); } /* * (non-Javadoc) * * @param context * * @param attrs * * @param defStyle * * @see android.view.View#BaseChart(Context, AttributeSet, int) */ public BaseChart(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /* * (non-Javadoc) * * @param widthMeasureSpec * * @param heightMeasureSpec * * @see android.view.View#onMeasure(int, int) */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(measureWidth(widthMeasureSpec), measureHeight(heightMeasureSpec)); } /* * (non-Javadoc) * * @param gainFocus * * @param direction * * @param previouslyFocusedRect * * @see android.view.View#onFocusChanged(boolean, int, * android.graphics.Rect) */ @Override protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect) { super.onFocusChanged(gainFocus, direction, previouslyFocusedRect); } private int measureWidth(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } return result; } private int measureHeight(int measureSpec) { int result = 0; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } return result; } }
实体类:
TitleValueColorEntity.java
public class TitleValueColorEntity extends TitleValueEntity { /** * 颜色 */ private int color; /** * * @param title * 标题 * @param value * 值 * @param color * 颜色 */ public TitleValueColorEntity(String title, float value, int color) { super(title, value); this.color = color; } public TitleValueColorEntity() { super(); } public int getColor() { return color; } public void setColor(int color) { this.color = color; } }
TitleValueEntity.java
public class TitleValueEntity { /** * 标题 */ private String title; /** * 值 */ private float value; public TitleValueEntity(String title, float value) { super(); this.title = title; this.value = value; } public TitleValueEntity() { super(); } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public float getValue() { return value; } public void setValue(float value) { this.value = value; } }
相关文章推荐
- socket的select模型【摘】
- IOS UIScrollView \\webview不允许拖动,不露底
- 2014阿里巴巴面试题目南京站
- java解析json格式数据
- 《#诗盗#·逼制主义社会》
- uva 568 Just the Facts
- 学习笔记:VMWARE ESXI5.5在windows 2003客户端连接报错的解决方法
- SQLite数据库中用insert同时插入多条记录的方法和效率比较
- COM编程入门不得不看的文章 :第一部分 什么是COM,如何使用COM
- java信号量PV操作 解决生产者-消费者问题
- 源代码编译安装apache启动报错
- jquery easyui 小记
- ping、traceroute、netstat、host、nslookup
- JSP/JavaMail实例:邮件发送
- Shell脚本语法
- 几个简单,但实用的,工具
- 用标准C编写COM(八)COM in plain C,Part8
- 数据
- 在宏定义中怎么使用可变参数
- JSP/JavaMail实例:邮件发送