SwiftUI图表扩展图像:Plottable

huangapple go评论54阅读模式
英文:

SwiftUI Charts extension Image: Plottable

问题

有没有明智的方法来创建类似extension Image: Plottable的东西,并将其传递给BarMark内部的.value。或者有其他方法在图表下方放置图像而不是文本?

我尝试了类似以下的方法,但当然不起作用:

extension Image: Plottable {
    
    public var primitivePlottable: String {
        ????
    }
    
    public init?(primitivePlottable: String) {
        self.init(primitivePlottable)
    }
}
英文:

Is there any sensible way to create sth like extension Image: Plottable and pass to .value inside BarMark. Or there is any other possibility to have images below charts instead text?

I try sth like but it of course doesn't work

extension Image: Plottable {
    
    public var primitivePlottable: String {
        ????
    }
    
    public init?(primitivePlottable: String) {
        self.init(primitivePlottable)
    }
}

答案1

得分: 1

我找到了一个稍微不同的解决方案,使用了带有位置:.bottom的注释修饰符,如下所示:

BarMark(x: .value("形状类型", shape.type),
        y:  .value("总计", shape.count))
    .annotation(position: .bottom, alignment: .center, spacing: 10, content: {
        Image(shape.type)
            .resizable()
            .scaledToFit()
            .frame(width: 50, height: 50)
    })
英文:

ok i found a slightly different solution to this problem using annotation modifier with position: .bottom like:

BarMark(x: .value("Shape Type", shape.type),
                        y:  .value("Total Count", shape.count))
                .annotation(position: .bottom, alignment: .center, spacing: 10, content: {
                    Image(shape.type)
                        .resizable()
                        .scaledToFit()
                        .frame(width: 50, height: 50)
                })

huangapple
  • 本文由 发表于 2023年2月14日 04:50:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441053.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定