First, I have messed with SwiftUI for a long time, but this is the first time that I’ve actually started making a ship-quality app with it, so it’s entirely possible (likely, even), that the answer is a simple one. I would still appreciate the help. I have very little pride, when learning new stuff.
The issue is that I’m setting up a Swift Chart, and it’s a basic bar chart, showing the number of users in a system, splitting them between new users, and ones that have been established.
The output currently looks like so:
Which is great, but the problem that I’m having, right now, is this:
Note how little space there is, between the axis labels, and the chart edge.
I’d like to give it more breathing room, but I’m kind of stumped, as to the proper way to do that.
Thanks, in advance, for any help!
Here’s the code for the chart section:
struct UserTypesChart: View {
@State var data: [RCVST_DataProvider.RowPlottableData]
var body: some View {
GroupBox("SLUG-USER-TOTALS-CHART-TITLE".localizedVariant) {
Chart(data) { inRowData in
ForEach(inRowData.data, id: .userType) { inUserTypeData in
BarMark(
x: .value("SLUG-BAR-CHART-USER-TYPES-X".localizedVariant, inRowData.date),
y: .value("SLUG-BAR-CHART-USER-TYPES-Y".localizedVariant, inUserTypeData.value)
)
.foregroundStyle(by: .value("SLUG-BAR-CHART-USER-TYPES-LEGEND".localizedVariant, inUserTypeData.userType.localizedString))
}
}
}
.chartYAxisLabel("Users", spacing: 12)
.chartYAxis {
AxisMarks(preset: .aligned, position: .leading) { _ in
AxisGridLine()
AxisValueLabel(anchor: .trailing)
}
}
.chartXAxisLabel("Date", alignment: .bottom)
.chartXAxis {
AxisMarks(preset: .aligned, position: .bottom) { _ in
AxisGridLine()
AxisValueLabel()
}
}
.padding()
}
}
2
Answers
Try using the horizontal spacing for the Y AxisValueLabel:
So in your example, something like this:
When plotting dates on a bar chart, always pass a
unit:
argument. In this case, each bar presumably represents a day, so you should doThis tells SwiftUI Charts that this is categorical data, and not continuous/numerical data. This helps the chart to lay out the chart in the way you expect, and adds some reasonable spacing between the axis and the marks.
This also gives the bars an appropriate width. Your current code should be emitting a warning telling you that the bar widths are not defined, and falling back to a default value.
If you want more spacing, you can pass a
horizontalSpacing:
argument like in Marcy’s answer.