Android简单的折线图表之LeftChart

左侧坐标自定义View.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.geyek.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
* Created by LiHuan on 09/10/2016.
*/
public class LeftChart extends View {

private SimpleChart mSimpleChart;

private float mWidth;
private float mHeight;
private float maxY;
private float mTextSpace;

private Paint mPaint = new Paint();
private float mYLablepadding;
private float mYLableSize;

public LeftChart(Context context) {
super(context);
}

public LeftChart(Context context, AttributeSet attrs) {
super(context, attrs);
}

public LeftChart(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@
Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
}

@
Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}

@
Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

update();

if (mSimpleChart == null) return; //如果绑定为空,则不显示了

float chartHeight = mSimpleChart.getChartHeight();

mPaint.setStrokeWidth(2);
canvas.drawLine(mWidth, 0, mWidth, mSimpleChart.getChartHeight(), mPaint);

int row = mSimpleChart.getRow();
float h = chartHeight / row;
while (true) {
if (mTextSpace + mYLablepadding & gt; mWidth) {
float textSize = mPaint.getTextSize();
mPaint.setTextSize(textSize - 1);
mTextSpace = mPaint.measureText(maxY + "");
} else {
break;
}
}

for (int i = 0; i & lt; = row; i++) {
if (i == 0) {
String v = mFormatYLableListener == null ? "0" : mFormatYLableListener.format(0);
canvas.drawText(v, mWidth - mYLablepadding - mPaint.measureText(v), mSimpleChart.getChartHeight() + mYLableSize / 3, mPaint);
} else if (i == row) {
String v = mFormatYLableListener == null ? (int) maxY + "" : mFormatYLableListener.format((int) maxY);
canvas.drawText(v, mWidth - mYLablepadding - mPaint.measureText(v), mYLableSize / 5 * 4, mPaint);
} else {
String v = mFormatYLableListener == null ? (int)(maxY / row * (row - i)) + "" : mFormatYLableListener.format((int)(maxY / row * (row - i)));
canvas.drawText(v, mWidth - mYLablepadding - mPaint.measureText(v), h * i + mYLableSize / 3, mPaint);
}
}
}

public void bindSimpleChar(SimpleChart simpleChart) {
mSimpleChart = simpleChart;
update();
}

public void update() {
if (mSimpleChart == null) return;
maxY = mSimpleChart.getMaxY();
mYLableSize = mSimpleChart.getXLableSize();
mYLablepadding = mSimpleChart.getXlablePadding();
mPaint.setTextSize(mYLableSize);
mTextSpace = mPaint.measureText(maxY + "");
}

/*public void setPreLable(String preLable) {
this.mPreLable = preLable;
}*/

public interface FormatYLableListener {
public String format(float yLable);
}

public FormatYLableListener mFormatYLableListener;

public void setFormatYLable(FormatYLableListener l) {
mFormatYLableListener = l;
}
}

图表坐标Bean类
package com.geyek.widget;

/**
* Created by LiHuan on 10/09/16.
*/
public class ChartPoint {
public float valueY;
public String lableY;
public String lableX;
public int pointColor;
public float hintXoffset;
public float hintYoffset;
}