diff --git a/wear/src/main/java/info/nightscout/androidaps/watchfaces/BgGraphBuilder.java b/wear/src/main/java/info/nightscout/androidaps/watchfaces/BgGraphBuilder.java index 6f00f42d3b..aa78e41856 100644 --- a/wear/src/main/java/info/nightscout/androidaps/watchfaces/BgGraphBuilder.java +++ b/wear/src/main/java/info/nightscout/androidaps/watchfaces/BgGraphBuilder.java @@ -388,41 +388,42 @@ public class BgGraphBuilder { public Axis xAxis() { final boolean is24 = DateFormat.is24HourFormat(context); + SimpleDateFormat timeFormat = new SimpleDateFormat(is24? "HH" : "h a"); + timeFormat.setTimeZone(TimeZone.getDefault()); + long timeNow = System.currentTimeMillis(); + Axis xAxis = new Axis(); xAxis.setAutoGenerated(false); List xAxisValues = new ArrayList(); - GregorianCalendar now = new GregorianCalendar(); - GregorianCalendar today = new GregorianCalendar(now.get(Calendar.YEAR), now.get(Calendar.MONTH), now.get(Calendar.DAY_OF_MONTH)); - SimpleDateFormat timeFormat = new SimpleDateFormat(is24? "HH" : "h a"); - timeFormat.setTimeZone(TimeZone.getDefault()); - long start_hour = today.getTime().getTime(); - long timeNow = System.currentTimeMillis(); - long endTime = getPredictionEndTime(); + //get the time-tick at the full hour after start_time + GregorianCalendar startGC = new GregorianCalendar(); + startGC.setTimeInMillis(start_time); + startGC.set(Calendar.MILLISECOND, 0); + startGC.set(Calendar.SECOND, 0); + startGC.set(Calendar.MINUTE, 0); + startGC.add(Calendar.HOUR, 1); + long start_hour = startGC.getTimeInMillis(); - for (int l = 0; l <= 24; l++) { - if ((start_hour + (60000 * 60 * (l))) < endTime) { - if ((start_hour + (60000 * 60 * (l + 1))) >= endTime) { - endHour = start_hour + (60000 * 60 * (l)); - l = 25; - } - } - } //Display current time on the graph SimpleDateFormat longTimeFormat = new SimpleDateFormat(is24? "HH:mm" : "h:mm a"); xAxisValues.add(new AxisValue(fuzz(timeNow), (longTimeFormat.format(timeNow)).toCharArray())); - //Add whole hours endTime the axis (as long as they are more than 15 mins away from the current time) - for (int l = 0; l <= 24; l++) { - long timestamp = endHour - (60000 * 60 * l); - if((timestamp - endTime < 0) && (timestamp > start_time)) { - if(Math.abs(timestamp - timeNow) > (1000 * 60 * 8 * timespan)){ - xAxisValues.add(new AxisValue(fuzz(timestamp), (timeFormat.format(timestamp)).toCharArray())); - }else { - xAxisValues.add(new AxisValue(fuzz(timestamp), "".toCharArray())); - } + long hourTick = start_hour; + + // add all full hours within the timeframe + while (hourTick < end_time){ + if(Math.abs(hourTick - timeNow) > (1000 * 60 * 8 * timespan)){ + xAxisValues.add(new AxisValue(fuzz(hourTick), (timeFormat.format(hourTick)).toCharArray())); + } else { + //don't print hour label if too close to now to avoid overlaps + xAxisValues.add(new AxisValue(fuzz(hourTick), "".toCharArray())); } + + //increment by one hour + hourTick += 60*60*1000; } + xAxis.setValues(xAxisValues); xAxis.setTextSize(10); xAxis.setHasLines(true);