I’m testing charts framework usage, but i cannot understand how to show two values from same origin in the same chart. Here I built a fake example of a blood pressure app, where I would like to show in the same chart BOTH data of Systolic and Diastolic pressure. I tried different “forEach” but cannot understand how to correct two issues.
1 – values are of the same color.
2 – values are “linked” as they were same set of values.
what I want is a chart with two lines like this one, one for each value (in different color etc…):
but I get this:
struct MyBloodValue {
var date: Date
var systolic: Double
var diastolic: Double
}
import SwiftUI
import Charts
struct ContentView2: View {
@State var bloodPressureData: [MyBloodValue] = []
var body: some View {
VStack {
Text("Blood Pressure Data")
.font(.headline)
Chart(bloodPressureData, id: .date) { data in
LineMark(
x: .value("Date", data.date),
y: .value("Systolic", data.systolic)
)
.foregroundStyle(.purple)
.symbol(Circle())
LineMark(
x: .value("Date", data.date),
y: .value("Diastolic", data.diastolic)
)
.foregroundStyle(.blue)
.symbol(Circle())
}
.chartForegroundStyleScale(range: [.green, .orange])
.frame(height: 300)
.padding()
}
.onAppear {
self.bloodPressureData = self.generateTestData(count: 3)
}
}
func generateTestData(count: Int) -> [MyBloodValue] {
var testData: [MyBloodValue] = []
let currentDate = Date()
for i in 0..<count {
let randomSystolic = Double.random(in: 100...125)
let randomDiastolic = Double.random(in: 70...90)
let randomDate = currentDate.addingTimeInterval(Double(-3600 * i))
testData.append(MyBloodValue(date: randomDate, systolic: randomSystolic, diastolic: randomDiastolic))
}
return testData
}
}
struct ContentView2_Previews: PreviewProvider {
static var previews: some View {
ContentView2()
}
}