可以使用以下代码通过使用覆盖位置和中心对齐来注释条形:
.annotation(position: .overlay, alignment: .center) {
// your Text or other overlay here
}
Run Code Online (Sandbox Code Playgroud)
带有注释的示例图表。
下面提供了用于创建图表的完整代码以供参考。
图表代码:
struct BarChart4: View {
var body: some View {
VStack(alignment: .center) {
Text("Basic Bar Chart")
.font(.callout)
.foregroundStyle(.secondary)
Chart (BarData4.data) { shape in
BarMark (
x: .value("Shape", shape.type),
y: .value("Count", shape.count)
)
.annotation(position: .overlay, alignment: .center) {
Text("\(shape.count, format: .number.precision(.fractionLength(0)))")
.foregroundColor(.white)
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
样本数据:
struct BarData4 {
static let data: [ShapeModel] = [
.init(type: "Square", count: 12),
.init(type: "Heart", count: 10),
.init(type: "Rectangle", count: 21),
.init(type: "Star", count: 15),
.init(type: "Circle", count: 8),
.init(type: "Triangle", count: 6)
]
}
Run Code Online (Sandbox Code Playgroud)
和数据模型:
struct ShapeModel: Identifiable {
var type: String
var count: Double
var id = UUID()
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3373 次 |
最近记录: |