How to make Pie and Donut chart using SwiftUI?
2 min readSep 14, 2020
Xcode 11+ — iOS 13+
In this tutorial, we are going to implement Pie chart. Later, we will migrate same Pie chart to donut chart.
We are going to divide our task into sub-tasks like following:
- Create Circle segment. These circle segment we are going to use as chart cell/bar.
- Implement flow layout logic to add Circle segments in continuous manner.
- Add tap gesture to show chart cell details
Create Circle segment:
- Create a Shape struct.
- Add arc from center of the view.
- Connect Arc edges to center
Implement flow layout logic
- Map chart data to 360 scale
- Set starting angle to 0
- Evaluate map cell data on 360 scale
- Move starting angle to previous cell end point.