IntroductionA bar chart presents categories of data in the form of rectangular bars whose width and height are proportional to the values they represent. This article will show you how to create a vertical bar chart where the height of the rectangle will represent the value of each category. Start chart layoutSwiftUI is very friendly for exploring different layouts and previewing the results in a live view. It’s easy to extract parts of the content into subviews so that each part is small and easy to maintain. Start with a view that will contain the BarChartView and possibly other text or data. This BarChartView contains a title and a chart area, which are represented by text and rounded rectangles. struct ChartView1 : View { struct BarChartView: View { struct ChartHeaderView : View { struct ChartAreaView : View { Add a bar chart to the chart areaDefine some simple data categories, such as the number of steps per day in a week. The following list data is used as the item data of the main view, and each piece of data contains a pair (name, value). In a real app, the data here should be taken from the model through ViewModel. Daily step count data
struct DataItem : Identifiable { Updated BarChartView so data can be passed as a parameter to ChartAreaView struct BarChartView : View { The updated BarChartView requires a list of DataItem . A GeometryReader is used to determine the available heights for the bars. The maximum value in the data is obtained and passed to each BarView. The main chart area remains the original rounded rectangle and overlays a series of bars in a horizontal stack, one for each DataItem . struct ChartAreaView : View { A new view is created for the BarView that creates a bar for each piece of data. It requires the name and value of each piece of data as well as the maximum and available bar height. Each bar is represented as a rounded rectangle, and the bar height is set relative to the maximum bar height. The color of the bar is set to solid blue. struct BarView : View { Screen rotationThe bar chart looks great with sample data. The chart will resize to fit the container view it is in. The same chart can be dropped into any new view without other views, and the icon will fill the space and resize when the device is rotated. struct ChartView3 : View { Chart displayed when the phone is rotated Bar chart of real dataUse real-world data for the bar chart. The ten countries with the highest under-five mortality rates from the UNICEF dataset. Under-five mortality rate: The probability of dying between birth and five years of age, per 1,000 live births Country-selected estimates of under-five mortality rates in 2019
As can be seen, the country names are much longer than the days of the week in the sample data, which uses multiple data names. The data is plotted in a bar chart using the country names. struct ChartView4 : View { Here are some changes made to the BarView. The value on the bar is moved to the top of the bar using an overlay view. The value is offset so the text is not too close to the top of the bar. The font size and weight of the data name can also be set. For longer text like the country names, showing the text below the bar pushes the bar outside the line. The text view width is constrained to the bar width and the bar label text is truncated. The bar text view is also constrained to the bar width and the text can be hidden. struct BarView : View { All country names are truncated, so the data is changed to use country codes instead of country names. The icons are set to a fixed size and the view is embedded in a ScrollView so that it scrolls when the device is rotated. struct ChartView5 : View { ConclusionIt's relatively easy to combine rectangles to create a bar chart in SwiftUI. SwiftUI is a great platform for creating views and quickly refactoring independent subviews. Building a bar chart in SwiftUI requires some work, and as you experiment with bar charts using data, you can determine more customization. Using GeometryReader, you can create bar charts that adapt to more available environments. In this post, we created a simple bar chart with values, labels below, and a title for the chart. The next step is to separate the x-axis and y-axis. |
<<: The official version of iOS 16.1.1 will be released soon, fixing multiple issues!
>>: iOS 16.2 Beta 2 updated with several changes!
User operation is an invisible skill that many ne...
When Chinese people entertain guests, they often ...
Earlier this month, a piece of news once again ar...
The Spring Festival is a day of family happiness....
In recent years, the competition among smartphone...
Let me ask you first: after you unpack the packag...
1.Q: The business entity or special qualification...
Many people feel their hands are very hot after c...
The Tokyo Olympics, which was delayed for a year ...
If you have been optimizing keywords for a long t...
Peak season is here! Search promotion and volume ...
Follow the steps below to publish your own cocoap...
In the SEO industry, the only thing that does not...
Now everyone has begun to pay attention to SEO op...
TikTok has recently started testing a new recomme...