getDailyMinMax method

Map<DateTime, (double, double)> getDailyMinMax(
  1. String dataType
)

Get daily min/max values for a specific data type. Returns a map where each date maps to (min, max) tuple.

Implementation

Map<DateTime, (double, double)> getDailyMinMax(String dataType) {
  final dailyValues = <DateTime, List<double>>{};

  for (final point in data) {
    final date = DateTime(point.time.year, point.time.month, point.time.day);

    switch (dataType) {
      case 'temperature':
        dailyValues.putIfAbsent(date, () => []).add(point.temperature);
      case 'humidity':
        if (point.humidity != null) {
          dailyValues
              .putIfAbsent(date, () => [])
              .add(point.humidity!.toDouble());
        }
      case 'wind_speed':
        if (point.windSpeed != null) {
          dailyValues.putIfAbsent(date, () => []).add(point.windSpeed!);
        }
      case 'precipitation':
        if (point.precipitation != null) {
          dailyValues.putIfAbsent(date, () => []).add(point.precipitation!);
        }
    }
  }

  return dailyValues.map((date, values) {
    if (values.isEmpty) return MapEntry(date, (0.0, 0.0));
    final min = values.reduce((a, b) => a < b ? a : b);
    final max = values.reduce((a, b) => a > b ? a : b);
    return MapEntry(date, (min, max));
  });
}