HomeiOS DevelopmentMaking a Line Chart Utilizing SwiftUI Charts API

Making a Line Chart Utilizing SwiftUI Charts API

The Charts framework is one in all my favorite options for the brand new model of SwiftUI in iOS 16. Previous to iOS 16, it’s worthwhile to construct your individual charts or depend on third social gathering library to render charts for you. With the discharge of this new framework, Apple has made it very straightforward for builders to create animated and interactive charts.

I’ve briefly mentioned the utilization of the Charts API in this publish. For this tutorial, let’s see easy methods to use the APIs to create a line chart.

The Climate Knowledge

To exhibit the SwiftUI Charts API, we are going to create a line chart that shows the typical temperature of Hong Kong, Taipei, and London from 2021-Jul to 2022-Jun.

To retailer the climate information, I created a WeatherData struct like under:

The Chart initializer takes in an inventory of Identifiable objects. That is why we make the WeatherData conform the Identifiable protocol.

For every metropolis, we create an array to retailer the climate information. Right here is an instance for London:

To retailer the climate information of the cities, we even have an array for that:

Making a Easy Line Chart Utilizing Chart and LineMark

To create any sorts of chart utilizing the Charts framework, you need to first import the Chartsframework:

Subsequent, you begin with the Chart view. Contained in the Chart view, you present a set of LineMark to create a line chart. Right here is an instance:

What the code above does is to plot a line chart for displaying the typical temperature of Hong Kong. The ForEach assertion loops by all gadgets saved in hkWeatherData. For every merchandise, we create a LineMark object that the x axis is about to the date and the y axis is about to the typical temperature.

Optionally, you’ll be able to resize the chart utilizing the body modifier. When you preview the code in Xcode preview, it’s best to see the next line chart:


Customizing Chart Axes

You’ll be able to customise each x and y axes by utilizing the chartXAxis and chartYAxis modifiers respectively. Let’s say, if we need to show the month labels utilizing the digit format, we are able to connect the chartXAxis modifier to the Chart view like this:

Inside chartXAxis , we create a visible mark referred to as AxisMarks for the values of month. For every worth, we show a worth label by utilizing a particular format. This line of code tells SwiftUI chart to make use of the digit format:

On prime of that, we added some grid traces by utilizing AxisGridLine.

For the y-axis, as a substitute of show the axis on the trailing (or proper) facet, we need to change it to the main (or left) facet. To do this, connect the chartYAxis modifier like this:

When you’ve made the change, Xcode preview ought to replace the chart like under. The y-axis is moved to the left facet and the format of month is modified. Plus, it’s best to see some grid traces.


Customizing the Background Shade of the Plot Space

The chartPlotStyle modifier means that you can change the background shade of the plot space. Connect the modifier to the Chart view like this:

We are able to then change the plot space utilizing the background modifier. For instance, we alter the plot space to gentle blue.


Making a Multi-line Chart

Now the chart shows a single supply of information (i.e. the climate information of Hong Kong), so how can we show the climate information of London and Taipei in the identical line chart?

You’ll be able to rewrite the code of Chart view like this:

We have now one other ForEach to loop by all of the cities of the chart information. Right here, the foregroundStyle modifier is used to use a unique shade for every line. You don’t must specify the colour. SwiftUI will routinely decide the colour for you.


Proper now, all of the cities share the identical image. If you wish to use a definite image, place the next line of code after foregroundStyle:

Now every metropolis has its personal image within the line chart.


Customizing the Interpolation Methodology

You’ll be able to alter the interpolation technique of the road chart by attaching the interpolationMethod modifier to LineMark. Right here is an instance:

When you change the interpolation technique to .stepStart, the road chart now seems like this:


Apart from .stepStart, you’ll be able to check out the next choices:

  • cardinal
  • catmullRom
  • linear
  • monotone
  • stepCenter
  • stepEnd


The Charts framework is a superb addition to SwiftUI. Even for those who simply start studying SwiftUI, you’ll be able to create pleasant charts with a number of traces of code. Whereas this tutorial focuses on line charts, the Charts API makes it very straightforward for builders to transform the chart to different kinds reminiscent of bar chart. You’ll be able to try the Swift Charts documentation for additional studying.

Observe: We’re updating our Mastering SwiftUI e-book for iOS 16. If you wish to begin studying SwiftUI, try the e-book right here. You’ll obtain a free replace later this yr.



Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments