前面说了 饼图,现在再来个折线图。
很多功能 都可以在基础上添加,
接下来代码,这个只是 原始的基础功能代码。
package com.lvxin.chartview.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
public class ChartView extends View {
private int lineColor = Color.BLACK; // 轴线颜色
private float lineWith = 1.0f; // 轴线的粗细
private int textColor; // 设置文本的颜色
private int textSize = 11; // 设置文本字体的大小
private int paintDataType; // 设置点的类型
private int leftMargins = 50; // 左边距
private int rightMargins = 20; // 右边距
private int buttomMargins = 10; // 下边距
private int topMargins = 10; // 上边距
private float XScale; // X的刻度长度
private int XLength; // X轴的长度
private int YScale; // Y的刻度长度
private int YLength; // Y轴的长度
private boolean hasYLine = true; // 是否显示Y轴轴线
private boolean hasXLine = true; // 是否吸纳是X轴轴线
private boolean hasYdata = true; // 是否显示Y轴数据
private boolean hasXScale= true; // 是否显示X轴的刻度
private int[] data; // 数据
private String Title; // 显示的标题
private Paint paintLine, paintText, paintShenHe, paintZi, paintHong,
paintJu, paintHuang, paintLu, dottedLine, dataPaint;
private int dataPaintColor = Color.RED;
private int datapaintWidth = 3;
public ChartView(Context context) {
super(context);
}
public ChartView(Context contex, AttributeSet attributeSet) {
super(contex, attributeSet);
}
public ChartView(Context context, int[] data) {
super(context);
setData(data);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);// 重写onDraw方法
if (null!= data) {
initPaint();
this.YLength = getHeight() - buttomMargins - topMargins;
this.XLength = getWidth() - leftMargins - rightMargins;
//根据X的数据得出 X刻度
XScale = XLength / data.length;
if (this.getRight() < XScale){
rightMargins = (int) (XScale + 10);
}
lineDraw(canvas);
}
}
private void lineDraw(Canvas canvas) {
// X Y 轴
if (isHasYLine()) {
canvas.drawLine(leftMargins, topMargins + YLength, leftMargins,
topMargins, paintLine);
}
if (isHasXLine()) {
canvas.drawLine(leftMargins, topMargins + YLength, XLength
+ leftMargins, topMargins + YLength, paintLine);
}
Paint[] paintList = { paintShenHe, paintZi, paintHong, paintJu,
paintHuang, paintLu };
String[] str = { "严重", "重度", "中度", "轻度", "良", "优" };
/*************************************************************************
*************************** 设置Y轴 **************************************
*************************************************************************
*/
for (int i = 0; i < 6; i++) {
// 绘制颜色矩形 坐标点
canvas.drawRect(
new RectF(leftMargins - 20, //左X
i * (YLength / 6f) + topMargins, //上Y
leftMargins, //右X
(i + 1) * (YLength / 6f)+ topMargins), //下Y
paintList[i]);
// 绘制表格线
canvas.drawLine(leftMargins, i * (YLength / 6f) + topMargins,
XLength + leftMargins, i * (YLength / 6f) + topMargins,
dottedLine);
// 绘制程度文字
//paintText.setStrokeWidth(1f); //设置线的宽度
paintText.setTextSize(16); //设置画出来的字体的大小
//paintText.setColor(color);
//paintText.setTextAlign(align)
canvas.drawText(str[i], leftMargins - 50, //X
(i + 1) * (YLength / 6f)- (YLength / 12f) + topMargins,//Y
paintText);
}
Path path1=new Path();
dataPaint.setStrokeWidth(1);
RectF oval2;
/*************************************************************************
*************************** 设置X轴 **************************************
*************************************************************************
*/
for (int i = 0; i * XScale <= XLength && i < data.length; i++) {
// 绘制刻度
try {
if (isHasXScale()){
canvas.drawText(String.valueOf(i), leftMargins + (i + 1)
* XScale - 2, YLength + topMargins + 10, paintText);
}
// 画折线
if (i > 0){
canvas.drawLine(leftMargins + (i) * XScale, YCoord(getData()[i - 1]),
leftMargins + (i + 1) * XScale, YCoord(getData()[i]),
dataPaint);
}
} catch (Exception e) {
}
}
/*************************************************************************
*************************** 设置 点的样式 ********************************
*************************************************************************
*/
for (int i = 0; i * XScale <= XLength && i < data.length; i++) {
try {
if (i > 0){
//绘制数值点
drawPaintType(canvas,
leftMargins + (i + 1) * XScale, //X
YCoord(getData()[i]), 2, dataPaint); //Y
//绘制数值
if (isHasYdata()){
// paintLine.setTextSize();
canvas.drawText(String.valueOf(getData()[i]), leftMargins
+ (i + 1) * XScale - (XScale / 2),
getData()[i] > 280 ? YCoord(getData()[i]) + YLength
/ 300f * 20 : YCoord(getData()[i])
- YLength / 300f * 10, paintText);
}
}
} catch (Exception e) {
}
}
}
protected void drawPaintType(Canvas canvas, float cx, float cy,
float radius, Paint paint) {
Paint paintTmp;
switch (getPaintDataType()) {
case 0:
break;
case 1: // 绘制空心圆
paintTmp = paint;
paintTmp.setStyle(Paint.Style.STROKE);
canvas.drawCircle(cx, cy, radius, paintTmp);
break;
case 2: // 绘制实心圆
paintTmp = paint;
paintTmp.setStyle(Paint.Style.FILL);
canvas.drawCircle(cx, cy, radius, paintTmp);
break;
case 3: // 绘制空心方块
paintTmp = paint;
paintTmp.setStyle(Paint.Style.STROKE);
canvas.drawRect(new Rect((int) cx - 2, (int) cy + 2, (int) cx + 2,
(int) cy - 2), paintTmp);
break;
case 4:// 绘制实心方块
paintTmp = paint;
paintTmp.setStyle(Paint.Style.FILL);
canvas.drawRect(new Rect((int) cx - 2, (int) cy + 2, (int) cx + 2,
(int) cy - 2), paintTmp);
break;
case 5: // 绘制实心圆
paintTmp = paint;
paintTmp.setStyle(Paint.Style.FILL);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
RadialGradient mRadialGradient = new RadialGradient(cx,cy, 6,
new int[]{Color.WHITE,Color.WHITE,Color.WHITE,Color.WHITE,Color.WHITE,
Color.WHITE,Color.WHITE,Color.WHITE,Color.WHITE,Color.WHITE,
Color.RED, Color.RED ,Color.RED},
null,RadialGradient.TileMode.REPEAT);
paint.setShader(mRadialGradient);
canvas.drawCircle(cx, cy, 6, paintTmp);
paint.setShader(null);
break;
}
}
//处理 显示数据的====================== 这个其实可以判断的-自己设置这里知道300
private int YCoord(int y0){
if (y0 > 300){
return YLength + topMargins - YLength;
}else{
return (int) (YLength + topMargins - y0 * (YLength / 300f));
}
}
private void initPaint() { // 生成Paint
paintLine = new Paint();
paintLine.setStyle(Paint.Style.STROKE);
paintLine.setAntiAlias(true);// 去锯齿
if (getLineColor() != 0){
paintLine.setColor(getLineColor());
}else{
paintLine.setColor(Color.BLACK);
}
if (getLineWith() != 0)
paintLine.setStrokeWidth(getLineWith());
paintText = new Paint();
paintText.setStyle(Paint.Style.STROKE);
paintText.setAntiAlias(true);// 去锯齿
paintText.setColor(getTextColor());
paintText.setTextSize(getTextSize()); // 设置轴文字大小
// Paint 褐红色
paintShenHe = new Paint();
paintShenHe.setColor(Color.rgb(132, 8, 57));
paintShenHe.setAntiAlias(true);
// Paint 紫色
paintZi = new Paint();
paintZi.setColor(Color.rgb(173, 0, 107));
paintZi.setAntiAlias(true);
// Paint 红色
paintHong = new Paint();
paintHong.setColor(Color.rgb(247, 8, 24));
paintHong.setAntiAlias(true);
// Paint 橘黄
paintJu = new Paint();
paintJu.setColor(Color.rgb(255, 125, 0));
paintJu.setAntiAlias(true);
// Paint 黄色
paintHuang = new Paint();
paintHuang.setColor(Color.rgb(255, 231, 24));
paintHuang.setAntiAlias(true);
// Paint 绿色
paintLu = new Paint();
paintLu.setColor(Color.rgb(82, 219, 57));
paintLu.setAntiAlias(true);
// 虚线
dottedLine = new Paint();
dottedLine.setColor(Color.rgb(89, 187, 241));
dottedLine.setAntiAlias(true);
dottedLine.setAlpha(100);
// 数据Paint
dataPaint = new Paint();
dataPaint.setAntiAlias(true);
dataPaint.setColor(getDataPaintColor());
dataPaint.setStrokeWidth(getDatapaintWidth());
}
/**
* @return 获得要显示的数据
*/
public int[] getData() {
return data;
}
/**
* @param 设置要显示的数据
*/
public void setData(int[] data) {
this.data = data;
}
/**
* @return 获得显示数据的线的颜色
*/
public int getLineColor() {
return lineColor;
}
/**
* @param 设置要显示数据线的颜色
*/
public void setLineColor(int lineColor) {
this.lineColor = lineColor;
}
/**
* @return 要显示数据的线的宽度
*/
public float getLineWith() {
return lineWith;
}
/**
* @param 设置要显示线的数据的宽度
*/
public void setLineWith(int lineWith) {
this.lineWith = lineWith;
}
/**
* @return 线条的颜色
*/
public void setDataPaintColor(int dataPaintColor) {
this.dataPaintColor = dataPaintColor;
}
public int getDataPaintColor() {
return dataPaintColor;
}
/**
* @return 线条的宽度
*/
public void setDatapaintWidth(int datapaintWidth) {
this.datapaintWidth = datapaintWidth;
}
public int getDatapaintWidth() {
return datapaintWidth;
}
/**
* @return 点的类型
*/
public int getPaintDataType() {
return paintDataType;
}
/**
* @param paintType 设置点的类型 1 2 3 4 5
*/
public void setDataPaintType(int paintDataType) {
this.paintDataType = paintDataType;
}
/**
* @return 是否显示Y轴轴线
*/
public boolean isHasYLine() {
return hasYLine;
}
/**
* @param 设置是否显示Y轴轴线
*/
public void setHasYLine(boolean hasYLine) {
this.hasYLine = hasYLine;
}
/**
* @return 是否显示X轴轴线
*/
public boolean isHasXLine() {
return hasXLine;
}
/**
* @param 设置是否显示X轴轴线
*/
public void setHasXLine(boolean hasXLine) {
this.hasXLine = hasXLine;
}
/**
* @return X轴的间距
*/
public float getXScale() {
return XScale;
}
/**
* @param 设置x轴的间距
*/
public void setXScale(float xScale) {
XScale = xScale;
}
/**
* @return X轴的长度
*/
public int getXLength() {
return XLength;
}
/**
* @param 设置X轴的长度
*/
public void setXLength(int xLength) {
XLength = xLength;
}
/**
* @return Y轴的刻度
*/
public int getYScale() {
return YScale;
}
/**
* @param 设置Y轴的刻度
*/
public void setYScale(int yScale) {
YScale = yScale;
}
/**
* @return Y轴的长度
*/
public int getYLength() {
return YLength;
}
/**
* @param 设置Y轴的
*/
public void setYLength(int yLength) {
YLength = yLength;
}
/**
* @return 是否显示坐标点的Y值
*/
public boolean isHasYdata() {
return hasYdata;
}
/**
* @param 设置是否显示坐标点的Y值
*/
public void setHasYdata(boolean hasYdata) {
this.hasYdata = hasYdata;
}
/**
* @return 文本字体的大小
*/
public int getTextSize() {
return textSize;
}
/**
* @param 设置文本字体的大小
*/
public void setTextSize(int textSize) {
this.textSize = textSize;
}
/**
* @return 文本字体的颜色
*/
public int getTextColor() {
return textColor;
}
/**
* @param 设置文本字体的颜色
*/
public void setTextColor(int textColor) {
this.textColor = textColor;
}
/**
* @return 是否显示X轴的刻度
*/
public boolean isHasXScale() {
return hasXScale;
}
/**
* @param 设置是否显示X轴刻度
*/
public void setHasXScale(boolean hasXScale) {
this.hasXScale = hasXScale;
}
/**
* @return 左边距
*/
public int getLeftMargins() {
return leftMargins;
}
/**
* @param 设置左边距
*/
public void setLeftMargins(int leftMargins) {
this.leftMargins = leftMargins;
}
/**
* @return 右边距
*/
public int getRightMargins() {
return rightMargins;
}
/**
* @param 设置右边距
*/
public void setRightMargins(int rightMargins) {
this.rightMargins = rightMargins;
}
/**
* @return 下边距
*/
public int getButtomMargins() {
return buttomMargins;
}
/**
* @param 设置下边距
*/
public void setButtomMargins(int buttomMargins) {
this.buttomMargins = buttomMargins;
}
/**
* @return 上边距
*/
public int getTopMargins() {
return topMargins;
}
/**
* @param 设置上边距
*/
public void setTopMargins(int topMargins) {
this.topMargins = topMargins;
}
}
接下来是View的显示 需要的设置
ccView = (ChartView) findViewById(R.id.mycharview);
// 设置要显示的数据
ccView.setData(new int[] { 35, 46, 46, 32, 67, 89, 28, 28, 50,300});
ccView.setHasYLine(false); // 设置显示Y轴轴线
ccView.setBackgroundColor(Color.rgb(240, 248, 255)); // 设置背景颜色
ccView.setLineColor(Color.rgb(46, 45, 44));// 设置轴线的颜色
ccView.setLineWith(2); // 设置轴线的宽度
ccView.setDataPaintType(5); // 设置数据坐标点的类型
ccView.setTextColor(Color.BLACK);// 设置文本(优、良……)的颜色
XML 文件: 就是可以直接放入这个 view 或者 你把这个view add 到你的 布局中
<com.ll.identityview.ChartView
android:id="@+id/mycharview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>