简体   繁体   English

Swift 图表中的自定义图例 iOS 16

[英]Custom legends in Swift Charts for iOS 16

How do I obtain colour imagery, or more generally, styling information for each legend entry to construct a custom legend in a chart in Swift Charts?如何获取每个图例条目的彩色图像或更一般的样式信息,以在 Swift 图表中的图表中构建自定义图例?

I have a chart here, with a legend positioned on the right, but using the content: argument of chartLegend does not pass any info into the closure to use.我在这里有一个图表,图例位于右侧,但使用content:chartLegend参数不会将任何信息传递给要使用的闭包。 I would like to wrap the legend into a scroll view, so when there are too many entries, the chart will appear correctly on the screen, and the user can scroll through the legend below the chart.我想将图例包装成滚动视图,这样当条目过多时,图表将正确显示在屏幕上,用户可以滚动浏览图表下方的图例。

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)
// ...

Here is the chart with too many legend entries interfering with the chart sizing, resulting in cropping:这是具有过多图例条目的图表,干扰了图表大小,导致裁剪:

裁剪图。

And here is the chart with only a few entries so that the chart is sized correctly, with no cropping, and the legend has text to discern between the electrodes they represent:这是只有几个条目的图表,因此图表的大小正确,没有裁剪,并且图例有文本可以区分它们所代表的电极:

具有较少图例条目的相同图表。

Any help is much appreciated.任何帮助深表感谢。

Not scrolling but the chart legend can be placed on the side with code like this:不滚动,但图表图例可以放在一边,代码如下:

.chartLegend(position: .trailing, alignment: .top)

This gives more room for a legend.这为传奇提供了更多空间。 The legend won't scroll but all the items will be listed even if going past the end of the chart.图例不会滚动,但即使超出图表末尾,也会列出所有项目。 The frame height can be increased.框架高度可以增加。 The example shows a before(default) and after(using the code above).该示例显示了之前(默认)和之后(使用上面的代码)。

在此处输入图像描述

The colors can be assigned manually using the chartForegroundStyleScale like this after the end of the chart: colors 可以在图表结束后使用 chartForegroundStyleScale 手动分配,如下所示:

    .chartForegroundStyleScale([
        "Hong Kong": Color.green,
        "London": Color.red,
        "Taipei": Color.purple,
        "New York": Color.teal,
        "Paris": Color.pink,
        "Sydney": Color.orange
    ])

F

Symbols can be created based on the series:可以基于系列创建符号:

.symbol(by: .value("City", series.city))

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM