
Donut Chart
From syxUI — written for both platforms, not translated between them.
A hand-built donut chart — self-contained SwiftUI, no chart framework, restyles from one accent.
Charts · SwiftUI · iOS 17
- charts
- swiftui
The actual source
CardHeader.swift
// Donut Chart · syxUI · https://syxui.dev/components/chart-donut
// Free in any project. Keep this line and credit syxUI where a person can read it.
import SwiftUI
extension View {
func formaCard(_ pad: CGFloat = 18) -> some View {
self.padding(pad)
.background(Color(.systemBackground), in: RoundedRectangle(cornerRadius: 20, style: .continuous))
.overlay(RoundedRectangle(cornerRadius: 20, style: .continuous).stroke(Color.primary.opacity(0.06), lineWidth: 1))
.shadow(color: .black.opacity(0.05), radius: 14, x: 0, y: 6)
}
}
struct CardHeader: View {
let label: String; let value: String; var trend: String? = nil
var body: some View {
HStack(alignment: .firstTextBaseline) {
VStack(alignment: .leading, spacing: 3) {
Text(label).font(.system(size: 13, weight: .medium)).foregroundStyle(.secondary)
Text(value).font(.system(size: 26, weight: .bold))
}
Spacer()
if let trend {
Text(trend).font(.system(size: 12, weight: .semibold))
.padding(.horizontal, 8).padding(.vertical, 3)
.background(Color.primary.opacity(0.06), in: Capsule())
}
}
}
}
// 5. Donut chart
struct DonutChartCard: View {
let segs: [Double] = [0.45, 0.28, 0.17, 0.10]
var body: some View {
HStack(spacing: 18) {
ZStack {
ForEach(segs.indices, id: \.self) { i in
let start = segs[0..<i].reduce(0, +)
Circle().trim(from: start, to: start + segs[i] - 0.006)
.stroke(Color.primary.opacity(1 - Double(i) * 0.24), style: .init(lineWidth: 16, lineCap: .butt))
.rotationEffect(.degrees(-90))
}
VStack(spacing: 0) { Text("$4.2k").font(.system(size: 17, weight: .bold)); Text("spent").font(.system(size: 10)).foregroundStyle(.secondary) }
}
.frame(width: 108, height: 108)
VStack(alignment: .leading, spacing: 8) {
ForEach(["Rent", "Food", "Travel", "Other"].indices, id: \.self) { i in
HStack(spacing: 8) {
Circle().fill(Color.primary.opacity(1 - Double(i) * 0.24)).frame(width: 8, height: 8)
Text(["Rent", "Food", "Travel", "Other"][i]).font(.system(size: 13)).foregroundStyle(.secondary)
Spacer()
Text("\(Int(segs[i] * 100))%").font(.system(size: 13, weight: .medium).monospacedDigit())
}
}
}
}
.formaCard()
}
}
iOS 17 · 2 dependencies
Dependencies
- SwiftUI
- formaCard
- CardHeader
Requires iOS 17


