I'm at my Witts end trying to figure out why charts is incorrectly labeling the days!
struct SunlightSupportBox: View {
@ObservedObject var viewModel = SunlightViewModel()
@EnvironmentObject var themeSettings: ThemeSettings
var sortedSunlightData: [SunlightData] {
viewModel.sunlightData.sorted(by: { $0.date < $1.date })
}
var body: some View {
VStack {
if !sortedSunlightData.isEmpty {
Chart {
ForEach(sortedSunlightData) { data in
BarMark(
x: .value("Day", formattedDate(date: data.date)),
y: .value("Triggers/Reflections", Double((data.triggersCount * 10 + data.reflectionsCount * 10))) // Each trigger/reflection represents 5 minutes
)
.foregroundStyle(Color.green.opacity(0.5))
BarMark(
x: .value("Day", formattedDate(date: data.date)),
yStart: .value("Sunlight Start", 0),
yEnd: .value("Minutes of Sunlight", data.duration * 60) // Convert hours to minutes
)
.foregroundStyle(Color.orange.opacity(0.5))
}
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(10)
.clipShape(RoundedRectangle(cornerRadius: 25))
.padding()
.background(themeSettings.currentColor)
.cornerRadius(25)
} else {
Text("No sunlight data")
.foregroundColor(.black)
.background(Color.white)
.cornerRadius(10)
.padding()
}
}
.frame(width: 350, height: 200)
.background(themeSettings.currentColor)
.cornerRadius(30)
}
private func formattedDate(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "E"
return formatter.string(from: date)
}
}
This view correctly shows todays day with the correct data
struct SleepSupportBox: View {
@ObservedObject var viewModel = SleepViewModel()
@EnvironmentObject var themeSettings: ThemeSettings
var body: some View {
VStack {
if !viewModel.sleepData.isEmpty {
Chart(viewModel.sleepData) { data in
BarMark(
x: .value("Day", formattedDate(date: data.date)),
y: .value("Triggers/Reflections", Double(data.triggersCount + data.reflectionsCount))
)
.foregroundStyle(Color.green.opacity(0.5))
BarMark(
x: .value("Day", formattedDate(date: data.date)),
y: .value("Hours of Sleep", data.hours)
)
.foregroundStyle(Color.asblue)
}
.frame(maxWidth: .infinity, maxHeight: .infinity)
.padding(10)
.clipShape(RoundedRectangle(cornerRadius: 25))
.padding()
.background(themeSettings.currentColor)
.cornerRadius(25)
} else {
Text("No sleep data")
.foregroundColor(.black)
.background(Color.white)
.cornerRadius(10)
.padding()
}
}
.frame(width: 350, height: 200)
.background(themeSettings.currentColor)
.cornerRadius(30)
}
private func formattedDate(date: Date) -> String {
let formatter = DateFormatter()
formatter.dateFormat = "E"
return formatter.string(from: date)
}
}