Member-only story
How to create a chart using Swift Charts and SwiftUI
Back in 2022, Apple introduced Swift Charts at the yearly Worldwide Developers Conference. Swift Charts is a powerful framework that allows you create interactive data visualizations. In this tutorial, we’re going to build a simple bar chart to display the number and genres of books you’ve read per year.
Data
We can get started by creating a new SwiftUI project on Xcode. Next, we’ll add a struct
that will represent a single bar, we’ll call it BookStat
.
Now we can create another struct
that we will use as our data provider. You can create a new file or just add it in ContentView.swift
for now, but the struct
will look like so:
Note that in addition to the bookData
list, we also created a simple Date
extension to create a Date
object from a given year.
Bar Chart
Now that our sample data is ready, we will display the bookData
list using a bar chart. Note that the…