在SwiftUI中绘制折线图

在SwiftUI中绘制折线图,swiftui,Swiftui,我试图在Swift UI中绘制折线图,但无法实现for循环: 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]))) } Path{Path in 移动路径(到:CGPoint(x:20,y:20)) ForEach(0..

我试图在Swift UI中绘制折线图,但无法实现for循环:

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])))
}
Path{Path in
移动路径(到:CGPoint(x:20,y:20))
ForEach(0..<11){index in
addLine(to:CGPoint(x:((索引*20)+40),y:Int(hr[index]))
}
给我一个生成错误:

类型“()”不能与“视图”一致;只能使用结构/枚举/类类型 遵守协议


以下是解决方案的演示:

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
Path{Path in
移动路径(到:CGPoint(x:20,y:20))
对于0..<11中的索引{
addLine(to:CGPoint(x:((索引*20)+40),y:y_pos[index]))
}
}
//.stroke(Color.red)//用于演示在ViewBuilder中进行的笔划