With the release of SwiftUI 4, Apple has made it super simple to add charts to your apps. With a simple Charts framework import and a simple API, we get powerful charts out of the box.
Today, we will look at creating a bar chart in SwiftUI 4.
Basic Bar Chart
Let’s start by putting together a simple chart view. We will start by importing Charts framework and creating a Chart view.
Note: Chart is a SwiftUI view that displays a chart
import SwiftUI
import Chartsstruct ContentView: View {
var body: some View {
Chart {
// TODO add Marks
}
}
}
Adding Bars with BarMark
Inside the chart, we specify the graphical marks that represent data which needs to be plotted. We will start by adding a single BarMark inside our chart.
Note: BarMark represents data using bars. BarMark takes x and y values, these values are PlottableValue types. PlottableValue type conforms to Plottable protocol which is a type that can serve as data for the labeled, plottable values that we can draw on a chart.
We will plot daily workout sessions inside our chart example, so let’s add a BarMark for Monday with time spent on workout as 20 minutes.
As we can see our Chart view takes over the screen and plots a single bar for the available space. We can use existing modifiers to style our charts. Let’s set the height for the chart using the frame modifier.
So far, we have been creating data marks inside the chart view, but in a production environment this will come dynamically, so let’s change our code a bit.
We will create a simple data structure to use with our shiny new chart. ✨. Let’s create a struct to capture total minutes spent on workouts by day.
struct Workout: Identifiable {
var id = UUID()
var day: String
var minutes: Double
}
We will add some sample data inside the extension for this workout struct, as shown below:
Notice how we have all the bars of the same color. This is great, but what if we need to color each bar a different color? Well, that can be done with another overload of the foregroundStyle modifier, where it takes PlottableValue as a parameter. With this new modifier, SwiftUI picks colors for you and colors each bar so you can identify them easily.
If you are a keen observer, you will notice something new on this screen. SwiftUI automatically adds a new view on to the screen called Legend.
Chart legend appears by default when you first create a chart with a different foreground style. Legends show the names and colors of each series of data. The legend text is taken from the chart’s nominal value.
Chart Foreground Style Scale
Applying foregroundStyle(by: PlottableValue) presents a challenge where we lose the ability to add color of our choice. Charts API does a great job picking random colors for us, but if we want more control over the theme, we can change that with the help of chartForegroundStyleScale modifier.
chartForegroundStyleScale modifier configures the foreground style scale for charts and using its domain and range parameters we can add our own colors to the bar.
chartForegroundStyleScale function takes 3 parameters:
domain: The possible data values plotted as foreground style in the chart. You can define the domain with a ClosedRange for number or Date values (e.g., 0 ... 500), and with an array for categorical values (e.g., ["A", "B", "C"])
range: The range of foreground styles that correspond to the scale domain.
scale: scale type defines the ways you can scale the domain or range of a plot. Default value is nil.
Marks can be decorated with annotations to show relevant information. Annotation is a SwiftUI view meaning, it’s on us to how we would like to show the information which is relevant and pleasant for the user.
We will annotate our bar marks with a simple indigo color figure image with the help of the annotation modifier, which takes position as a parameter to define the location where the annotation will be shown:
We can take this a step further by building a custom view just for annotations. For that, we will move code for colors into its own struct in order to access it from other views.