-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGraphView2.java
More file actions
61 lines (51 loc) · 1.98 KB
/
GraphView2.java
File metadata and controls
61 lines (51 loc) · 1.98 KB
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
package com.redkb.graphviewdemo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
public class GraphView2 extends View {
private static final int STROKE_WIDTH_DP = 4;
private final Paint mPaintLine = new Paint();
private final Path mPath = new Path();
public GraphView2(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaintLine.setColor(Color.argb(255, 33, 189, 222));
mPaintLine.setStrokeWidth((int) Utility.dpToPx(STROKE_WIDTH_DP, getResources()));
mPaintLine.setStyle(Paint.Style.STROKE);
mPaintLine.setAntiAlias(true);
mPaintLine.setStrokeJoin(Paint.Join.ROUND);
}
public void setData(float[] data) {
mPath.reset();
float max = Utility.findMax(data);
float min = Utility.findMin(data);
mPath.rMoveTo(getXFromIndex(0, data.length), getYFromValue(data[0], max, min));
for (int i = 1; i < data.length; i++) {
mPath.lineTo(getXFromIndex(i, data.length), getYFromValue(data[i], max, min));
}
invalidate();
}
@Override
protected void onDraw(Canvas canvas) {
drawAxes(canvas);
drawLineGraph(canvas);
super.onDraw(canvas);
}
private void drawLineGraph(Canvas canvas) {
canvas.drawPath(mPath, mPaintLine);
}
private void drawAxes(Canvas canvas) {
canvas.drawLine(0, 0, 0, canvas.getHeight(), mPaintLine);
canvas.drawLine(0, canvas.getHeight(), canvas.getWidth(), canvas.getHeight(), mPaintLine);
}
private float getYFromValue(float value, float max, float min) {
return (max - value) * (getHeight()) / (max - min);
}
private float getXFromIndex(int index, int max) {
return ((float) index * ((getWidth()) / ((float) max - 1.0f)));
}
}