[英]Swift: Make Int Enum conform to Plottable protocol in order to show a legend using Swift Charts
我試圖使我的 Int 枚舉符合Plottable以便在我的 Swift 圖表中使用圖例。 我不確定我是否了解如何正確實施它的方式。 這是我的枚舉:
enum Type: Int, CaseIterable, Identifiable, Plottable {
init?(primitivePlottable: Int) {
self.init(rawValue: primitivePlottable)
}
var primitivePlottable: Int {
return rawValue
}
var id: Int {
return rawValue
}
case first = 32
case second = 434
}
}
我的圖表如下所示:
Chart {
if let allObejcts = object.objects(type: Type.first) {
ForEach(allObejcts) { object in
BarMark(
x: .value("#", object.indexInSegment),
y: .value("Distance", object.distance)
)
.foregroundStyle(by: .value("type", object.type))
}
}
}
.chartLegend(position: .bottom, spacing: 8)
.chartLegend(SwiftUI.Visibility.visible)
.chartForegroundStyleScale([
Type.first: Color.pink,
Type.second: Color.yellow
])
.frame(minHeight: 200)
正如文檔所述enum
它應該是String
作為原始類型
所以制作下一個作品(用 Xcode 14b5 / iOS 16 測試)
enum Type: String, CaseIterable, Identifiable, Plottable {
init?(primitivePlottable: String) {
self.init(rawValue: primitivePlottable)
}
var primitivePlottable: String {
return rawValue
}
var id: String {
return rawValue
}
case first = "32"
case second = "434"
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.