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.
Photo Credit: Apple Documentation
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 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 -
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 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
CEO, Evangelist Apps | Creating seamless app experiences | Build AI apps | Vision Pro | AR | The Climate App | Tech Enthusiast
2yGreat article about Chart in iOS16.