swiftui - 想要移动数值数字坐在柱状图的顶部
问题描述
// 这将创建一个条形图,按 dateCreated 显示 item.totalscore。// 我怎样才能把 item.totalscore 放在栏的末尾?
var body: some View {
VStack{
Text("Prior scores")
ForEach(items, id: \.self) { item in
HStack{
Text(Date2String(someDate: item.dateCreated!)).font(.caption2)
Rectangle()
.fill(BarChartColors(answer: item.totalscore))
.scaleEffect(CGSize(width: (Double(item.totalscore)+0.5)), height: 0.7), anchor: .leading)
Text("\(item.totalscore)").font(.caption2)
}
}
}.padding()
Spacer()
}
解决方案
尝试这样的事情:(注意你需要锻炼规模)
@State var max = 250.0 // <-- to be determined
let maxw = 333.0 // <-- to be determined
let h = 22.0 // <-- to be determined
var body: some View {
VStack{
Text("Prior scores")
ForEach(items, id: \.self) { item in
HStack{
Text(Date2String(someDate: item.dateCreated!)).font(.caption2)
Rectangle()
.fill(BarChartColors(answer: item.totalscore))
.scaleEffect(CGSize(width: Double(item.totalscore)+0.5, height: 0.7), anchor: .leading)
.frame(width: item.totalscore / max * maxw, height: h)
.clipped() // <--- here
Text("\(item.totalscore)").font(.caption2)
Spacer() // <--- here
}
}
}.padding()
.onAppear {
max = items.map { Double($0.totalscore) }.max() ?? 0.0
}
Spacer()
}
推荐阅读
- javascript - 微信小程序app开启支持对话
- google-cloud-platform - 谷歌云 CDN 后端服务负载均衡器不缓存任何资源
- python - 我无法导入语音
- salt-stack - salt cmd.run 如果没有结果不返回错误:非零退出代码
- python - django.db.utils.ProgrammingError:“FUNCTION”处或附近的语法错误
- multiple-databases - CustomUser 关于单独的数据库问题
- spring - 使用名称 springSecurityFilterChain 创建 bean 时出错 使用 LDAP 配置启动 Para 时未找到密钥安全的配置设置
- javascript - 默认为水平滚动的表格
- python - 为什么前五名的颜色都是红红红红?
- azure - 使用 Azure Front Door 将静态文件请求路由到 Blob 存储