From 4f43b5ac7112ec0a373224fe06a4fb36b4ac22ad Mon Sep 17 00:00:00 2001 From: TheMoonThatRises <58153205+TheMoonThatRises@users.noreply.github.com> Date: Tue, 29 Oct 2024 23:11:56 -0600 Subject: [PATCH] fix attendance barcharts --- PortalBook/Views/Tabs/AttendanceView.swift | 62 ++++++++++++++++++---- 1 file changed, 51 insertions(+), 11 deletions(-) diff --git a/PortalBook/Views/Tabs/AttendanceView.swift b/PortalBook/Views/Tabs/AttendanceView.swift index 9fbe69e..d8786c9 100644 --- a/PortalBook/Views/Tabs/AttendanceView.swift +++ b/PortalBook/Views/Tabs/AttendanceView.swift @@ -21,7 +21,7 @@ enum AttendanceType: String { struct AttendanceBar: Identifiable { var color: Color // var type: AttendanceType - var period: Int + var period: String var count: Int var id = UUID() } @@ -36,18 +36,34 @@ struct AttendanceView: View { @State var refresh = false @State var attendanceData: [AttendanceBar] = [] + @State var foregroundStyleColors: [String: Color] = [:] + var body: some View { + let item: KeyValuePairs = [:] + NavigationStack { if !attendanceData.isEmpty { Chart { ForEach(attendanceData) { data in - BarMark( - x: .value("Period", data.period), - y: .value("Count", data.count) - ) - .foregroundStyle(by: .value("Color", data.color.description)) + if data.count > 0 { + BarMark( + x: .value("Period", data.period), + y: .value("Count", data.count) + ) + .foregroundStyle(data.color) + .annotation(position: .overlay, alignment: .center) { + Text("\(data.count)") + .foregroundColor(.white) + } + } } } + .chartForegroundStyleScale(item) + .aspectRatio(1, contentMode: .fit) + .padding() + .refreshable { + refresh.toggle() + } } else if loadingMessage == .empty { Text("Unable to load attendance") } @@ -88,35 +104,59 @@ struct AttendanceView: View { private func populateAttendance() { if let attendance = dataCache.attendance { + attendanceData.removeAll() + for excused in attendance.totalExcused { attendanceData.append(.init(color: .excused, - period: excused.period, + period: String(excused.period), count: excused.total)) + + if excused.total > 0 { + foregroundStyleColors["Excused"] = Color.excused + } } for tardy in attendance.totalTardies { attendanceData.append(.init(color: .tardies, - period: tardy.period, + period: String(tardy.period), count: tardy.total)) + + if tardy.total > 0 { + foregroundStyleColors["Tardy"] = Color.tardies + } } for unexcused in attendance.totalUnexcused { attendanceData.append(.init(color: .unexcused, - period: unexcused.period, + period: String(unexcused.period), count: unexcused.total)) + + if unexcused.total > 0 { + foregroundStyleColors["Unexcused"] = Color.unexcused + } } for activity in attendance.totalActivities { attendanceData.append(.init(color: .activities, - period: activity.period, + period: String(activity.period), count: activity.total)) + + if activity.total > 0 { + foregroundStyleColors["Activity"] = Color.activities + } } for unexcusedTardy in attendance.totalUnexcusedTardies { attendanceData.append(.init(color: .unexcusedTardies, - period: unexcusedTardy.period, + period: String(unexcusedTardy.period), count: unexcusedTardy.total)) + + if unexcusedTardy.total > 0 { + foregroundStyleColors["Unexcused Tardy"] = Color.unexcusedTardies + } } + + attendanceData.sort(by: { $0.period < $1.period }) } } }