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!
WeChat is used every day, but do you know how man...
On April 15, "Overseas Flight Attendant Ling...
gossip "Noodles that don't get soft afte...
The 2nd anniversary celebration of Honor of Kings...
Just as there are a thousand Hamlets in the eyes ...
Toutiao is one of the important platforms of self...
The furthest distance in the world is not that yo...
Since mid-August, a large-scale epidemic has occu...
Before learning Zookeeper in depth, I would like ...
@Mr. Dongpo, your farming equipment has been &quo...
Produced by: Science Popularization China Author:...
Since 2013, the term "autonomous driving&quo...
Source code introduction: This is a demo modeled ...
MP3 and other devices for listening to music at a...
In this era where we can connect to the world wit...