1

I want to plot a date against a time, extracted from the same date: item.sunrise (ISO 8601). The compiler rejects the code below stating: Initializer init(x:y:) requires that DateComponents conform to Plottable.

        Chart{
            ForEach(sunriseSunsetDates) {item in
                LineMark(
                    x: .value("Date", calendar.dateComponents([.year, .month, .day], from: item.sunrise)),
                    y: .value("Time", calendar.dateComponents([.hour, .minute, .second], from: item.sunrise))
                )
                .foregroundStyle(.blue.gradient)
                .interpolationMethod(.catmullRom)
            }
        }

Can this be done or do I need to rewrite this in a different format?

Edward Hasted
  • 3,201
  • 8
  • 30
  • 48

1 Answers1

1

To plot Dates, you can use the value func that takes a Date and Calendar.Component as parameters:

Chart{
    ForEach(sunriseSunsetDates) {item in
        LineMark(
            x: .value("Date", item.sunrise, unit: .day),
            y: .value("Time", item.sunrise, unit: .minute)
        )
    }
}
Ashley Mills
  • 50,474
  • 16
  • 129
  • 160
  • That works but how to I get it to accept the time of day? Surely .minute just registers the minutes components when I need the complete time? – Edward Hasted Jan 24 '23 at 22:13