diff --git a/lib/core/utils/time_series_mapper.dart b/lib/core/utils/time_series_mapper.dart index 5cc4ac0..3133012 100644 --- a/lib/core/utils/time_series_mapper.dart +++ b/lib/core/utils/time_series_mapper.dart @@ -37,7 +37,9 @@ class TimeSeriesMapper { required double Function(T) getValue, }) { final now = DateTime.now(); - final slots = List.filled(7, 0.0); + + final sums = List.filled(7, 0.0); + final counts = List.filled(7, 0); DateTime startOfWeek = now.subtract(Duration(days: now.weekday - 1)); startOfWeek = @@ -47,11 +49,16 @@ class TimeSeriesMapper { final time = getTime(item); if (time.isAfter(startOfWeek)) { - slots[time.weekday - 1] = getValue(item); + final index = time.weekday - 1; + sums[index] += getValue(item); + counts[index]++; } } - return slots; + return List.generate(7, (i) { + if (counts[i] == 0) return 0; + return sums[i] / counts[i]; + }); } /// ========================= @@ -64,17 +71,24 @@ class TimeSeriesMapper { }) { final now = DateTime.now(); final daysInMonth = DateTime(now.year, now.month + 1, 0).day; - final slots = List.filled(daysInMonth, 0.0); + + final sums = List.filled(daysInMonth, 0.0); + final counts = List.filled(daysInMonth, 0); for (final item in data) { final time = getTime(item); if (time.month == now.month && time.year == now.year) { - slots[time.day - 1] = getValue(item); + final index = time.day - 1; + sums[index] += getValue(item); + counts[index]++; } } - return slots; + return List.generate(daysInMonth, (i) { + if (counts[i] == 0) return 0; + return sums[i] / counts[i]; + }); } /// ========================= diff --git a/lib/screens/monitoring/wind_speed/blocs/wind_speed_bloc.dart b/lib/screens/monitoring/wind_speed/blocs/wind_speed_bloc.dart index 7544b4e..27f633c 100644 --- a/lib/screens/monitoring/wind_speed/blocs/wind_speed_bloc.dart +++ b/lib/screens/monitoring/wind_speed/blocs/wind_speed_bloc.dart @@ -111,28 +111,31 @@ class WindSpeedBloc extends Bloc { final history = state.history; - List updatedGraph; + List raw; if (event.period == "Minggu Ini") { - updatedGraph = TimeSeriesMapper.toWeekly( + raw = TimeSeriesMapper.toWeekly( data: history, getTime: (e) => e.timestamp, getValue: (e) => e.speed, ); } else if (event.period == "Bulan Ini") { - updatedGraph = TimeSeriesMapper.toMonthly( + raw = TimeSeriesMapper.toMonthly( data: history, getTime: (e) => e.timestamp, getValue: (e) => e.speed, ); } else { - updatedGraph = TimeSeriesMapper.toDaily( + raw = TimeSeriesMapper.toDaily( data: history, getTime: (e) => e.timestamp, getValue: (e) => e.speed, ); } + /// 🔥 baru di-smooth + final updatedGraph = TimeSeriesMapper.smooth(raw); + emit(state.copyWith( dailySpeeds: updatedGraph, isLoading: false,