Reputation: 13
I'm trying to draw a line chart in Swift UI but can't implement the for loop:
Path { path in
path.move(to: CGPoint(x: 20, y: 20))
ForEach(0 ..< 11) { index in
path.addLine(to: CGPoint(x: ((index * 20) + 40), y: Int(hr[index])))
}
Gives me a build error:
Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols
Upvotes: 1
Views: 1975
Reputation: 257543
Here is a demo of solution:
Path { path in
path.move(to: CGPoint(x: 20, y: 20))
for index in 0 ..< 11 {
path.addLine(to: CGPoint(x: ((index * 20) + 40), y: y_pos[index]))
}
}
// .stroke(Color.red) // for demo stroked if inside ViewBuilder
Upvotes: 1