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