Skip to content
Stat Breakdown preview
Rendered from the SwiftUI source on this page.

Stat Breakdown

From syxUI — written for both platforms, not translated between them.

A metric row that expands to a mini bar breakdown.

Accordions · SwiftUI · iOS 17

  • accordion
  • swiftui

The actual source

AccStat.swift
// Stat Breakdown · syxUI · https://syxui.dev/components/acc-stat
// 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)
    }
}

// Stat row expands to a mini breakdown
struct AccStat: View {
    @State private var open = true
    let green = Color(red: 0.16, green: 0.66, blue: 0.40)
    let months = [("Apr", 0.5), ("May", 0.7), ("Jun", 0.6), ("Jul", 0.9)]
    var body: some View {
        VStack(spacing: 12) {
            HStack {
                VStack(alignment: .leading, spacing: 2) {
                    Text("Revenue").font(.system(size: 13)).foregroundStyle(.secondary)
                    Text("$48,290").font(.system(size: 22, weight: .bold))
                }
                Spacer()
                Image(systemName: "chevron.down").font(.system(size: 13, weight: .semibold)).foregroundStyle(.secondary).rotationEffect(.degrees(open ? 180 : 0))
            }.contentShape(Rectangle()).onTapGesture { open.toggle() }
            if open {
                HStack(alignment: .bottom, spacing: 10) {
                    ForEach(Array(months.enumerated()), id: \.offset) { _, m in
                        VStack(spacing: 6) {
                            Capsule().fill(green).frame(width: 22, height: CGFloat(40 * m.1))
                            Text(m.0).font(.system(size: 11)).foregroundStyle(.secondary)
                        }
                    }
                }.frame(maxWidth: .infinity)
            }
        }.formaCard().frame(width: 300)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17