1
votes

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

1

1 Answers

1
votes

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