I have data that returns a timestamp. I want to stride through these dates every 3 days – something like this. The issue is that the data I have is a string but I need it to be a Date
object for the purpose of the chart. Can anyone advise the best way to approach this?
struct StockPrice: Identifiable {
let id = UUID()
let timestamp: String
let price: Double
}
let stockPrices: [StockPrice] = [
StockPrice(timestamp: "2018-06-22T19:29:37.000Z", price: 121.12),
StockPrice(timestamp: "2018-06-23T19:29:37.000Z", price: 122.34),
StockPrice(timestamp: "2018-06-24T19:29:37.000Z", price: 120.78),
...
]
struct ContentView: View {
let maxStockPrice = stockPrices.map { $0.price }.max()
let minStockPrice = stockPrices.map { $0.price }.min()
var body: some View {
VStack {
Chart(stockPrices) {
AreaMark(
x: .value("Month", $0.timestamp),
yStart: .value("Total", $0.price),
yEnd: .value("Total", minStockPrice ?? 0)
)
}
.chartXAxis {
AxisMarks {
let dateFormatter = DateFormatter()
if let value = $0.as(String.self) {
let _ = print(dateFormatter.date(from: value))
AxisValueLabel {
Text("T")
.fontWeight(.bold)
.foregroundColor(.white)
}
}
}
}
}
}