In Swift Charts can the x/y: .value be a double, or does it have to be an integer?
I'm generating a graph that is showing hours on the Y axis. Hours is hours + minutes (as a decimal) but it only seems to work with the integers and won't take doubles. Can I use a double or do I have to do a work around?
A sample part of the code is below:
Chart{
ForEach(sunriseSunset) {item in
LineMark(
x: .value("Date", item.sunrise),
y: .value("Sunrise", (((Calendar.current.dateComponents(components, from: item.sunrise).hour!) * 60) + (Calendar.current.dateComponents(components, from: item.sunrise).minute!)) / 60),
// y: .value("Sunrise", (((Calendar.current.dateComponents(components, from: item.sunrise).hour!)) + (Calendar.current.dateComponents(components, from: item.sunrise).minute!)) * minsdecimal),
series: .value("Date", "Sunrise")
)
.foregroundStyle(.orange)
.lineStyle(StrokeStyle(lineWidth: 2))
}
}
.frame(height: 300)
.chartYAxis {
AxisMarks(
values: [0, 6, 12, 18, 24]
)
}
}
Yes you can have doubles for the y axis values.
To get doubles, you could try something like this for your y
values of hours + minutes (as a decimal)
:
y: .value("Sunrise", Double(Calendar.current.dateComponents([.hour], from: item.sunrise).hour!) + Double(Calendar.current.dateComponents([.minute], from: item.sunrise).minute!) / 60.0),
You could of course have this in your item
struct:
var sunriseHour: Double {
Double(Calendar.current.dateComponents([.hour], from: sunrise).hour!) + Double(Calendar.current.dateComponents([.minute], from: sunrise).minute!) / 60.0
}
and call:
y: .value("Sunrise", item.sunriseHour),