getTimeLabels method

Map<int, String> getTimeLabels(
  1. int minTime,
  2. int maxTime
)

Implementation

Map<int, String> getTimeLabels(int minTime, int maxTime ) {

  Map<int, String> ret = {};

  var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
  var days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
  var hour = 1000 * 60 *60;
  var hoursDiff = ((maxTime - minTime) / hour);

  var step = 0;
  List<int> allowedMonthDays = [];
  List<int> allowedMonths = [];
  var formatter = (DateTime date) => '';
  if (hoursDiff < 30) {
    step = hour * 4;
    formatter = (DateTime date) => this.forceTwoDigits(date.hour) + ':' + this.forceTwoDigits(date.minute);
  } else if (hoursDiff < (24 * 8)) {
    step = hour * 24;
    formatter = (DateTime date) => days[date.weekday - 1] + ' ' + date.day.toString();
  } else if (hoursDiff < (24 * 100)) {
    step = hour * 24 * 5;
    formatter = (DateTime date) => months[date.month] + ' ' + date.day.toString();
  } else {
    step = hour * 24;
    allowedMonthDays = [1];
    if (hoursDiff < (24 * 600)) {
      allowedMonths = [0, 2, 4, 6, 8, 10];
      formatter = (DateTime date) => months[date.month];
    } else if (hoursDiff < (24 * 1000)) {
      allowedMonths = [0, 6];
      formatter = (DateTime date) => months[date.month] + ' ' + date.year.toString();
    } else {
      allowedMonths = [0];
      formatter = (DateTime date) => date.year.toString();
    }
  }
  int stepTime = (minTime / step).ceil() * step;
  while (stepTime < maxTime) {
    var stepDate = new DateTime.fromMillisecondsSinceEpoch(stepTime);
    if (allowedMonthDays.isNotEmpty && (allowedMonthDays.indexOf(stepDate.day) == -1)) {
      stepTime += step;
      continue;
    }
    if (allowedMonths.isNotEmpty && (allowedMonths.indexOf(stepDate.month) == -1)) {
      stepTime += step;
      continue;
    }
    ret[stepTime] = formatter(stepDate);
    stepTime += step;
  }
  return ret;
}