1、View类
package com.clcx.mproject.utils.linegraph;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.PointF;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ljc123 on 2017/7/19.
*/
public class ClcxLineChart extends View {
private float max = 0.0f;
private List<LineChartModel> data = new ArrayList<>();
private Paint mPaint;
//坐标轴颜色
private static final String color1 = "#666666";
//曲线颜色
private static final String color2 = "#ff8888";
//填充颜色和alpha值
private static final String color3 = "#ff8888";
private static final int alp = 150;
//曲线宽度
private static final int value1 = 5;
//两边的padding,气泡大小=paddingx2
// private static final int PADDING = 40;
//气泡高度,文字字体大小=60/2
private static final int QP_HEIGHT = 60;
//气泡文字两侧padding
private static final int QP_PADDING = 20;
//底部PADDING,绘制日期文字,文字字体大小=40/2
private static final int BOTTOM_PADDING = 40;
//小圆点半径
private static final int POINT_RADIU = 4;
public ClcxLineChart(Context context) {
super(context);
init();
}
public ClcxLineChart(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClcxLineChart(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (data.size() > 0) {
//计算气泡文字长度,以此算出图表两侧的padding
mPaint.setTextSize(QP_HEIGHT / 2);
String val = String.valueOf(data.get(data.size() - 1).getValue());
int qpWidth = (int) mPaint.measureText(val) / 2 + QP_PADDING;
//坐标真实宽度和高度
final int width = getMeasuredWidth() - 2 * qpWidth;
final int height = getMeasuredHeight() - BOTTOM_PADDING;
//绘制坐标
mPaint.setColor(Color.parseColor(color1));
canvas.drawLine(qpWidth, height, width + qpWidth, height, mPaint);
//绘制曲线
final int size = this.data.size();
PointF[] point = new PointF[size];
mPaint.setColor(Color.parseColor(color2));
mPaint.setStrokeWidth(value1);
mPaint.setStyle(Paint.Style.STROKE);
if (this.data.size() > 1) {
point[0] = new PointF(qpWidth, ((1.0f - (this.data.get(0).getValue() / max)) * height));
for (int a = 1; a < this.data.size(); a++) {
point[a] = new PointF(qpWidth + width / (size - 1) * a, ((1.0f - (this.data.get(a).getValue() /
max)) *
height));
canvas.drawLine(point[a - 1].x, point[a - 1].y, point[a].x, point[a].y,
mPaint);
}
}
//绘制填充色
mPaint.setColor(Color.parseColor(color3));
mPaint.setStrokeWidth(0);
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAlpha(alp);
Path path = new Path();
path.moveTo(qpWidth, height);
for (int a = 0; a < this.data.size(); a++) {
path.lineTo(point[a].x, point[a].y);
}
path.lineTo(width + qpWidth, height);
path.close();
canvas.drawPath(path, mPaint);
//绘制底端文字
mPaint.setColor(Color.parseColor(color1));
mPaint.setTextSize(BOTTOM_PADDING / 2);
for (int a = 0; a < size; a++) {
String time = data.get(a).getTime();
int fontHeight = (int) Math.ceil(mPaint.getFontMetrics().descent - mPaint.getFontMetrics().ascent);
int fontWidth = (int) mPaint.measureText(time);
canvas.drawText(time, qpWidth - (fontWidth / 2) + (width / (size - 1)) * a, height + fontHeight,
mPaint);
}
//绘制圆形
PointF lp = point[data.size() - 1];
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(value1);
mPaint.setColor(Color.parseColor("#ffffff"));
canvas.drawCircle(lp.x, lp.y, POINT_RADIU * 2, mPaint);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(value1);
mPaint.setColor(Color.parseColor(color2));
canvas.drawCircle(lp.x, lp.y, POINT_RADIU * 2, mPaint);
//绘制气泡
Path qpPath = new Path();
int realY = (int) (lp.y - POINT_RADIU);
qpPath.moveTo(lp.x, realY);
qpPath.lineTo(lp.x - 10, realY - 10);
qpPath.lineTo(lp.x - qpWidth, realY - 10);
qpPath.lineTo(lp.x - qpWidth, realY - QP_HEIGHT);
qpPath.lineTo(lp.x + qpWidth, realY - QP_HEIGHT);
qpPath.lineTo(lp.x + qpWidth, realY - 10);
qpPath.lineTo(lp.x + 10, realY - 10);
qpPath.close();
mPaint.setColor(Color.parseColor(color2));
mPaint.setStyle(Paint.Style.FILL);
mPaint.setStrokeWidth(0);
canvas.drawPath(qpPath, mPaint);
//绘制文字
mPaint.setTextSize(QP_HEIGHT / 2);
mPaint.setColor(Color.WHITE);
int fontHeight = (int) Math.ceil(mPaint.getFontMetrics().descent - mPaint.getFontMetrics().ascent);
int fontWidth = (int) mPaint.measureText(val);
canvas.drawText(val, lp.x - fontWidth / 2, realY - 10 - (QP_HEIGHT / 2) + (fontHeight / 2), mPaint);
}
}
/**
* 初始化图表数据
*
* @param data
* @param max
*/
public void initData(List<LineChartModel> data, float max) {
this.data = data;
this.max = max;
invalidate();
}
public void addData(LineChartModel model) {
this.data.add(model);
invalidate();
}
}
2、Model类
package com.clcx.mproject.utils.linegraph;
/**
* Created by ljc123 on 2017/7/19.
*/
public class LineChartModel {
private String time;
private float value;
public LineChartModel(String time, float value) {
this.time = time;
this.value = value;
}
public String getTime() {
return time;
}
public float getValue() {
return value;
}
}
3、使用
clcxlinechart= (ClcxLineChart) findViewById(R.id.clcxlinechart);
clcxlinechart.initData(datas(), 0.5f);
private List<LineChartModel> datas() {
List<LineChartModel> datas = new ArrayList<>();
for (int a = 0; a < 15; a++) {
datas.add(new LineChartModel(日期, 随机数(0.1f, 0.3f)));
}
return datas;
}
效果图