Create a chart using Swift Charts
charts
In WWDC 22 the new Swift Charts framework was announced and it has never been easy to create beautiful Charts. You will need Xcode 14 installed which at the time of writing is in Beta. Let’s get right to it..
In this example, we will show a graph of the average temperatures in Kansas City for the last week. We will create a struct which will hold the day of the week and the average temperature for that day respectively. These will be our X and Y axes on the chart.
Next we will create an array that will hold temperatures for all days Monday thru Sunday.
You can create various types of Charts using the new Charts framework. We will create a Line Chart which will display the average temperature trend over the week.
We can also represent data using points on the Line chart. Swift Charts framework allows us to combine Marks.
Sometimes, showing these temperatures as Bars for each day might make more sense. You can easily do this by
switching LineMark
struct with BarMark
.
If you want to now add a color to your bars or line chart you can use the foregroundStyle
modifier.
And that’s pretty much what you need to get started with Charts.