Skip to content

Commit

Permalink
fix attendance barcharts
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMoonThatRises committed Oct 30, 2024
1 parent d1b3607 commit 4f43b5a
Showing 1 changed file with 51 additions and 11 deletions.
62 changes: 51 additions & 11 deletions PortalBook/Views/Tabs/AttendanceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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<String, Color> = [:]

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")
}
Expand Down Expand Up @@ -88,35 +104,59 @@ struct AttendanceView: View {

private func populateAttendance() {

Check failure on line 105 in PortalBook/Views/Tabs/AttendanceView.swift

View workflow job for this annotation

GitHub Actions / swiftlint

Cyclomatic Complexity Violation: Function should have complexity 10 or less; currently complexity is 11 (cyclomatic_complexity)
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 })
}
}
}

0 comments on commit 4f43b5a

Please sign in to comment.