<p> 本代码是一年前公司项目需要自己所写,当时做android项目需要统计的功能,统计的图表包括饼图、柱状图、折线图,代码写的有些笨拙!希望大家能够看懂!其代码做抛砖引玉,希望大家能够再改写下!可以用多线程做出动画,是不是更好呢?这些后期我也没有整理过,所以谁能再整理下!</p>
<p>CookieData.java</p>
<p><span style="color:#ff0000">class CookieData</span></p>
<pre class="blockcode"><code class="language-java">package com.wforyou.dhh.ui.widget;
public class CookieData {
/**名称**/
private String name;
/**数值**/
private double data;
/**颜色**/
private int color;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getData() {
return data;
}
public void setData(double data) {
this.data = data;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
@Override
public String toString() {
return "CookieData [name=" + name + ", data=" + data + ", color="
+ color + "]";
}
}
</code></pre>
<p><span style="color:#ff0000">Cookie.java</span></p>
<pre class="blockcode"><code class="language-java">package com.wforyou.dhh.ui.widget;
import com.wforyou.dhh.util.Logs;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.TextView;
public class Cookie extends View {
private int mAscent;
private String mText = "";
// 屏幕的高度
public static int heightPixels = 0;
// 屏幕的宽度
public static int widthPixels = 0;
/** 画笔 **/
private static Paint paint;
/** 半径 **/
private float radius = 130;
/** X起点坐标 **/
private static float xstartcoordinate = 100;
/** Y起点坐标 **/
private static float ystartcoordinate = 180;
/** X终点坐标 **/
private static float xendcoordinate = 400;
/** Y终点坐标 **/
private static float yendcoordinate = 180;
private static RectF f;
private static int[] colors = { Color.parseColor("#29A3A4"),
Color.parseColor("#4239A2"), Color.parseColor("#9C46FF"),
Color.parseColor("#E8C000"), Color.parseColor("#4B0064"),
Color.parseColor("#16699B"), Color.parseColor("#3E635B"),
Color.parseColor("#B19862"), Color.parseColor("#F736F7"),
Color.parseColor("#164988") };
private String[] textValues = { "裤子", "裙子", "内裤", "文胸", "保暖衣" };
private double[] numberValues = { 63, 80, 0, 150, 140 };
private String noteText = "";
private static float statar = 0;
private static float addx = 0;
private static float addy = 0;
// 距离屏幕左边的距离
private static float marginleft = 0;
// 距离屏幕上边的距离
private static float margintop = 40;
// 距离屏幕右边的距离
private static float marginrigth = 0;
// 距离屏幕下边的距离
private static float margindown = 50;
/** 圆下方提示 **/
private String downNoteText = "大类 金额";
public Cookie(Context context) {
super(context);
}
public Cookie(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Cookie(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public String[] getTextValues() {
return textValues;
}
public void setTextValues(String[] textValues) {
this.textValues = textValues;
}
public double[] getNumberValues() {
return numberValues;
}
public void setNumberValues(double[] numberValues) {
this.numberValues = numberValues;
}
public String getNoteText() {
return noteText;
}
public void setNoteText(String noteText) {
this.noteText = noteText;
}
public String getDownNoteText() {
return downNoteText;
}
public void setDownNoteText(String downNoteText) {
this.downNoteText = downNoteText;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
getScreenData();
paint = new Paint();
canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent,
paint);
// 抗锯齿
paint.setAntiAlias(true);
// 实心扇形
paint.setStyle(Style.FILL);
// 设置矩形
getRectF();
// 得到总数据
double dsum = getSum();
getaddy();
int intlength = numberValues.length;
float temp = 0;
float dtemp = 0;
for (int i = 0; i < intlength; i++) {
if (numberValues[i] > 0) {
doDrawTextNote(colors[i], textValues[i], 50, canvas);
dodrawdegree(canvas, colors[i], temp,
(float) (numberValues[i] / dsum) * 360);// 画扇形
dtemp = temp + (float) (numberValues[i] / dsum) * 360 / 2;
temp += ((float) (numberValues[i] / dsum) * |
|