package com.pan.myview;
import java.util.ArrayList;
import java.util.List;
import com.pan.R;
import com.pan.R.string;
import com.pan.info.OileUseUpInfo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
* 油耗信息统计图
*
* @author zjpan
*
*/
public class OileUseUpView extends View
{
/**
* 日志标志
*/
private static final String TAG = "OileUseUpView";
/**
* Y轴上显示的最多的刻度
*/
private static final int MAX_Y_SCALE_NUM = 10;
/**
* 油耗信息列表
*/
private List<OileUseUpInfo> useUpList;
/**
* 画笔
*/
private Paint paint;
/**
* 距上距离
*/
private int toTop = 20;
/**
* 距下距离
*/
private int toBottom = 40;
/**
* 距左距离
*/
private int toLeft = 20;
/**
* 距右距离
*/
private int toRight = 30;
/**
* 显示区域的高
*/
private int showHeight;
/**
* 显示区域宽
*/
private int showWidth;
/**
* X轴上每个刻度的宽
*/
private int preLengthX;
/**
* Y轴上每个刻度的高度
*/
private int preLengthY;
/**
* 最小油耗的floor整数
*/
private int floorMinOile;
/**
* 是否只有一条数据
*/
private boolean isOneData;
/**
* 最小的油耗
*/
private float minOile;
/**
* 最大油耗
*/
private float maxOile;
/**
* Y轴上画几个刻度
*/
private int countY;
/**
* 最近日期的月
*/
private int maxMonth;
/**
* 一个油耗和一格刻度的比例
*/
private int oileUseUpRate = 1;
/**
* 最近日期的年
*/
private int maxYear;
/**
* 最早日期的年
*/
private int minYear;
/**
* 最早日期的月
*/
private int minMonth;
public OileUseUpView(Context context)
{
super(context);
paint = new Paint();
}
public OileUseUpView(Context context, AttributeSet attrs)
{
super(context, attrs);
paint = new Paint();
}
protected void onDraw(Canvas canvas)
{
// 如果还没有数据,不绘制
if (null == useUpList || useUpList.size()==0)
{
return;
}
showWidth = getWidth();
showHeight = getHeight();
// 画X轴
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3.0f);
canvas.drawLine(toLeft, showHeight - toBottom, showWidth - toRight,
showHeight - toBottom, paint);
paint.setTextSize(12);
canvas.drawText(getResources().getString(R.string.month), showWidth
- toRight + 5, showHeight - toBottom + 5, paint);
// 画箭头
Path mPath = new Path();
mPath.moveTo(showWidth - toRight - 5, showHeight - toBottom - 5);
mPath.lineTo(showWidth - toRight - 5, showHeight - toBottom + 5);
mPath.lineTo(showWidth - toRight + 5, showHeight - toBottom);
mPath.close();
canvas.drawPath(mPath, paint);
// 取出最大的日期
String[] strDate = useUpList.get(useUpList.size() - 1).getDate().split(
"-");
maxYear = Integer.parseInt(strDate[0]);
maxMonth = Integer.parseInt(strDate[1]);
// 显示最近十二个月的油耗情况,这里算出显示距离现在时间最长的日期年、月
if (maxMonth == 12)
{
minMonth = 1;
minYear = maxYear;
}
else
{
minMonth = maxMonth + 1;
minYear = maxYear - 1;
}
Log.d(TAG, "maxYear/MaxMonth = " + maxYear + "/" + maxMonth);
Log.d(TAG, "minYear/MinMonth = " + minYear + "/" + minMonth);
int tempMonth = minMonth;
// 画X轴上刻度
preLengthX = (showWidth - toLeft - toRight - 5) / 12;
paint.setTextSize(10f);
for (int i = 0; i < 12; i++)
{
paint.setStrokeWidth(1.0f);
canvas
.drawLine(toLeft + (preLengthX * i), showHeight - toBottom,
toLeft + (preLengthX * i), showHeight - toBottom
- 5, paint);
paint.setStrokeWidth(0.2f);
paint.setAntiAlias(false);
canvas.drawLine(toLeft + (preLengthX * i), showHeight - toBottom,
toLeft + (preLengthX * i), toTop, paint);
paint.setAntiAlias(true);
tempMonth = tempMonth > 12 ? tempMonth - 12 : tempMonth;
// 画X轴上的年或月
if (tempMonth == 1)
{
canvas.drawText(String.valueOf(maxYear), toLeft
+ (preLengthX * i) - 10, showHeight - toBottom + 15,
paint);
}
else
{
int moveX = tempMonth > 9 ? 5 : 2;
canvas.drawText(String.valueOf(tempMonth), toLeft
+ (preLengthX * i) - moveX, showHeight - toBottom + 15,
paint);
}
tempMonth++;
}
// 画X轴下的说明
int tempPreWidth = (showWidth - toLeft - toRight - 40) / 3;
paint.setColor(Color.YELLOW);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
canvas.drawCircle(toLeft + 20, showHeight - toBottom + 30, 3, paint);
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setTextSize(12);
canvas.drawText(getResources().getString(R.string.current_use_up),
toLeft + 30, showHeight - toBottom + 34, paint);
canvas.drawText(getResources().getString(R.string.oile_change), toLeft
+ tempPreWidth + 45, showHeight - toBottom + 34, paint);
canvas.drawText(getResources().getString(R.string.oile_avg), toLeft
+ (tempPreWidth * 2) + 55, showHeight - toBottom + 34, paint);
paint.setColor(Color.RED);
paint.setStrokeWidth(2.0f);
canvas.drawLine(toLeft + tempPreWidth + 20, showHeight - toBottom + 30,
toLeft + tempPreWidth + 40, showHeight - toBottom + 30, paint);
paint.setColor(Color.GREEN);
canvas.drawLine(toLeft + (tempPreWidth * 2) + 30, showHeight - toBottom
+ 30, toLeft + (tempPreWidth * 2) + 50, showHeight - toBottom
+ 30, paint);
// 画Y轴
minOile = useUpList.get(0).getOilUseUp();
maxOile = useUpList.get(0).getOilUseUp();
// 取出最小和最大的油耗
for (int i = 0; i < useUpList.size(); i++)
{
if (useUpList.get(i).getOilUseUp() < minOile)
{
minOile = useUpList.get(i).getOilUseUp();
}
if (useUpList.get(i).getOilUseUp() > maxOile)
{
maxOile = useUpList.get(i).getOilUseUp();
}
}
floorMinOile = (int) Math.floor(minOile);
// 最高油耗与最小油耗的差值,用于计算Y轴刻度的精度
int differenceCount = (int) (Math.ceil(maxOile) - floorMinOile);
oileUseUpRate = (differenceCount / MAX_Y_SCALE_NUM) + 1;
// Y轴最多显示MAX_Y_SCALE_NUM个刻度(如果油耗相差太大,刻度太多看不清楚)
if (differenceCount / MAX_Y_SCALE_NUM > 0)
{
countY = MAX_Y_SCALE_NUM;
}
else
{
countY = differenceCount;
}
Log.d(TAG, "countY = "+countY);
if(countY==0)
{
isOneData = true;
preLengthY = showHeight;
}
else
{
isOneData = false;
preLengthY = (showHeight - toTop - toBottom) / countY;
}
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3.0f);
canvas.drawLine(toLeft, showHeight - toBottom, toLeft, toTop, paint);
Path path = new Path();
path.moveTo(toLeft - 5, toTop + 5);
path.lineTo(toLeft + 5, toTop + 5);
path.lineTo(toLeft, toTop - 5);
path.close();
canvas.drawPath(path, paint);
paint.setTextSize(12);
canvas.drawText(getResources().getString(R.string.oile_use_up),
toLeft - 12, toTop - 8, paint);
// 画Y轴上的刻度
int tempPreY;
// Y轴上刻度对应的值
int tempNum = floorMinOile;
for (int i = 0; i < countY; i++)
{
paint.setStrokeWidth(1.0f);
tempPreY = showHeight - toBottom - (preLengthY * i);
canvas.drawLine(toLeft, tempPreY, toLeft + 5, tempPreY, paint);
paint.setTextSize(10);
paint.setStrokeWidth(0.2f);
paint.setAntiAlias(