我正试图在Swift UI中实现一个基于特定日期的费用应用程序的图表。我可以得到每月的总金额&显示每一个,但我被困在显示特定月份的付款。E上个月,当前月份)按周划分。
我正在从Firestore获取支付数据,但只能获取所有数据,而不仅仅是日期,据我所知?
Chart {
ForEach(paymentModel.payments) { payment in
BarMark(
x: .value("month", payment.paymentDate, unit: .weekOfMonth),
y: .value("Amount", payment.paymentAmount)
)
}
}
.frame(height: 180)
.padding()
.chartXAxis {
AxisMarks() { date in
AxisValueLabel(format: .dateTime.week(), centered: true)
}
}
.onAppear() {
self.paymentModel.getAllPayments()
}
}
}
}
谢谢!
我已经使用了总金额,但不确定如何获得仅基于某个月或其他标准(如类别)的显示付款
你需要先过滤它。至于图表,它有点棘手,我仍在努力找出如何做到这一点。但是,如果您想查看本周或仅今天完成的事务列表,您可以使用这样的过滤器:
func filteredTransactions(for category: Category, in timeFrame: TimeFrame) -> [FinancialTransaction] {
let now = Date()
let calendar = Calendar.current
let filteredTransactions = category.transactions.filter { transaction in
let components: DateComponents
switch timeFrame {
case .day:
components = calendar.dateComponents([.day], from: transaction.date, to: now)
return components.day! < 1
case .week:
components = calendar.dateComponents([.weekOfYear], from: transaction.date, to: now)
return components.weekOfYear! < 1
case .month:
components = calendar.dateComponents([.month], from: transaction.date, to: now)
return components.month! < 1
case .year:
components = calendar.dateComponents([.year], from: transaction.date, to: now)
return components.year! < 1
}
}
return filteredTransactions
}
我正试图用图表实现这个过滤器,我成功了,我会让你知道,这样你也可以使用它。您还可以通过显示自定义日期来更改过滤器。