- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGraphView.java
More file actions
Latest commit
131 lines (119 loc) · 3.66 KB
/
Copy pathGraphView.java
File metadata and controls
131 lines (119 loc) · 3.66 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
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
packagearnodenhond.graphviewdemo;
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.Paint.Align;
importandroid.view.View;
/**
* GraphView creates a scaled line or bar graph with x and y axis labels.
* @author Arno den Hond
*
*/
publicclassGraphViewextendsView {
publicstaticbooleanBAR = true;
publicstaticbooleanLINE = false;
privatePaintpaint;
privatefloat[] values;
privateString[] horlabels;
privateString[] verlabels;
privateStringtitle;
privatebooleantype;
publicGraphView(Contextcontext, float[] values, Stringtitle, String[] horlabels, String[] verlabels, booleantype) {
super(context);
if (values == null)
values = newfloat[0];
else
this.values = values;
if (title == null)
title = "";
else
this.title = title;
if (horlabels == null)
this.horlabels = newString[0];
else
this.horlabels = horlabels;
if (verlabels == null)
this.verlabels = newString[0];
else
this.verlabels = verlabels;
this.type = type;
paint = newPaint();
}
@Override
protectedvoidonDraw(Canvascanvas) {
floatborder = 20;
floathorstart = border * 2;
floatheight = getHeight();
floatwidth = getWidth() - 1;
floatmax = getMax();
floatmin = getMin();
floatdiff = max - min;
floatgraphheight = height - (2 * border);
floatgraphwidth = width - (2 * border);
paint.setTextAlign(Align.LEFT);
intvers = verlabels.length - 1;
for (inti = 0; i < verlabels.length; i++) {
paint.setColor(Color.DKGRAY);
floaty = ((graphheight / vers) * i) + border;
canvas.drawLine(horstart, y, width, y, paint);
paint.setColor(Color.WHITE);
canvas.drawText(verlabels[i], 0, y, paint);
}
inthors = horlabels.length - 1;
for (inti = 0; i < horlabels.length; i++) {
paint.setColor(Color.DKGRAY);
floatx = ((graphwidth / hors) * i) + horstart;
canvas.drawLine(x, height - border, x, border, paint);
paint.setTextAlign(Align.CENTER);
if (i==horlabels.length-1)
paint.setTextAlign(Align.RIGHT);
if (i==0)
paint.setTextAlign(Align.LEFT);
paint.setColor(Color.WHITE);
canvas.drawText(horlabels[i], x, height - 4, paint);
}
paint.setTextAlign(Align.CENTER);
canvas.drawText(title, (graphwidth / 2) + horstart, border - 4, paint);
if (max != min) {
paint.setColor(Color.LTGRAY);
if (type == BAR) {
floatdatalength = values.length;
floatcolwidth = (width - (2 * border)) / datalength;
for (inti = 0; i < values.length; i++) {
floatval = values[i] - min;
floatrat = val / diff;
floath = graphheight * rat;
canvas.drawRect((i * colwidth) + horstart, (border - h) + graphheight, ((i * colwidth) + horstart) + (colwidth - 1), height - (border - 1), paint);
}
} else {
floatdatalength = values.length;
floatcolwidth = (width - (2 * border)) / datalength;
floathalfcol = colwidth / 2;
floatlasth = 0;
for (inti = 0; i < values.length; i++) {
floatval = values[i] - min;
floatrat = val / diff;
floath = graphheight * rat;
if (i > 0)
canvas.drawLine(((i - 1) * colwidth) + (horstart + 1) + halfcol, (border - lasth) + graphheight, (i * colwidth) + (horstart + 1) + halfcol, (border - h) + graphheight, paint);
lasth = h;
}
}
}
}
privatefloatgetMax() {
floatlargest = Integer.MIN_VALUE;
for (inti = 0; i < values.length; i++)
if (values[i] > largest)
largest = values[i];
returnlargest;
}
privatefloatgetMin() {
floatsmallest = Integer.MAX_VALUE;
for (inti = 0; i < values.length; i++)
if (values[i] < smallest)
smallest = values[i];
returnsmallest;
}
}