struct MonthlySalesChart: View {
var body: some View {
Chart(data, id: \.month) {
BarMark(
x: .value("Month", $0.month, unit: .month),
y: .value("Sales", $0.sales)
)
}
.chartXAxis {
AxisMarks(values: .stride(by: .month)) { value in
if value.as(Date.self)!.isFirstMonthOfQuarter {
AxisGridLine().foregroundStyle(.black)
AxisTick().foregroundStyle(.black)
AxisValueLabel(
format: .dateTime.month(.narrow)
)
} else {
AxisGridLine()
}
}
}
}
}
Interactive Charts
struct InteractiveBrushingChart: View {
@State var range: (Date, Date)? = nil
var body: some View {
Chart {
ForEach(data, id: \.day) {
LineMark(
x: .value("Month", $0.day, unit: .day),
y: .value("Sales", $0.sales)
)
.interpolationMethod(.catmullRom)
.symbol(Circle().strokeBorder(lineWidth: 2))
}
if let (start, end) = range {
RectangleMark(
xStart: .value("Selection Start", start),
xEnd: .value("Selection End", end)
)
.foregroundStyle(.gray.opacity(0.2))
}
}
.chartOverlay { proxy in
GeometryReader { nthGeoItem in
Rectangle().fill(.clear).contentShape(Rectangle())
.gesture(DragGesture()
.onChanged { value in
// Find the x-coordinates in the chart’s plot area.
let xStart = value.startLocation.x - nthGeoItem[proxy.plotAreaFrame].origin.x
let xCurrent = value.location.x - nthGeoItem[proxy.plotAreaFrame].origin.x
// Find the date values at the x-coordinates.
if let dateStart: Date = proxy.value(atX: xStart),
let dateCurrent: Date = proxy.value(atX: xCurrent) {
range = (dateStart, dateCurrent)
}
}
.onEnded { _ in range = nil } // Clear the state on gesture end.
)
}
}
}
}
What's new in SwiftUI
New EnvironmentValues
TextField inside Alert
List uses UICollectionView
See gist https://gist.github.com/onmyway133/fc08111964984ef544a176a6e9806c18
ButtonStyle composition
Customize Charts
Interactive Charts
Half sheet
Grouped Form
dropDestination
AnyLayout
LayoutThatFits https://gist.github.com/ryanlintott/d03140dd155d0493a758dcd284e68eaa
ShapeStyle extensions
Text and Image transition
Grid
Search token and search scope
openWindow
PhotosPicker
Table
Customizable Toolbar
ShareLink
onTapGesture with location
MenuBarExtra
Variable SFSymbol
What's in UIKit
Use SwiftUI inside UICollectionViewCell
New APIs
SMAppService
LARightStore
ScreenCaptureKit
Read more