Skip to content

Instantly share code, notes, and snippets.

@bawantha
Created June 15, 2022 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bawantha/08afb5cc7c530e16b43eedc769af1848 to your computer and use it in GitHub Desktop.
Save bawantha/08afb5cc7c530e16b43eedc769af1848 to your computer and use it in GitHub Desktop.
chart_data_process
/// WIdget input data
/// ==================
/// List of [KirkseyDeviceActivationRecord]
List<KirkseyDeviceActivation> deviceActivations = [];
/// unitId
/// set as 7 for demo
int unitId = 7;
/// Date of selected chart
/// set as today for demo
DateTime date = DateTime.now();
/// to save processed data
Map<DateTime, double> dataMapForChart = {};
/// ===========================================================
/// add mock data for [deviceActivations] to testing
/// purpose
/// ==========================
for (int i = 0; i < 10; i++) {
deviceActivations.add(
KirkseyDeviceActivation(
deviceType: 'Hybrid',
groupId: 2,
location: 'Conference Area',
offDateTime: DateTime(2022, 06, 15, 12 - i, 15, 02),
onDateTime: DateTime(2022, 06, 15, 12 - i, 02, 02),
roomName: 'Conference Room',
unitId: 7,
timeOn: 201,
),
);
}
/// filter out the [deviceActivations] where unitId == [unitId]
/// in this demo unitId is set as 7
List<KirkseyDeviceActivation> filteredDeviceActivations =
deviceActivations
.where((element) => element.unitId == unitId)
.toList();
/// filter out the [deviceActivations] where onDateTime and offDateTime are in the same day
/// as [date] in this demo date is set as today
List<KirkseyDeviceActivation> filteredDeviceActivationsForDate =
filteredDeviceActivations
.where((element) =>
element.onDateTime.day == date.day &&
element.offDateTime.day == date.day)
.toList();
/// Calculate DosageAchieved
double dosageArchivedForDevice(KirkseyDeviceActivation device) {
/// switch for [device.deviceType]
/// and return the dosageAchieved
switch (device.deviceType) {
case 'Hybrid':
return device.timeOn * .011;
case '2x4':
return device.timeOn * .033;
case '2x2':
return device.timeOn * .022;
default:
return 0;
}
}
/// each item in [filteredDeviceActivationsForDate] is processed to get the
/// dosageAchieved and added to [dataMapForChart]
for (KirkseyDeviceActivation kirkseyDeviceActivation
in filteredDeviceActivationsForDate) {
/// get the dosageAchieved for each device
/// and add it to the [dataMapForChart]
/// with the key as [kirkseyDeviceActivation.onDateTime] and [kirkseyDeviceActivation.offDateTime]
/// value as [dosageArchivedForDevice(kirkseyDeviceActivation)]
/// ==========================
dataMapForChart[kirkseyDeviceActivation.onDateTime] =
dataMapForChart[kirkseyDeviceActivation.offDateTime] =
dosageArchivedForDevice(kirkseyDeviceActivation);
}
/// ===========================================================
/// return the [dataMapForChart]
/// for map
/// ==========================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment