format method

  1. @override
List<String> format(
  1. List<DateTime> tickValues,
  2. Map<DateTime, String> cache, {
  3. num? stepSize,
})
override

Formats a list of tick values.

Implementation

@override
List<String> format(List<DateTime> tickValues, Map<DateTime, String> cache,
    {num? stepSize}) {
  final tickLabels = <String>[];
  if (tickValues.isEmpty) {
    return tickLabels;
  }

  // Find the formatter that is the largest interval that has enough
  // resolution to describe the difference between ticks. If no such formatter
  // exists pick the highest res one.
  var formatter = _timeFormatters[_timeFormatters.keys.first]!;
  var formatterFound = false;
  if (_timeFormatters.keys.first == ANY) {
    formatterFound = true;
  } else {
    final minTimeBetweenTicks = stepSize?.toInt() ?? 0;

    // TODO: Skip the formatter if the formatter's step size is
    // smaller than the minimum step size of the data.

    var keys = _timeFormatters.keys.iterator;
    while (keys.moveNext() && !formatterFound) {
      if (keys.current > minTimeBetweenTicks) {
        formatterFound = true;
      } else {
        formatter = _timeFormatters[keys.current]!;
      }
    }
  }

  // Format the ticks.
  final tickValuesIt = tickValues.iterator;

  var tickValue = (tickValuesIt..moveNext()).current;
  var prevTickValue = tickValue;
  tickLabels.add(formatter.formatFirstTick(tickValue));

  while (tickValuesIt.moveNext()) {
    tickValue = tickValuesIt.current;
    if (formatter.isTransition(tickValue, prevTickValue)) {
      tickLabels.add(formatter.formatTransitionTick(tickValue));
    } else {
      tickLabels.add(formatter.formatSimpleTick(tickValue));
    }
    prevTickValue = tickValue;
  }

  return tickLabels;
}