I'm currently building an app in SwiftUI that needs to show some charts. Swift Charts has been quite helpful but I can't seem to set the domain of the chart just how I want it. The following chart display events that happend in a week and I want to display every hour even if nothing happened there, and I want the hours to go up, instead of the default where the new day start at the top of the chart.
struct ChartView: View {
let dataSets: [someData]
// simplified date init
let datemin = date(year:2025, month: 2, day: 24)
let datemax = date(year:2025, month: 2, day: 25)
var body: some View {
Chart(dataset) { data in
PointMark(x: .value("Day", data.day, unit: .weekday),
y: .value("Time", data.time, unit: .minute))
}
}
// The focus is on this line
.chartYScale(domain: .automatic(reversed: true))
}
}
The .chartYScale(domain:)
modifier allows me to set the start of the day at the bottom, but still ignores some hours.
If I instead use this
Chart(...) { ... }
.chartYScale(domain: datemin...datemax)
The chart display every hour of the day, but now the start of the day is at the bottom. I can't seem to find a way to get both things at the same time. if I add both modifiers only the first one get applied while the other ignored.
Any solutions or workarounds?