Bar graph
Let's start with the bar chart showing the average number of wines sold per day of the week.
π₯ Create a SwiftUI file called : ParisDetailsChart
The sales data has the weekday stored as a date and how many wine is sold as an integer.
// The store data has the weekday stored as a date
// And how many wines sold as an integer
// The data for Paris is in a variable, parisData
import Charts
import SwiftUI
func date(year: Int, month: Int, day: Int = 1) -> Date {
Calendar.current.date(from: DateComponents(year: year, month: month, day: day)) ?? Date()
}
struct SalesSummary: Identifiable {
let weekday: Date
let sales: Int
var id: Date { weekday }
}
let parisData: [SalesSummary] = [
/// Monday
.init(weekday: date(year: 2022, month: 5, day: 2), sales: 87),
/// Tuesday
.init(weekday: date(year: 2022, month: 5, day: 3), sales: 42),
/// Wednesday
.init(weekday: date(year: 2022, month: 5, day: 4), sales: 67),
/// Thursday
.init(weekday: date(year: 2022, month: 5, day: 5), sales: 24),
/// Friday
.init(weekday: date(year: 2022, month: 5, day: 6), sales: 139),
/// Saturday
.init(weekday: date(year: 2022, month: 5, day: 7), sales: 150),
/// Sunday
.init(weekday: date(year: 2022, month: 5, day: 8), sales: 60)
]
import Charts
import SwiftUI
func date(year: Int, month: Int, day: Int = 1) -> Date {
Calendar.current.date(from: DateComponents(year: year, month: month, day: day)) ?? Date()
}
struct SalesSummary: Identifiable {
let weekday: Date
let sales: Int
var id: Date { weekday }
}
let parisData: [SalesSummary] = [
/// Monday
.init(weekday: date(year: 2022, month: 5, day: 2), sales: 87),
/// Tuesday
.init(weekday: date(year: 2022, month: 5, day: 3), sales: 42),
/// Wednesday
.init(weekday: date(year: 2022, month: 5, day: 4), sales: 67),
/// Thursday
.init(weekday: date(year: 2022, month: 5, day: 5), sales: 24),
/// Friday
.init(weekday: date(year: 2022, month: 5, day: 6), sales: 139),
/// Saturday
.init(weekday: date(year: 2022, month: 5, day: 7), sales: 150),
/// Sunday
.init(weekday: date(year: 2022, month: 5, day: 8), sales: 60)
]
struct ParisDetailsChart: View {
var body: some View {
Chart(parisData) { element in
BarMark(
x: .value("Day", element.weekday, unit: .day),
y: .value("Sales", element.sales)
)
}
.padding(20)
}
}
struct ParisDetailsChart_Previews: PreviewProvider {
static var previews: some View {
ParisDetailsChart()
}
}
Last updated