Charts — SwiftUI framework for 
 building charts in iOS 16

Charts — SwiftUI framework for building charts in iOS 16

By Atikur Rehman, Senior iOS Developer at Evangelist Apps Limited.

With the announcement of?Charts 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.

Charts is a powerful framework for visualizing data in our iOS apps. Using the building blocks like?marks,?scales,?axes, and?legends, we can develop a broad range of charts with ease. Currently, the framework supports?Bar,?Area,?Line,?Point, and Rectangle charts out of the box.

No alt text provided for this image

Photo Credit: Apple Documentation

Let’s quickly see an example of how to build a simple bar chart. We will use Xcode 14 beta.

No alt text provided for this image

As you can see in the code example above, we use a?Chart?view. Whether we want to create a bar chart or a line chart, we will need to use that?Chart?view. Next, we use two?BarMark?views to provide the data for our bar chart. Each of the?BarMark?view accepts?x?and?y?values. Using the?x?value, we specify chart data for x-axis, where?y value denotes data for y-axis.

For both?x and?y, we provide something called?PlottableValue. We create instances of?PlottableValue?by using a static function by providing the label key and value.

The above code will result in the following bar chart -

No alt text provided for this image

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 -

No alt text provided for this image

Which will produce the following bar chart -

No alt text provided for this image

We can use?Charts framework on all Apple platforms.

Please share your thoughts and suggestions on this article.

Keep up with us on social media:

Medium: https://medium.com/evangelist-apps

Twitter:?https://twitter.com/EvangelistSW/

#swiftui #ios #iosdeveloper #xcode #iosappdevelopment #swift #iosappdevelopment #iosdevelopers #iosdevelopment #ios16

Amit Ranjan ?

CEO, Evangelist Apps | Creating Next-Gen Mobile Experiences | Innovating with AI & Emerging Technologies | Vision Pro & AR Innovator | Exploring the Future of Tech | Driving The Climate App | Tech Enthusiast

2 年

Great article about Chart in iOS16.

要查看或添加评论,请登录

Evangelist Apps的更多文章

社区洞察

其他会员也浏览了