Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added value labels for the currently selected date

  • Loading branch information...
commit 1e61cb18e79b522bbc1b2f0c51e3561288a88b80 1 parent 27246e9
Alexander Konstantinov authored

Showing 1 changed file with 78 additions and 36 deletions. Show diff stats Hide diff stats

  1. +78 36 src/com/k10v/goaltracker/GraphDrawer.java
114 src/com/k10v/goaltracker/GraphDrawer.java
@@ -18,7 +18,7 @@
18 18
19 19 private final int mLabelTextMargin = 5;
20 20 private final int mLabelTextSize = 16;
21   - private final int mTickSize = 4;
  21 + private final int mTickSize = 5;
22 22
23 23 private Context context;
24 24
@@ -201,48 +201,48 @@ private void drawPointerAndLabels() {
201 201 }
202 202
203 203 // Find the dates below the pointers
204   - int day1N = getDayNByCanvasX(Math.round(mPointer1X));
205   - int day2N = getDayNByCanvasX(Math.round(mPointer2X));
  204 + int startDayN = getDayNByCanvasX(Math.round(mPointer1X));
  205 + int finishDayN = getDayNByCanvasX(Math.round(mPointer2X));
206 206
207 207 // Make sure day 1 is earlier than day 2
208   - if (day2N < day1N) {
209   - int temp = day1N;
210   - day1N = day2N;
211   - day2N = temp;
  208 + if (finishDayN < startDayN) {
  209 + int temp = startDayN;
  210 + startDayN = finishDayN;
  211 + finishDayN = temp;
212 212 }
213 213
214 214 // Check is pointer 1 is inside the graph
215   - if (day1N <= 0 || mDateRange < day1N) {
  215 + if (startDayN <= 0 || mDateRange < startDayN) {
216 216 return;
217 217 }
218 218
219 219 // Make sure pointer 2 is inside the graph
220   - if (day2N <= 0) {
221   - day2N = 1;
  220 + if (finishDayN <= 0) {
  221 + finishDayN = 1;
222 222 }
223   - if (mDateRange < day2N) {
224   - day2N = mDateRange;
  223 + if (mDateRange < finishDayN) {
  224 + finishDayN = mDateRange;
225 225 }
226 226
227 227 Calendar cal = Calendar.getInstance();
228 228 Date date = mMinDate;
229 229
230 230 float value = mStartValue;
231   - float value1 = value;
232   - float value2 = value;
  231 + float startValue = value;
  232 + float finishValue = value;
233 233
234   - for (int i = 1; i <= day2N; i++) {
  234 + for (int i = 1; i <= finishDayN; i++) {
235 235
236 236 if (mValues.containsKey(date)) {
237 237 value = mValues.get(date);
238 238 }
239 239
240   - if (i == day1N - 1) {
241   - value1 = value;
  240 + if (i == startDayN - 1) {
  241 + startValue = value;
242 242 }
243 243
244   - if (i == day2N) {
245   - value2 = value;
  244 + if (i == finishDayN) {
  245 + finishValue = value;
246 246 }
247 247
248 248 // Next date
@@ -251,34 +251,72 @@ private void drawPointerAndLabels() {
251 251 date = cal.getTime();
252 252 }
253 253
  254 + float valueDiff = finishValue - startValue;
  255 +
254 256 // X-coordinates of two vertical lines
255   - int x1 = getCanvasXByDayN(day1N - 1);
256   - int x2 = getCanvasXByDayN(day2N);
  257 + int xMin = getCanvasXByDayN(startDayN - 1);
  258 + int xMax = getCanvasXByDayN(finishDayN);
257 259
258 260 // Y-coordinates of two horizontal lines
259   - int y1, y2;
260   - boolean isNegativeDifference = false;
261   - if (value1 <= value2) {
262   - y1 = getCanvasYByValue(value2);
263   - y2 = getCanvasYByValue(value1);
  261 + int yMin, yMax;
  262 + if (0 <= valueDiff) {
  263 + yMin = getCanvasYByValue(finishValue);
  264 + yMax = getCanvasYByValue(startValue);
264 265 } else {
265   - y1 = getCanvasYByValue(value1);
266   - y2 = getCanvasYByValue(value2);
267   - isNegativeDifference = true;
  266 + yMin = getCanvasYByValue(startValue);
  267 + yMax = getCanvasYByValue(finishValue);
268 268 }
269 269
270 270 // Vertical fill
271   - mCanvas.drawRect(x1, mCanvasYMin, x2 + 1, mCanvasYMax + 1, mPaintSelectedDate);
  271 + mCanvas.drawRect(xMin, mCanvasYMin, xMax + 1, mCanvasYMax + 1, mPaintSelectedDate);
272 272
273 273 // Horizontal fill
274   - Paint paint = isNegativeDifference ? mPaintSelectedValueNegative : mPaintSelectedValue;
275   - mCanvas.drawRect(mCanvasXMin, y1, mCanvasXMax + 1, y2 + 1, paint);
  274 + Paint paint = valueDiff < 0 ? mPaintSelectedValueNegative : mPaintSelectedValue;
  275 + mCanvas.drawRect(mCanvasXMin, yMin, mCanvasXMax + 1, yMax + 1, paint);
276 276
277   - drawLabels();
278   - }
  277 + // Draw pointer's labels
279 278
280   - private void drawLabels() {
281   - drawLabels(true, true, true, true);
  279 + DecimalFormat format = new DecimalFormat("#.#####");
  280 +
  281 + int labelX = mCanvasXMin + mTickSize + mLabelTextMargin;
  282 +
  283 + String topLabel = null;
  284 + String bottomLabel = null;
  285 +
  286 +
  287 + if (0 < valueDiff) {
  288 + bottomLabel = format.format(startValue);
  289 + topLabel = format.format(finishValue) + " (+" + format.format(valueDiff) + ")";
  290 + } else if (valueDiff < 0) {
  291 + topLabel = format.format(startValue);
  292 + // "\u2212" is a proper "minus" sign
  293 + bottomLabel = format.format(finishValue) + " (\u2212" + format.format(-valueDiff) + ")";
  294 + } else {
  295 + topLabel = format.format(finishValue);
  296 + // Show only one label if value has not changed
  297 + }
  298 +
  299 + // Calculate label's positions
  300 + Rect bounds = new Rect();
  301 + mPaintLabels.getTextBounds("0", 0, 1, bounds);
  302 + int topLabelY = yMin - bounds.bottom - mLabelTextMargin;
  303 + int bottomLabelY = yMax - bounds.top + mLabelTextMargin;
  304 +
  305 + // Top label
  306 + if (topLabel != null) {
  307 + mCanvas.drawText(topLabel, labelX, topLabelY, mPaintLabels);
  308 + }
  309 +
  310 + // Bottom label
  311 + if (bottomLabel != null) {
  312 + mCanvas.drawText(bottomLabel, labelX, bottomLabelY, mPaintLabels);
  313 + }
  314 +
  315 + drawHorizontalTickForValue(finishValue);
  316 + drawHorizontalTickForValue(startValue);
  317 +
  318 + // TODO: temporarily disabled
  319 + // drawLabels();
282 320 }
283 321
284 322 private void drawLabels(
@@ -363,6 +401,10 @@ private void drawLabels(
363 401 }
364 402 }
365 403
  404 + private void drawLabels() {
  405 + drawLabels(true, true, true, true);
  406 + }
  407 +
366 408 // TODO: Move this utility method outside?
367 409 private String getFormattedDate(Date date) {
368 410

0 comments on commit 1e61cb1

Please sign in to comment.
Something went wrong with that request. Please try again.