Swift
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Optionals
-
Section 4: Object-Oriented and Value Types
-
Section 5: Memory Management
-
Section 6: Generics and Error Handling
-
Section 7: Concurrency
-
Section 8: Working with Collections
-
Section 9: Codable and Data Handling
-
Section 10: Protocol-Oriented Programming
-
Section 11: Testing and Tooling
-
Section 12: Practical Projects
-
Section 13: Interview Practice
-
Section 14: More Practice Exercises
-
Section 15: More Standard Library
-
Section 16: Advanced Concurrency
-
Section 17: Foundation Framework Deep Dive
-
Section 18: URLSession and Networking Deep Dive
-
Section 19: Combine Framework
-
Section 20: SwiftUI Fundamentals for Swift Developers
-
Section 21: Server-Side Swift with Vapor
-
Section 22: Swift Package Manager Deep Dive
-
Section 23: Swift Concurrency Deep Dive
-
Section 24: More Language Features
-
Section 25: Error Handling Deep Dive
-
Section 26: Testing Deep Dive
-
Section 27: Data Structures and Algorithms in Swift
-
Section 28: More Practice Exercises
-
Section 29: More Interview Practice
-
Section 30: Swift Macros (Swift 5.9+)
-
Section 31: Property Wrappers Ecosystem
-
Section 32: Swift Interop Deep Dive
-
Section 33: iOS App Architecture Patterns
-
Section 34: Performance and Debugging
-
Section 35: App Distribution and CI/CD
-
Section 36: More Practical Projects
-
Section 37: SwiftData and Persistence
-
Section 38: More Design Patterns
-
Section 39: More Review and Practice
-
Section 40: More Foundation Deep Dive
-
Section 41: Advanced Collections in Swift
-
Section 42: Advanced Generics Practice
-
Section 43: UIKit for Legacy and Hybrid Apps
-
Section 44: watchOS and visionOS Development Basics
-
Section 45: More Networking Patterns
-
Section 46: More Testing Practice
-
Section 47: Accessibility in Swift Apps
-
Section 48: Localization
-
Section 49: More Practical Projects Round 2
-
Section 50: Swift Charts Framework
-
Section 51: More Interview and Algorithm Practice
-
Section 52: Final Practice and Mastery
-
Section 53: Swift Compiler and Build System
-
Section 54: More Concurrency Practice
-
Section 55: App Store Guidelines and Review
-
Section 56: More Design and Architecture
217: Building Bar and Line Charts with Swift Charts
Swift Charts is honestly a breath of fresh air. Before this framework, if you wanted a custom chart, you were either wrestling with complex Core Graphics paths or paying for a third-party library. Now, we can describe our data visually using a declarative syntax that feels just like the rest of SwiftUI.
To get a feel for this, let's build a "Weekly Activity Tracker." We want to show a user's step counts for the last seven days. I think a hybrid approach is best here: bars to show the volume of each day, and a line overlaid on top to highlight the trend. It gives the user the best of both worlds.
Structuring the Step Data
First, we need a simple model. I'm going to use a basic struct. Since we're dealing with dates and numbers, a Date and an Int will do the trick. I'll create a small array of mock data so we have something to actually visualize.
struct StepCount: Identifiable {
let id = UUID()
let date: Date
let count: Int
}
let weeklyData: [StepCount] = [
StepCount(date: Calendar.current.date(byAdding: .day, value: -6, to: Date())!, count: 8000),
StepCount(date: Calendar.current.date(byAdding: .day, value: -5, to: Date())!, count: 12000),
StepCount(date: Calendar.current.date(byAdding: .day, value: -4, to: Date())!, count: 7000),
StepCount(date: Calendar.current.date(byAdding: .day, value: -3, to: Date())!, count: 15000),
StepCount(date: Calendar.current.date(byAdding: .day, value: -2, to: Date())!, count: 11000),
StepCount(date: Calendar.current.date(byAdding: .day, value: -1, to: Date())!, count: 9000),
StepCount(date: Calendar.current.date(byAdding: .day, value: 0, to: Date())!, count: 13000),
]
Visualizing Daily Volume with BarMark
Now for the chart itself. The Chart view acts as the container, and inside it, we use "Marks." To get those daily columns, I'll use BarMark. I'm mapping the date to the x-axis and the count to the y-axis.
Chart(weeklyData) { step in
BarMark(
x: .value("Day", step.date, unit: .day),
y: .value("Steps", step.count)
)
.foregroundStyle(.blue.gradient)
}
.frame(height: 300)
I used unit: .day in the x-value. This is a neat feature of Swift Charts—it tells the framework how to group the dates automatically so we don't have to manually format strings for the axis labels.
Adding the Trend Line
Bars are great for volume, but a line helps the eye track the "flow" of the week. I'll add a LineMark right below the BarMark. Because they are in the same Chart block, they share the same coordinate system.
Chart(weeklyData) { step in
BarMark(
x: .value("Day", step.date, unit: .day),
y: .value("Steps", step.count)
)
.foregroundStyle(.blue.opacity(0.3))
LineMark(
x: .value("Day", step.date, unit: .day),
y: .value("Steps", step.count)
)
.foregroundStyle(.blue)
.interpolationMethod(.catmullRom)
}
I added .interpolationMethod(.catmullRom) here. By default, lines are jagged (linear). Catmull-Rom smooths the line out, which usually looks more "premium" for fitness apps.
Fixing the Zig-Zag Mess
Here is where I messed up. When I first ran this with a larger, randomized dataset, my line chart looked like a heart monitor during a panic attack—it was zig-zagging back and forth across the screen. I realized I had added some data points out of chronological order.
Swift Charts doesn't automatically sort your data; it draws the line in the order the elements appear in the array. To fix this, I can't just pass the array as-is. I need to ensure it's sorted by date before it hits the Chart view.
// The fix: Sort the data before passing it to the Chart
let sortedData = weeklyData.sorted { $0.date < $1.date }
Chart(sortedData) { step in
// ... marks go here
}
Polishing the Axis and Details
To wrap this up, the chart looks a bit naked. I want to add a PointMark so the user can see exactly where the data points sit on the line, and I'll add a chart title and some axis styling.
Chart(sortedData) { step in
BarMark(
x: .value("Day", step.date, unit: .day),
y: .value("Steps", step.count)
)
.foregroundStyle(.blue.opacity(0.2))
LineMark(
x: .value("Day", step.date, unit: .day),
y: .value("Steps", step.count)
)
.foregroundStyle(.blue)
.interpolationMethod(.catmullRom)
PointMark(
x: .value("Day", step.date, unit: .day),
y: .value("Steps", step.count)
)
.foregroundStyle(.blue)
}
.chartYAxis {
AxisMarks(position: .leading) { value in
AxisGridLine()
AxisTick()
AxisValueLabel()
}
}
.frame(height: 300)
.padding()
By moving the Y-axis to the .leading position, it feels more like a professional dashboard. We've now combined three different mark types to create a single, cohesive visualization.
📋 Practical Task
Exercise: Building a Monthly Temperature Range Chart
Your task is to create a chart that visualizes the temperature fluctuations for a city over a 5-day period. Instead of just a single line, you will create a "Range Chart" using AreaMark.
- Create a
TemperatureReadingstruct withdate: Date,lowTemp: Double, andhighTemp: Double. - Generate an array of 5 days of data.
- Use an
AreaMarkto fill the space between thelowTempandhighTemp. Hint: You'll need to provide both a start and end value for the y-axis in theAreaMark. - Overlay a
LineMarkthat tracks only thehighTempto show the peak trend. - Ensure the data is sorted by date to avoid any rendering glitches.
- Apply a custom color (like
.orange) to the area and line to represent heat.
There are no comments for now.