* 画饼图类
*/
public class ChartView extends View{
int areaX = 0; //矩形框的left
int areaY = 0; //矩形框的right
int colors[];
int shade_colors[];
int percent[];
private int thickness=0;//饼图厚度
//饼图大小
private int width;
private int height;
//父布局大小
private int parent_width;
private int parent_height;
//数据
private List<PerformanceChart> chart;
private double dataValue[];
private int tempWidth;
//数据和
private double dataTotal;
public ChartView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ChartView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ChartView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
/**
* @param context 上下文
* @param colors 最上面颜色数组
* @param shade_colors 阴影颜色数组
* @param percent 百分比 (和必须是360)
*/
public void setData(int[] colors,int[] shade_colors,int[] percent ,int width, int height,
List<PerformanceChart> chart, double dataValue[], double dataTotal) {
this.colors = colors;
this.shade_colors = shade_colors;
this.percent = percent;
this.width = width;
this.height = height;
this.chart = chart;
this.dataValue = dataValue;
this.dataTotal = dataTotal;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
parent_width = getWidth();
parent_height = getHeight();
int h = parent_height/(percent.length*2+1);
areaX = (int)(parent_width*0.02);
areaY = (parent_height-height)/2;
tempWidth = (int)(parent_width*0.15);
// System.out.println("width= "+width+" height="+height + " parent width="+parent_width+" areaY="+areaY);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
paint.setAntiAlias(true);
paint.setStrokeWidth(1);
if(dataTotal!=0){
for(int i=0;i<=thickness;i++){
int tempAngle=0;
// for(int j=0;j<percent.length;j++){
// paint.setColor(shade_colors[j]);
// canvas.drawArc(new RectF(areaX, areaY-i, areaX+areaWidth, areaHight-i), tempAngle,percent[j], true, paint);
// tempAngle+=percent[j];
// }
if(i==thickness){
for(int j=0;j<percent.length;j++){
//画饼图
paint.setColor(colors[j]);
canvas.drawArc(new RectF(areaX, areaY, areaX+width, height+areaY), tempAngle,percent[j], true, paint);
tempAngle+=percent[j];
//画指标方块
canvas.drawRect(parent_width-width-tempWidth, h*(2*j+1), parent_width-width-tempWidth+h,
h*(2*j+2), paint);
//画指标数据
paint.setColor(Color.BLACK);
paint.setTextSize(getResources().getDimension(R.dimen.default_chart_textsize));
canvas.drawText(chart.get(j).getPerChartDesc()+" "+dataValue[j],
parent_width-width-tempWidth+h*3/2, h*(2*j+1)+h*9/10, paint);//h*(2*j+1)+h*9/10
}
}
}
}else{
paint.setColor(Color.WHITE);
canvas.drawArc(new RectF(areaX, areaY, areaX+width, height+areaY), 0,360, true, paint);
for(int j=0;j<percent.length;j++){
paint.setColor(colors[j]);
//画指标方块
canvas.drawRect(parent_width-width-tempWidth, h*(2*j+1), parent_width-width-tempWidth+h,
h*(2*j+2), paint);
//画指标数据
paint.setColor(Color.BLACK);
paint.setTextSize(getResources().getDimension(R.dimen.default_chart_textsize));
canvas.drawText(chart.get(j).getPerChartDesc()+" "+dataValue[j],
parent_width-width-tempWidth+h*3/2, h*(2*j+1)+h*9/10, paint);//h*(2*j+1)+h*9/10
}
}
}
//设置饼图厚度
public void setThickness(int thickness) {
this.thickness = thickness;
areaY=thickness+2;
this.invalidate();
}
}
*/
public class ChartView extends View{
int areaX = 0; //矩形框的left
int areaY = 0; //矩形框的right
int colors[];
int shade_colors[];
int percent[];
private int thickness=0;//饼图厚度
//饼图大小
private int width;
private int height;
//父布局大小
private int parent_width;
private int parent_height;
//数据
private List<PerformanceChart> chart;
private double dataValue[];
private int tempWidth;
//数据和
private double dataTotal;
public ChartView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public ChartView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ChartView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
/**
* @param context 上下文
* @param colors 最上面颜色数组
* @param shade_colors 阴影颜色数组
* @param percent 百分比 (和必须是360)
*/
public void setData(int[] colors,int[] shade_colors,int[] percent ,int width, int height,
List<PerformanceChart> chart, double dataValue[], double dataTotal) {
this.colors = colors;
this.shade_colors = shade_colors;
this.percent = percent;
this.width = width;
this.height = height;
this.chart = chart;
this.dataValue = dataValue;
this.dataTotal = dataTotal;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
parent_width = getWidth();
parent_height = getHeight();
int h = parent_height/(percent.length*2+1);
areaX = (int)(parent_width*0.02);
areaY = (parent_height-height)/2;
tempWidth = (int)(parent_width*0.15);
// System.out.println("width= "+width+" height="+height + " parent width="+parent_width+" areaY="+areaY);
Paint paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Style.FILL);
paint.setAntiAlias(true);
paint.setStrokeWidth(1);
if(dataTotal!=0){
for(int i=0;i<=thickness;i++){
int tempAngle=0;
// for(int j=0;j<percent.length;j++){
// paint.setColor(shade_colors[j]);
// canvas.drawArc(new RectF(areaX, areaY-i, areaX+areaWidth, areaHight-i), tempAngle,percent[j], true, paint);
// tempAngle+=percent[j];
// }
if(i==thickness){
for(int j=0;j<percent.length;j++){
//画饼图
paint.setColor(colors[j]);
canvas.drawArc(new RectF(areaX, areaY, areaX+width, height+areaY), tempAngle,percent[j], true, paint);
tempAngle+=percent[j];
//画指标方块
canvas.drawRect(parent_width-width-tempWidth, h*(2*j+1), parent_width-width-tempWidth+h,
h*(2*j+2), paint);
//画指标数据
paint.setColor(Color.BLACK);
paint.setTextSize(getResources().getDimension(R.dimen.default_chart_textsize));
canvas.drawText(chart.get(j).getPerChartDesc()+" "+dataValue[j],
parent_width-width-tempWidth+h*3/2, h*(2*j+1)+h*9/10, paint);//h*(2*j+1)+h*9/10
}
}
}
}else{
paint.setColor(Color.WHITE);
canvas.drawArc(new RectF(areaX, areaY, areaX+width, height+areaY), 0,360, true, paint);
for(int j=0;j<percent.length;j++){
paint.setColor(colors[j]);
//画指标方块
canvas.drawRect(parent_width-width-tempWidth, h*(2*j+1), parent_width-width-tempWidth+h,
h*(2*j+2), paint);
//画指标数据
paint.setColor(Color.BLACK);
paint.setTextSize(getResources().getDimension(R.dimen.default_chart_textsize));
canvas.drawText(chart.get(j).getPerChartDesc()+" "+dataValue[j],
parent_width-width-tempWidth+h*3/2, h*(2*j+1)+h*9/10, paint);//h*(2*j+1)+h*9/10
}
}
}
//设置饼图厚度
public void setThickness(int thickness) {
this.thickness = thickness;
areaY=thickness+2;
this.invalidate();
}
}