This commit is contained in:
kleponijo 2026-04-23 13:37:08 +07:00
parent 760f15db14
commit c9221e45f6
2 changed files with 27 additions and 10 deletions

View File

@ -37,7 +37,9 @@ class TimeSeriesMapper {
required double Function(T) getValue,
}) {
final now = DateTime.now();
final slots = List<double>.filled(7, 0.0);
final sums = List<double>.filled(7, 0.0);
final counts = List<int>.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<double>.filled(daysInMonth, 0.0);
final sums = List<double>.filled(daysInMonth, 0.0);
final counts = List<int>.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];
});
}
/// =========================

View File

@ -111,28 +111,31 @@ class WindSpeedBloc extends Bloc<WindSpeedEvent, WindSpeedState> {
final history = state.history;
List<double> updatedGraph;
List<double> 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,