大学即将毕业,现在公司实习了,最近公司让我制作andriod端的chart,我表示一窍不通,只好慢慢学了。
迷茫了几天,看了些博客,感觉深受启发,所以把自己的尝试发出来希望能给人帮助。
先来张效果图:
下面直接上代码:
package mychart;
import java.util.List;
import android.content.Context;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.view.View;
public class LineView extends View{
private int axisColor; // 轴线颜色
private float axisWith; // 轴线宽度
private int[] lineColor; // 数据线/点颜色
private int[] lineWidth; // 数据线/点宽度
private int textColor; // 文本颜色
private int textSize; // 文本字体
private int dotType; // 数据点类型
private int leftMargins; // 左边距
private int rightMargins; // 右边距
private int bottomMargins; // 下边距
private int topMargins; // 上边距
private float XScale; // X的刻度长度
private int xLength; // X轴的长度
private int YScale; // Y的刻度长度
private int yLength; // Y轴的长度
private List<int[]> data; // y轴数据
private String[] xData; // x轴数据
private boolean hasYAxis; // 显示Y轴轴线
private boolean hasXAxis; // 显示X轴轴线
private boolean hasYScale; // 显示Y轴刻度
private boolean hasXScale; // 显示X轴刻度
private boolean showData; // 显示数据值
private Paint axisPaint, textPaint, linePaint, dotPaint;
private int[] colors = new int[]{Color.BLUE, Color.CYAN, Color.RED, Color.GREEN, Color.YELLOW};
//构造函数
public LineView(Context context) {
super(context);
}
public LineView(Context context, List<int[]> data, String[] xData) {
super(context);
setData(data, xData);
setLineColor(colors);
}
public LineView(Context context, List<int[]> data, int[] color, String[] xData) {
super(context);
setData(data, xData);
setLineColor(color);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (null!= data) {
init();
initPaint();
axisDraw(canvas);
lineDraw(canvas);
}
}
//绘制折线图
private void lineDraw(Canvas canvas){
int len = data.size();
for (int j = 0; j < len; j++){
int []temp = data.get(j);
final int MAX = getMax(data);
for (int i = 0; i < temp.length; i ++) {
linePaint.setColor(getLineColor()[j]);
dotPaint.setColor(getLineColor()[j]);
if (isShowData()){
canvas.drawText(String.valueOf(temp[i]),
leftMargins + (float) (i + 0.5) * XScale,
YCoord(MAX, temp[i]) - 20, textPaint);
}
if(i > 0){
canvas.drawLine(leftMargins + (float) (i - 0.5) * XScale, YCoord(MAX, temp[i-1]),
leftMargins + (float) (i + 0.5) * XScale, YCoord(MAX, temp[i]),
linePaint);
}
canvas.drawCircle(leftMargins + (float) (i + 0.5) * XScale,
YCoord(MAX, temp[i]), 5, dotPaint);
}
}
}
//绘制坐标系
private void axisDraw(Canvas canvas){
if (isHasYAxis()) {
canvas.drawLine(leftMargins, topMargins + yLength,
leftMargins,topMargins, axisPaint);
}
if (isHasXAxis()) {
canvas.drawLine(leftMargins, topMargins + yLength,
xLength + leftMargins, topMargins + yLength, axisPaint);
}
final int MAX = getMax(data);
for (int i = 0; i <= 5; i ++){
if (i < 5)
canvas.drawLine(leftMargins - 5, i * (yLength / 5f) + topMargins,
xLength + leftMargins, i * (yLength / 5f) + topMargins, axisPaint);
canvas.drawText(String.valueOf(MAX * (5 - i ) / 5), leftMargins - 20,
i * (yLength / 5f) + topMargins + 5, textPaint);
}
for (int i = 0; i < data.get(0).length; i ++) {
if (isHasXScale()){
canvas.drawLine(leftMargins + (i + 1) * XScale, yLength + topMargins,
leftMargins + (i + 1) * XScale, yLength + topMargins + 5,
axisPaint);
canvas.drawText(xData[i], leftMargins + (i + 1)
* XScale - XScale / 2, yLength + topMargins + 15, textPaint);
}
}
}
private void init(){
//初始化绘图范围
setLeftMargins(50);
setRightMargins(50);
setTopMargins(50);
setBottomMargins(50);
yLength = getHeight() - bottomMargins - topMargins;
xLength = getWidth() - leftMargins - rightMargins;
XScale = xLength / (data.get(0).length);
//初始化轴显示
setHasXAxis(true);
setHasYAxis(true);
setHasXScale(true);
setHasYData(true);
setShowData(true);
//初始化轴线画笔
axisPaint = new Paint();
axisPaint.setStyle(Paint.Style.STROKE);
axisPaint.setAntiAlias(true);
axisPaint.setColor(Color.GRAY);
axisPaint.setStrokeWidth(1);
//初始化折线画笔
linePaint = new Paint();
linePaint.setStyle(Paint.Style.STROKE);
linePaint.setAntiAlias(true);
linePaint.setColor(Color.BLACK);
BlurMaskFilter PaintBGBlur = new BlurMaskFilter(
1, BlurMaskFilter.Blur.SOLID);
linePaint.setMaskFilter(PaintBGBlur);
linePaint.setStrokeWidth(3);
//初始化数据点画笔
dotPaint = new Paint();
dotPaint.setStyle(Paint.Style.FILL);
dotPaint.setAntiAlias(true);
dotPaint.setColor(Color.BLACK);
//初始化文本画笔
textPaint = new Paint();
textPaint.setStyle(Paint.Style.STROKE);
textPaint.setAntiAlias(true);
textPaint.setColor(Color.BLACK);
textPaint.setTextSize(11);
textPaint.setTextAlign(Align.CENTER);
}
//设置画笔
private void initPaint() {
if (getAxisColor() != 0)
axisPaint.setColor(getAxisColor());
if (getAxisWidth() != 0)
axisPaint.setStrokeWidth(getAxisWidth());
if (getTextColor() != 0)
textPaint.setColor(getTextColor());
if (getTextSize() != 0)
textPaint.setTextSize(getTextSize());
}
//定位
private int YCoord(int Max, int y){
return (int) (yLength + topMargins - y * (yLength / (float) Max));
}
//获取图例最大值
private int getMax(List<int []> data){
int max = 0;
for (int j = 0; j< data.size(); j++)
{
int id = 0;
int[] temp = data.get(j);
for (int i = 1; i < temp.length; i++){
if (temp[i] > temp[id])
id = i;
}
if (temp [id] > max)
max = temp[id];
}
int i=0;
while (max > 10){
max /= 10;
i ++;
}
return (int) ((max + 1) * Math.pow(10, i));
}
//数据设置
public int getData(int i, int j) {
return data.get(i)[j];
}
public void setData(List<int[]> data, String[] xData) {
this.data = data;
this.xData = xData;
}
//轴线设置
public int getAxisColor() {
return axisColor;
}
public void setAxisolor(int axisColor) {
this.axisColor = axisColor;
}
public float getAxisWidth() {
return axisWith;
}
public void setAxisWidth(int axisWith) {
this.axisWith = axisWith;
}
//折线设置
public void setLineColor(int[] lineColor) {
this.lineColor = lineColor;
}
public int[] getLineColor() {
return lineColor;
}
public void setLineWidth(int[] lineWidth) {
this.lineWidth = lineWidth;
}
public int[] getLineWidth() {
return lineWidth;
}
//数据点设置
public int getDotType() {
return dotType;
}
public void setDotType(int dotType) {
this.dotType = dotType;
}
private boolean isShowData() {
return showData;
}
private void setShowData(boolean showData) {
this.showData = showData;
}
//X轴设置
public boolean isHasXAxis() {
return hasXAxis;
}
public void setHasXAxis(boolean hasXAxis) {
this.hasXAxis = hasXAxis;
}
public float getXScale() {
return XScale;
}
public void setXScale(float xScale) {
XScale = xScale;
}
public int getXLength() {
return xLength;
}
public void setXLength(int xLength) {
this.xLength = xLength;
}
public boolean isHasXScale() {
return hasXScale;
}
public void setHasXScale(boolean hasXScale) {
this.hasXScale = hasXScale;
}
//Y轴设置
public boolean isHasYAxis() {
return hasYAxis;
}
public void setHasYAxis(boolean hasYAxis) {
this.hasYAxis = hasYAxis;
}
public int getYScale() {
return YScale;
}
public void setYScale(int yScale) {
YScale = yScale;
}
public int getYLength() {
return yLength;
}
public void setYLength(int yLength) {
this.yLength = yLength;
}
public boolean isHasYScale() {
return hasYScale;
}
public void setHasYData(boolean hasYScale) {
this.hasYScale = hasYScale;
}
//文本设置
public int getTextSize() {
return textSize;
}
public void setTextSize(int textSize) {
this.textSize = textSize;
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
//边距设置
public int getLeftMargins() {
return leftMargins;
}
public void setLeftMargins(int leftMargins) {
this.leftMargins = leftMargins;
}
public int getRightMargins() {
return rightMargins;
}
public void setRightMargins(int rightMargins) {
this.rightMargins = rightMargins;
}
public int getBottomMargins() {
return bottomMargins;
}
public void setBottomMargins(int buttomMargins) {
this.bottomMargins = buttomMargins;
}
public int getTopMargins() {
return topMargins;
}
public void setTopMargins(int topMargins) {
this.topMargins = topMargins;
}
}