[英]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.