Pretty basic code for a horizontal bar chart using Apple's Swift Charts:
private var testData: [(String, Int)] = []
init() {
for i in (0 ..< 10).reversed() {
testData.append(
("Item\(i)", i + 3)
)
}
}
var body: some View {
Chart(testData, id: \.0) { item in
BarMark(
x: .value("x", item.1),
y: .value("y", item.0)
)
.foregroundStyle(Color.red)
.annotation(
position: .overlay,
alignment: .trailing
) {
Text("\(item.1)")
.foregroundColor(.white)
.fontWeight(.bold)
.padding(.trailing)
}
}
.chartXAxis(.hidden)
.chartYAxis {
AxisMarks(
position: .trailing
) { value in
AxisValueLabel(centered: true) {
if let stringValue = value.as(String.self) {
Text(stringValue)
.font(.title2)
}
}
}
}
}
With this result but I can't seem to prevent the chart from overlapping the Y axis label:
Even if I align the label with position: .leading
it's still hidden under the chart. Any help would be appreciated.