如何更改 SwiftUI 图表中 BarMark 之间的间距

问题描述 投票:0回答:1

如何更改 SwiftUI 图表中 BarMark 之间的间距。似乎没有这方面的文档。这是我的图表,当我将其放入 ScrollView 中时,它会变得拥挤:

ScrollView(.vertical) {
    Chart {
        ForEach(0...csv.rows.count-1, id: \.self) { i in
            let movie = csv.rows[i][0]
            let rating = Double(csv.rows[i][2]) ?? 0
            BarMark(
                x: .value("Rating", rating),
                y: .value("Movie", movie),
                width: 30
            )
            .annotation(position: .trailing) {
                Text(String(format: "%.0f%", rating))
                .foregroundColor(Color.gray)
               .font(.system(size: 12, weight: .bold))
            }
            .foregroundStyle(.linearGradient(
                colors: [.mint, .green],
                startPoint: .leading,
                endPoint: .trailing)
            )
            .cornerRadius(10)
        }
    }
    .chartXAxisLabel("Rating")
    .chartYAxisLabel("Movie")
}
ios swiftui charts
1个回答
2
投票

Chart
适应给定的空间,不幸的是,
ScrollView
仅提供最小高度。因此,您必须根据条目数量为图表添加定义的高度,例如像这样:

ScrollView(.vertical) {
    Chart {
        ForEach(0...csv.rows.count-1, id: \.self) { i in
            let movie = csv.rows[i][0]
            let rating = Double(csv.rows[i][2]) ?? 0
            BarMark(
                x: .value("Rating", rating),
                y: .value("Movie", movie),
                width: 30
            )
            .annotation(position: .trailing) {
                Text(String(format: "%.0f%", rating))
                .foregroundColor(Color.gray)
               .font(.system(size: 12, weight: .bold))
            }
            .foregroundStyle(.linearGradient(
                colors: [.mint, .green],
                startPoint: .leading,
                endPoint: .trailing)
            )
            .cornerRadius(10)
        }
    }
    .chartXAxisLabel("Rating")
    .chartYAxisLabel("Movie")

    .frame(height: csv.rows.count * 60) // << HERE

}
© www.soinside.com 2019 - 2024. All rights reserved.