I'm doing a dead simple bar chart. It will show one bar per hour for a few days. The bar chart seems to have a bug where it will overlap the bars as soon as the overall width of the chart reaches some hardcoded value. The chart has .chartScrollableAxes(.horizontal)
so horizontal space is no issue, there's infinite amounts of it.
This screenshot shows the same content, but the bottom one has 25pt wide bars and the top one 16pt. 16 is the last width before they started overlapping.
To test play with numberOfValues
as well as the fixed widths for the BarMark
:s. Under no circumstances should the bars overlap unless I tell it to, it should use some configurable minimum spacing between bars. In my real case I do not artificially color the bars like this and the chart is really hard to read.
I've tried to look in the docs, but most modifiers are totally undocumented and I can't seem to find anything that would apply. By setting .chartXVisibleDomain
to some really low value I can force it to spread the bars out more, but then the my bar width is not honoured.
import SwiftUI
import Charts
struct Value: Hashable {
let x: Int
let y: Int
}
struct ContentView: View {
let values: [Value]
let colors: [Color] = [.red, .green, .blue]
var body: some View {
VStack {
Chart {
ForEach(values, id: \.self) { value in
BarMark(
x: .value("X", value.x),
y: .value("Y", value.y),
width: .fixed(16)
)
.foregroundStyle(colors[value.x % colors.count])
}
}
.chartScrollableAxes(.horizontal)
Chart {
ForEach(values, id: \.self) { value in
BarMark(
x: .value("X", value.x),
y: .value("Y", value.y),
width: .fixed(25)
)
.foregroundStyle(colors[value.x % colors.count])
}
}
.chartScrollableAxes(.horizontal)
}
.padding()
}
}
#Preview {
var rnd = SystemRandomNumberGenerator()
let numberOfValues = 50
var values: [Value] = []
for index in 0 ..< numberOfValues {
values.append(Value(x: index, y: Int(rnd.next() % 50)))
}
return ContentView(values: values)
}
Example with bars the same color. It's pretty much unusable.