PrefaceApple introduced SwiftUI Charts at WWWDC 2022, which makes it incredibly easy to create charts in SwiftUI views. Charts are a great way to present visual data in a rich format that is easy to understand. This article shows how to easily create a line chart with much less code than you would have to create the same line chart from scratch before. It's also very easy to customize the look and feel of the chart and make the information in the chart easily accessible. As shown in previous articles, it’s possible to create a line chart without using SwiftUI Charts. However, using the Charts[1] framework makes it much easier by providing a wide range of charts to explore the most effective approach to the data in your app. Series of articles
Simple line chartStart with data that contains step counts for a week, similar to the data used in Create a line chart in SwiftUI. Define a structure to hold the date and the number of steps for that day, and create an array for the current week. struct StepCount : Identifiable { To create a line chart, create a chart with a LineMark for each element in the step data. Specify the weekday in the X value of the LineMark and the number of steps in the Y value. Note that you also need to import the Charts framework. This creates a line chart for the step data. Since there is only one series of data, the ForEach can be omitted and the data can be passed directly to the Chart initializer. Both parts produce the same line chart. import SwiftUI A line chart showing daily step count created using SwiftUI Charts Other chartsSwiftUI Charts has many chart options available. These can be used to generate bar charts by changing the chart marker from LineMark to other types of markers such as BarMark. struct OtherCharts : View { Other chart types created with SwiftUI Charts, showing daily step count Making line charts more accessibleOne benefit of building the chart into SwiftUI is that it’s easy to make the chart accessible using accessibility modifiers [2]. Add a computed property for StepCount that returns the data as a string that can be used by accessibilityLabel. Then add accessibility labels and values for each marker in the chart. struct StepCount : Identifiable { GroupBox ( "Line Chart - Daily Step Count" ) { Making Line Charts Accessible in SwiftUI Charts Add multiple data series to a line chartA line chart is a great way to compare two different series of data. Create a second series, the number of steps from the previous week, and add both series to the line chart. let previousWeek : [ StepCount ] = [ The first attempt to add the data for these two series did not display as expected. struct LineChart2 : View { First attempt at creating a line chart with two series of step data in SwiftUI Charts Display step seriesDisplay multiple weekday-based step count series in a line chart The problem with my initial attempt to display multiple sets of data in a line chart was that the X-axis used dates. The current week number followed the previous week, so each point was plotted linearly along the X-axis. It is necessary to use only weekdays as the x-axis values, so that all Sundays are plotted on the same x-coordinate. Add another calculated property to StepCount to return the short day of the weekday in string format. struct StepCount : Identifiable { This shortDay is used for the x-values of the LineMarks in the chart. Additionally, the foreground is styled as a period based on the stepCount array. The line chart uses the weekdays on the x-axis to show the number of steps for two weeks, allowing comparisons between weeks. struct LineChart3 : View { Line chart with two series of step data in SwiftUI Charts in conclusionThere’s a lot more to explore in SwiftUI Charts. Using this framework is definitely better than building your own charts from scratch. References[1] Charts: https://developer.apple.com/documentation/charts. [2] Accessibility modifiers: https://developer.apple.com/documentation/swiftui/view-accessibility. |
<<: Let's talk about the three new font width styles in iOS 16
>>: Solution to resource confusion in Android plug-in
This topic has been popular on the Internet for a...
For those who are new to operations or have just ...
Brands such as Perfect Diary , Hua Xizi, and Cha ...
In 2020, from Perfect Diary in the beauty industr...
Recently, the news that "QQ accounts can be ...
Guidelines for the public to wear masks to preven...
According to reports, Apple plans to sell accessi...
"Celadon" is adapted from the novel of ...
Tencent's motivation for making TV Sun Peng, ...
Recently, Mengshi Technology and CATL officially ...
Mixed Knowledge Specially designed to cure confus...
What I want to talk about are the 6 core ideas ab...
Brief introduction of 100 sets of resources for t...
At present, Zhihu is the platform that can best s...
Nowadays, it is becoming more and more convenient...