英文:
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)
                })
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论