Create a bar Chart with data displayed (side by side) instead of default (stacked bar chart) (using .position modifier) Issue: chart proxy value cannot find any value. If I change to stacked bar chart, then proxy value works ok.
Sample code:-
Chart() {
ForEach(barchartdata, id:\.self) { one_row in
BarMark(
x: .value("Date", one_row.shortDate),
y: .value("$", one_row.steps)
) // LineMark
.foregroundStyle(by: .value("Name", one_row.name))
.symbol(by: .value("Name", one_row.name))
.interpolationMethod(.catmullRom)
.position(by: .value("Name", one_row.name))
}
}
.chartPlotStyle { plotArea in
plotArea
.frame(width: UIScreen.main.bounds.size.width * 6/8)
.background(.blue.opacity(0.2))
.border(.blue, width: 1)
}
.chartOverlay { proxy in
GeometryReader { geometry in
RoundedRectangle(cornerRadius : 5)
.fill(.clear)
.contentShape(Rectangle())
.onTapGesture { location in
updateSelectedMonth(location: location, proxy: proxy, geometry: geometry)
} // onTapGesture
}
}
....
func updateSelectedMonth(location : CGPoint, proxy: ChartProxy, geometry : GeometryProxy) {
let local_location = CGPoint(
x: location.x - geometry[proxy.plotAreaFrame].origin.x,
y: location.y - geometry[proxy.plotAreaFrame].origin.y)
let (sel_date, sel_steps) = proxy.value(at: local_location, as: (Date, Int64).self) ?? (nil,nil)
}