英文:
SwiftUI Charts cutting off last value with centered AxisValueLabel
问题
我正在使用新的 Swift 图表库,它的 X 轴数值对齐有点奇怪。当我指定 AxisValueLabel(centered: true)
时,最后一个数值被切掉(在这个例子中是 "December")。
如果我不指定 center=true
,那么 X 轴数值会具有前导对齐,看起来不太对。
以下是用于图表的代码:
struct graphOne: View {
@ObservedObject var vm: CVVM
@State private var position = 30
var frameWidth = UIScreen.screenWidth
var body: some View {
Chart {
ForEach(vm.currentUsage) { empRes in
BarMark(
x: .value("Month", empRes.date, unit: vm.filter),
y: .value("energy", empRes.energyUsage)
)
//.foregroundStyle(.green)
.annotation(position: .top) {
Text("\(empRes.energyUsage)")
.font(.caption)
}
.cornerRadius(5)
}
}
.chartXAxis {
AxisMarks(preset: .aligned, values: vm.currentUsage.map{ $0.date}) { date in
AxisValueLabel(format: vm.chartType == "months" ? .dateTime.month() : .dateTime.day(), centered: true)
}
}
.chartYAxis {
AxisMarks(position: .leading)
}
.padding()
.frame(width: frameWidth)
}
}
英文:
I am messing with the new swift charts library and its alignment is a little weird for the x values.
When i specify AxisValueLabel(centered: true) the last value gets cut off(December in this case).
If I dont specify center=true, then the x value has leading alignment and it just looks off.
code for graph
struct graphOne: View {
@ObservedObject var vm: CVVM
@State private var position = 30
var frameWidth = UIScreen.screenWidth
var body: some View {
Chart {
ForEach(vm.currentUsage) { empRes in
BarMark(
x: .value("Month", empRes.date, unit: vm.filter),
y: .value("energy", empRes.energyUsage)
)
//.foregroundStyle(.green)
.annotation(position: .top) {
Text("\(empRes.energyUsage)")
.font(.caption)
}
.cornerRadius(5)
}
}
.chartXAxis {
AxisMarks(preset: .aligned, values: vm.currentUsage.map{ $0.date}) { date in
AxisValueLabel(format: vm.chartType == "months" ? .dateTime.month() : .dateTime.day(), centerd: true)
}
}
.chartYAxis {
AxisMarks(position: .leading)
}
.padding()
.frame(width: frameWidth )
}
}
答案1
得分: 0
在我发布的代码中,我在Chart{}上有一个修饰符
.chartXAxis{}
在这个修饰符中,我有如下的AxisMarks
.chartXAxis{
AxisMarks(preset: .aligned....){}
}
我在某个教程中找到了这个,当我删除preset: .aligned后,我的问题得以解决。
解决方案
Chart{...}
.chartXAxis{
AxisMarks(...){}
}
英文:
in the code I have posted I have a modifier on the Chart{} called
.chartXAxis{}
within this modifier I have the AxisMarks as follows
.chartXAxis{
AxisMarks(preset: .aligned....){}
}
I found that in a tutorial somewhere and didn't think much of it. After removing the preset: .aligned my issues were solved.
SOLUTION
Chart{...}
.chartXAxis{
AxisMarks(...){}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论