![](/img/trans.png)
[英]How to add custom value in charts for bottom label in swift iOS
[英]Custom legends in Swift Charts for iOS 16
如何獲取每個圖例條目的彩色圖像或更一般的樣式信息,以在 Swift 圖表中的圖表中構建自定義圖例?
我在這里有一個圖表,圖例位於右側,但使用content:
的chartLegend
參數不會將任何信息傳遞給要使用的閉包。 我想將圖例包裝成滾動視圖,這樣當條目過多時,圖表將正確顯示在屏幕上,用戶可以滾動瀏覽圖表下方的圖例。
Chart(points, id: \.self) { point in
LineMark(
x: .value("time/s", point.timestamp),
y: .value("potential/mV", point.potential)
)
.foregroundStyle(by: .value("Electrode", point.electrode.symbol))
}
.chartLegend(position: .bottom)
// ...
這是具有過多圖例條目的圖表,干擾了圖表大小,導致裁剪:
這是只有幾個條目的圖表,因此圖表的大小正確,沒有裁剪,並且圖例有文本可以區分它們所代表的電極:
任何幫助深表感謝。
不滾動,但圖表圖例可以放在一邊,代碼如下:
.chartLegend(position: .trailing, alignment: .top)
這為傳奇提供了更多空間。 圖例不會滾動,但即使超出圖表末尾,也會列出所有項目。 框架高度可以增加。 該示例顯示了之前(默認)和之后(使用上面的代碼)。
colors 可以在圖表結束后使用 chartForegroundStyleScale 手動分配,如下所示:
.chartForegroundStyleScale([
"Hong Kong": Color.green,
"London": Color.red,
"Taipei": Color.purple,
"New York": Color.teal,
"Paris": Color.pink,
"Sydney": Color.orange
])
可以基於系列創建符號:
.symbol(by: .value("City", series.city))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.