Charts — SwiftUI framework for building charts in iOS 16
With the announcement of framework in WWDC 2022, we can now build customizable charts in SwiftUI easily. We no longer need to use any third-party chart library or build our own.
is a powerful framework for visualizing data in our iOS apps. Using building blocks like , , , and , we can develop a broad range of charts with ease. Currently, the framework supports , , , , and charts out of the box.
Let’s quickly see an example of how to build a simple bar chart. We will use Xcode 14 beta.
As you can see in the code example above, we use a view. Whether we want to create a bar chart or a line chart, we will need to use that view. Next, we use two views to provide the data for our bar chart. Each of the view accepts and values. Using the value, we specify chart data for x-axis, where value denotes data for y-axis.
For both and , we provide something called . We create instances of by using a function by providing the label key and value.
The above code will result in the following bar chart -
As you can see, we can build charts with just a few lines of code in a declarative way.
Let’s take a look at the following code to see how we can use collections of data to generate a bar chart -
Which will produce the following bar chart -
We can use framework on all Apple platforms.
Please share your thoughts and suggestions on this article.