Charts — SwiftUI framework for building charts in iOS 16
Evangelist Apps
?? We craft mobile apps, full-stack web solutions & AI-powered innovations to accelerate business and digital success!
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/
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.