/
GraphDrawer.java
468 lines (368 loc) · 13.8 KB
/
GraphDrawer.java
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
package com.k10v.goaltracker;
import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
/**
* Class responsible for drawing a graph on the canvas
*/
public class GraphDrawer {
private final int mLabelTextMargin = 5;
private final int mLabelTextSize = 16;
private final int mTickSize = 5;
private Context context;
private Canvas mCanvas;
private int mCanvasXMin;
private int mCanvasXMax;
private int mCanvasYMin;
private int mCanvasYMax;
private HashMap<Date, Float> mValues;
private float mMinValue;
private float mMaxValue;
private float mStartValue;
private float mLastValue;
private Date mMinDate;
private Date mMaxDate;
private int mDateRange;
private boolean mIsTouched = false;
private float mPointer1X = 0;
private float mPointer1Y = 0;
private float mPointer2X = 0;
private float mPointer2Y = 0;
private Paint mPaintVerticalGrid;
private Paint mPaintAxes;
private Paint mPaintCurrentValue;
private Paint mPaintProgress;
private Paint mPaintSelectedDate;
private Paint mPaintSelectedValue;
private Paint mPaintSelectedValueNegative;
private Paint mPaintLabels;
public GraphDrawer(Context c) {
context = c;
setupPaints();
}
private void setupPaints() {
mPaintVerticalGrid = new Paint();
mPaintVerticalGrid.setARGB(255, 32, 32, 32);
mPaintAxes = new Paint();
mPaintAxes.setARGB(255, 255, 255, 255);
mPaintCurrentValue = new Paint();
mPaintCurrentValue.setARGB(255, 0, 64, 0);
mPaintProgress = new Paint();
mPaintProgress.setARGB(255, 64, 255, 64);
mPaintProgress.setStrokeWidth(1.5f);
mPaintProgress.setAntiAlias(true);
mPaintSelectedDate = new Paint();
mPaintSelectedDate.setARGB(255, 128, 128, 128);
mPaintSelectedValue = new Paint();
mPaintSelectedValue.setARGB(192, 64, 128, 64);
mPaintSelectedValueNegative = new Paint();
mPaintSelectedValueNegative.setARGB(192, 192, 96, 96);
mPaintLabels = new Paint();
mPaintLabels.setARGB(255, 255, 255, 255);
mPaintLabels.setTextSize(mLabelTextSize);
mPaintLabels.setFakeBoldText(true);
mPaintLabels.setAntiAlias(true);
mPaintLabels.setShadowLayer(5, 0, 0, Color.BLACK);
}
public void setCanvas(Canvas canvas) {
mCanvas = canvas;
}
public void setCanvasRectangle(int xMin, int yMin, int xMax, int yMax) {
mCanvasXMin = xMin;
mCanvasYMin = yMin;
mCanvasXMax = xMax;
mCanvasYMax = yMax - mLabelTextSize - 2 * mLabelTextMargin;
}
public void setValues(HashMap<Date, Float> values) {
mValues = values;
}
public void setValueRange(float minValue, float maxValue) {
mMinValue = minValue;
mMaxValue = maxValue;
}
public void setDateRange(Date minDate, Date maxDate) {
mMinDate = minDate;
mMaxDate = maxDate;
// TODO: Use JodaTime library for dates instead?
mDateRange = 1 + Math.round(
(maxDate.getTime() - minDate.getTime()) /
(24 * 60 * 60 * 1000f));
}
public void setStartValue(float value) {
mStartValue = value;
}
public void setLastValue(float value) {
mLastValue = value;
}
public void setPointer(boolean isTouched, float x1, float y1, float x2, float y2) {
mIsTouched = isTouched;
mPointer1X = x1;
mPointer1Y = y1;
mPointer2X = x2;
mPointer2Y = y2;
}
public void draw() {
mCanvas.drawColor(Color.BLACK);
drawVerticalGrid();
drawCurrentValue();
drawPointerAndLabels();
drawAxes();
drawProgress();
}
private void drawVerticalGrid() {
for (int i = 0; i <= mDateRange; i++) {
drawVerticalLineForDayN(i, mPaintVerticalGrid);
}
}
private void drawAxes() {
drawHorizontalLineForValue(mStartValue, mPaintAxes);
drawVerticalLine(mCanvasXMin, mPaintAxes);
}
private void drawCurrentValue() {
drawHorizontalLineForValue(mLastValue, mPaintCurrentValue);
}
private void drawProgress() {
Calendar cal = Calendar.getInstance();
Date date = mMinDate;
float value = mStartValue;
int x0 = getCanvasXByDayN(0);
int y0 = getCanvasYByValue(mStartValue);
for (int i = 1; i <= mDateRange; i++) {
// TODO: probably should use dateString as a key instead?
if (mValues.containsKey(date)) {
value = mValues.get(date);
}
int x = getCanvasXByDayN(i);
int y = getCanvasYByValue(value);
// Draw progress
mCanvas.drawLine(x0, y0, x, y, mPaintProgress);
// Next date
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 1);
date = cal.getTime();
x0 = x;
y0 = y;
}
}
private void drawPointerAndLabels() {
if (!mIsTouched) {
drawLabels();
return;
}
// Find the dates below the pointers
int startDayN = getDayNByCanvasX(Math.round(mPointer1X));
int finishDayN = getDayNByCanvasX(Math.round(mPointer2X));
// Make sure day 1 is earlier than day 2
if (finishDayN < startDayN) {
int temp = startDayN;
startDayN = finishDayN;
finishDayN = temp;
}
// Check is pointer 1 is inside the graph
if (startDayN <= 0 || mDateRange < startDayN) {
return;
}
// Make sure pointer 2 is inside the graph
if (finishDayN <= 0) {
finishDayN = 1;
}
if (mDateRange < finishDayN) {
finishDayN = mDateRange;
}
Calendar cal = Calendar.getInstance();
Date date = mMinDate;
float value = mStartValue;
float startValue = value;
float finishValue = value;
for (int i = 1; i <= finishDayN; i++) {
if (mValues.containsKey(date)) {
value = mValues.get(date);
}
if (i == startDayN - 1) {
startValue = value;
}
if (i == finishDayN) {
finishValue = value;
}
// Next date
cal.setTime(date);
cal.add(Calendar.DAY_OF_MONTH, 1);
date = cal.getTime();
}
float valueDiff = finishValue - startValue;
// X-coordinates of two vertical lines
int xMin = getCanvasXByDayN(startDayN - 1);
int xMax = getCanvasXByDayN(finishDayN);
// Y-coordinates of two horizontal lines
int yMin, yMax;
if (0 <= valueDiff) {
yMin = getCanvasYByValue(finishValue);
yMax = getCanvasYByValue(startValue);
} else {
yMin = getCanvasYByValue(startValue);
yMax = getCanvasYByValue(finishValue);
}
// Vertical fill
mCanvas.drawRect(xMin, mCanvasYMin, xMax + 1, mCanvasYMax + 1, mPaintSelectedDate);
// Horizontal fill
Paint paint = valueDiff < 0 ? mPaintSelectedValueNegative : mPaintSelectedValue;
mCanvas.drawRect(mCanvasXMin, yMin, mCanvasXMax + 1, yMax + 1, paint);
// Draw pointer's labels
DecimalFormat format = new DecimalFormat("#.#####");
int labelX = mCanvasXMin + mTickSize + mLabelTextMargin;
String topLabel = null;
String bottomLabel = null;
if (0 < valueDiff) {
bottomLabel = format.format(startValue);
topLabel = format.format(finishValue) + " (+" + format.format(valueDiff) + ")";
} else if (valueDiff < 0) {
topLabel = format.format(startValue);
// "\u2212" is a proper "minus" sign
bottomLabel = format.format(finishValue) + " (\u2212" + format.format(-valueDiff) + ")";
} else {
topLabel = format.format(finishValue);
// Show only one label if value has not changed
}
// Calculate label's positions
Rect bounds = new Rect();
mPaintLabels.getTextBounds("0", 0, 1, bounds);
int topLabelY = yMin - bounds.bottom - mLabelTextMargin;
int bottomLabelY = yMax - bounds.top + mLabelTextMargin;
// Top label
if (topLabel != null) {
mCanvas.drawText(topLabel, labelX, topLabelY, mPaintLabels);
}
// Bottom label
if (bottomLabel != null) {
mCanvas.drawText(bottomLabel, labelX, bottomLabelY, mPaintLabels);
}
drawHorizontalTickForValue(finishValue);
drawHorizontalTickForValue(startValue);
// TODO: temporarily disabled
// drawLabels();
}
private void drawLabels(
boolean drawMinValue, boolean drawMaxValue,
boolean drawMinDate, boolean drawMaxDate) {
Rect bounds = new Rect();
String text;
DecimalFormat format = new DecimalFormat("#.#####");
// Min value
if (drawMinValue) {
text = format.format(mMinValue);
mPaintLabels.getTextBounds(text, 0, text.length(), bounds);
mCanvas.drawText(text,
mCanvasXMin + mLabelTextMargin,
mCanvasYMax - bounds.bottom - mLabelTextMargin,
mPaintLabels);
drawHorizontalTickForValue(mMinValue);
}
// Max value
if (drawMaxValue) {
text = format.format(mMaxValue);
mPaintLabels.getTextBounds(text, 0, text.length(), bounds);
mCanvas.drawText(text,
mCanvasXMin + mTickSize + mLabelTextMargin,
mCanvasYMin - bounds.top,
mPaintLabels);
drawHorizontalTickForValue(mMaxValue);
}
// Min date
if (drawMinDate) {
text = getFormattedDate(mMinDate);
mPaintLabels.getTextBounds(text, 0, text.length(), bounds);
// Try to center the label against date column, but adjust label's
// position if there is not enough space
int x = (
(getCanvasXByDayN(0) + getCanvasXByDayN(1)) -
(bounds.right - bounds.left)
) / 2;
if (x < 0) {
x = mCanvasXMin;
}
mCanvas.drawText(text,
x,
mCanvasYMax - bounds.top + mTickSize + mLabelTextMargin,
mPaintLabels);
drawVerticalTickForDayN(0);
drawVerticalTickForDayN(1);
}
// Max date
if (drawMaxDate) {
text = getFormattedDate(mMaxDate);
mPaintLabels.getTextBounds(text, 0, text.length(), bounds);
// Try to center the label against date column, but adjust label's
// position if there is not enough space
int w = bounds.right - bounds.left;
int x = (
(getCanvasXByDayN(mDateRange - 1) + getCanvasXByDayN(mDateRange)) -
(bounds.right + bounds.left)
) / 2;
if (mCanvasXMax < x + w) {
x = mCanvasXMax - bounds.right;
}
mCanvas.drawText(text,
x,
mCanvasYMax - bounds.top + mTickSize + mLabelTextMargin,
mPaintLabels);
drawVerticalTickForDayN(mDateRange - 1);
drawVerticalTickForDayN(mDateRange);
}
}
private void drawLabels() {
drawLabels(true, true, true, true);
}
// TODO: Move this utility method outside?
private String getFormattedDate(Date date) {
Calendar today = Calendar.getInstance();
today.set(Calendar.HOUR_OF_DAY, 0);
today.set(Calendar.MINUTE, 0);
today.set(Calendar.SECOND, 0);
today.set(Calendar.MILLISECOND, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
if (calendar.equals(today)) {
return context.getString(R.string.date_today);
}
String format = calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) ? "MMM d" : "MMM d, yyyy";
return android.text.format.DateFormat.format(format, date).toString();
}
private int getCanvasYByValue(float value) {
return mCanvasYMax - Math.round(
(mCanvasYMax - mCanvasYMin) *
(value - mMinValue) /
(mMaxValue - mMinValue));
}
private int getCanvasXByDayN(int n) {
return mCanvasXMin + (mCanvasXMax - mCanvasXMin) * n / mDateRange;
}
private int getDayNByCanvasX(float x) {
return (int) Math.ceil((x - mCanvasXMin) * mDateRange / (mCanvasXMax - mCanvasXMin));
}
private void drawHorizontalLine(int y, Paint paint) {
mCanvas.drawLine(mCanvasXMin, y, mCanvasXMax + 1, y, paint);
}
private void drawHorizontalLineForValue(float value, Paint paint) {
drawHorizontalLine(getCanvasYByValue(value), paint);
}
private void drawVerticalLine(int x, Paint paint) {
mCanvas.drawLine(x, mCanvasYMin, x, mCanvasYMax + 1, paint);
}
private void drawVerticalLineForDayN(int n, Paint paint) {
drawVerticalLine(getCanvasXByDayN(n), paint);
}
private void drawVerticalTickForDayN(int n) {
int x = getCanvasXByDayN(n);
mCanvas.drawLine(x, mCanvasYMax, x, mCanvasYMax + mTickSize, mPaintAxes);
}
private void drawHorizontalTickForValue(float value) {
int y = getCanvasYByValue(value);
mCanvas.drawLine(mCanvasXMin, y, mCanvasXMin + mTickSize, y, mPaintAxes);
}
}