Thursday, July 14, 2022
HomeiOS developmentMaking a Line Chart Utilizing SwiftUI Charts API

Making a Line Chart Utilizing SwiftUI Charts API


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

I’ve briefly mentioned the utilization of the Charts API in this submit. For this tutorial, let’s see how you can use the APIs to create a line chart.

The Climate Information

To show 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 beneath:

The Chart initializer takes in a listing 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 forms of chart utilizing the Charts framework, you must 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 way of all gadgets saved in hkWeatherData. For every merchandise, we create a LineMark object that the x axis is ready to the date and the y axis is ready to the typical temperature.

Optionally, you possibly can resize the chart utilizing the body modifier. Should you preview the code in Xcode preview, you must see the next line chart:

swiftui-line-chart-simple

Customizing Chart Axes

You may customise each x and y axes through the use of the chartXAxis and chartYAxis modifiers respectively. Let’s say, if we wish 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 known as AxisMarks for the values of month. For every worth, we show a worth label through the use of 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 strains through the use of AxisGridLine.

For the y-axis, as an alternative of show the axis on the trailing (or proper) aspect, we wish to swap it to the main (or left) aspect. To do this, connect the chartYAxis modifier like this:

Should you’ve made the change, Xcode preview ought to replace the chart like beneath. The y-axis is moved to the left aspect and the format of month is modified. Plus, you must see some grid strains.

swiftui-charts-changing-axis

Customizing the Background Coloration of the Plot Space

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

We will then change the plot space utilizing the background modifier. For example, we alter the plot space to mild blue.

swiftui-line-chart-background

Making a Multi-line Chart

Now the chart shows a single supply of knowledge (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 may rewrite the code of Chart view like this:

We’ve one other ForEach to loop by way of all of the cities of the chart information. Right here, the foregroundStyle modifier is used to use a distinct shade for every line. You don’t should specify the colour. SwiftUI will robotically decide the colour for you.

swiftui-multi-line-chart

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.

swiftui-line-chart-symbol

Customizing the Interpolation Technique

You may alter the interpolation methodology of the road chart by attaching the interpolationMethod modifier to LineMark. Right here is an instance:

Should you change the interpolation methodology to .stepStart, the road chart now appears like this:

swiftui-chart-interpolation-method

Apart from .stepStart, you possibly can check out the next choices:

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

Abstract

The Charts framework is a superb addition to SwiftUI. Even for those who simply start studying SwiftUI, you possibly can create pleasant charts with a couple of strains of code. Whereas this tutorial focuses on line charts, the Charts API makes it very simple for builders to transform the chart to different varieties resembling bar chart. You may try the Swift Charts documentation for additional studying.

Be aware: 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 12 months.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments