Components

Stats

A dashboard: KPI tiles, a chart with a period picker and a breakdown list. Built in all 5 Effect UI styles, in light and dark mode.

Stats in Prism, light mode
Stats in Prism, dark mode
Prism ProLiquid Glass on living colour.
Stats in Ledger, light mode
Stats in Ledger, dark mode
Ledger FreeCalm, precise, trustworthy.
Stats in Bounce, light mode
Stats in Bounce, dark mode
Bounce ProChunky, tactile, joyful.
Stats in Folio, light mode
Stats in Folio, dark mode
Folio ProEditorial and literary.
Stats in Bloom, light mode
Stats in Bloom, dark mode
Bloom ProSoft, organic, calming.

Source: Ledger

StatsView.swift in the free Ledger style. It needs Ledger's Theme.swift, which comes with the style: join early access on the home page and it's emailed to you.

import SwiftUI
import Charts

// MARK: - Sample data

private struct Bucket: Identifiable {
    let id: Int
    let label: String
    let amount: Double
}

private struct CategoryShare: Identifiable {
    var id: String { name }
    let name: String
    let symbol: String
    let amount: Double
}

private enum StatsPeriod: String, CaseIterable, Identifiable {
    case week = "Week", month = "Month", year = "Year"
    var id: String { rawValue }

    var buckets: [Bucket] {
        let pairs: [(String, Double)]
        switch self {
        case .week:
            pairs = [("Sat", 48.20), ("Sun", 126.75), ("Mon", 31.40), ("Tue", 212.90),
                     ("Wed", 64.15), ("Thu", 18.60), ("Fri", 110.40)]
        case .month:
            pairs = [("Nov 1", 1612.15), ("Nov 8", 455.40), ("Nov 15", 402.35), ("Nov 22", 376.40)]
        case .year:
            pairs = [("D", 2712.40), ("J", 2785.75), ("F", 2606.45), ("M", 2921.70),
                     ("A", 2776.55), ("M", 3172.35), ("J", 2665.35), ("J", 2958.35),
                     ("A", 2702.45), ("S", 3148.45), ("O", 2912.80), ("N", 2846.30)]
        }
        return pairs.enumerated().map { Bucket(id: $0.offset, label: $0.element.0, amount: $0.element.1) }
    }

    var total: Double { buckets.reduce(0) { $0 + $1.amount } }

    var days: Double {
        switch self {
        case .week: return 7
        case .month: return 27
        case .year: return 365
        }
    }

    var transactions: Int {
        switch self {
        case .week: return 23
        case .month: return 94
        case .year: return 1106
        }
    }

    var largest: (amount: Double, merchant: String) {
        switch self {
        case .week: return (164.22, "Costco, Nov 22")
        case .month, .year: return (1250.00, "Halstead Property")
        }
    }

    var label: String {
        switch self {
        case .week: return "Spent · Nov 21 – 27"
        case .month: return "Spent · November to date"
        case .year: return "Spent · Dec 2025 – Nov 2026"
        }
    }

    /// Change versus the previous period; negative means less spent.
    var change: (amount: Double, percent: Double, against: String) {
        switch self {
        case .week: return (-88.35, -12.6, "than last week")
        case .month: return (-262.15, -8.4, "than October")
        case .year: return (1537.25, 4.7, "than the prior year")
        }
    }

    var categories: [CategoryShare] {
        switch self {
        case .week:
            return [
                CategoryShare(name: "Groceries", symbol: "cart.fill", amount: 184.32),
                CategoryShare(name: "Shopping", symbol: "bag.fill", amount: 142.10),
                CategoryShare(name: "Dining out", symbol: "fork.knife", amount: 96.75),
                CategoryShare(name: "Utilities", symbol: "bolt.fill", amount: 92.18),
                CategoryShare(name: "Transport", symbol: "tram.fill", amount: 58.40),
            ]
        case .month:
            return [
                CategoryShare(name: "Housing", symbol: "house.fill", amount: 1250.00),
                CategoryShare(name: "Groceries", symbol: "cart.fill", amount: 412.80),
                CategoryShare(name: "Shopping", symbol: "bag.fill", amount: 318.60),
                CategoryShare(name: "Dining out", symbol: "fork.knife", amount: 286.45),
                CategoryShare(name: "Utilities", symbol: "bolt.fill", amount: 162.35),
            ]
        case .year:
            return [
                CategoryShare(name: "Housing", symbol: "house.fill", amount: 15000.00),
                CategoryShare(name: "Groceries", symbol: "cart.fill", amount: 5126.40),
                CategoryShare(name: "Shopping", symbol: "bag.fill", amount: 3512.80),
                CategoryShare(name: "Dining out", symbol: "fork.knife", amount: 3388.15),
                CategoryShare(name: "Travel", symbol: "airplane", amount: 2410.00),
            ]
        }
    }

    var categoryCount: Int { self == .week ? 9 : 12 }
}

// MARK: - Screen

struct StatsView: View {
    @State private var period: StatsPeriod = .month

    var body: some View {
        ScrollView {
            VStack(alignment: .leading, spacing: Theme.Space.xxl) {
                VStack(alignment: .leading, spacing: Theme.Space.xl) {
                    ScreenHeader(eyebrow: "Insights", title: "Spending") {
                        IconButton(symbol: "square.and.arrow.up")
                    }
                    Picker("Period", selection: $period) {
                        ForEach(StatsPeriod.allCases) { Text($0.rawValue).tag($0) }
                    }
                    .pickerStyle(.segmented)
                }
                hero
                kpiGrid
                chartCard
                breakdown
            }
            .padding(.horizontal, Theme.Space.margin)
            .padding(.top, Theme.Space.sm)
            .padding(.bottom, Theme.Space.xxl)
        }
        .scrollIndicators(.hidden)
        .paperBackground()
        .animation(Theme.Motion.surfaceEnter, value: period)
        .sensoryFeedback(.selection, trigger: period)
    }

    // MARK: Hero

    private var hero: some View {
        let change = period.change
        let less = change.amount < 0
        return VStack(alignment: .leading, spacing: Theme.Space.sm) {
            MicroLabel(period.label)
                .contentTransition(.opacity)
            HeroAmount(value: period.total)
            HStack(spacing: Theme.Space.xs) {
                Image(systemName: less ? "arrow.down.right" : "arrow.up.right")
                    .font(.system(size: 11, weight: .semibold))
                Text("\(Money.format(abs(change.amount), decimals: 2)) \(less ? "less" : "more") (\(Money.percent(change.percent)))")
                    .monospacedDigit()
                    .contentTransition(.numericText(value: change.amount))
                Text(change.against)
                    .foregroundStyle(Theme.onSurfaceMuted)
            }
            .font(.system(size: 15, weight: .regular))
            .foregroundStyle(less ? Theme.positive : Theme.negative)
            .lineLimit(1)
            .minimumScaleFactor(0.8)
        }
    }

    // MARK: KPI tiles

    private var kpiGrid: some View {
        let largest = period.largest
        return VStack(spacing: 0) {
            Hairline(strong: true)
            HStack(spacing: 0) {
                KPITile(label: "Daily average", value: Money.format(period.total / period.days, decimals: 2), note: "over \(Int(period.days)) days")
                Rectangle().fill(Theme.rule).frame(width: 0.5)
                KPITile(label: "Transactions", value: period.transactions.formatted(), note: "\(Money.format(period.total / Double(period.transactions), decimals: 2)) on average")
                    .padding(.leading, Theme.Space.lg)
            }
            .fixedSize(horizontal: false, vertical: true)
            Hairline()
            HStack(spacing: 0) {
                KPITile(label: "Largest", value: Money.format(largest.amount, decimals: 2), note: largest.merchant)
                Rectangle().fill(Theme.rule).frame(width: 0.5)
                KPITile(label: "Top category", value: period.categories.first?.name ?? "", note: String(format: "%.1f%% of spend", 100 * (period.categories.first?.amount ?? 0) / period.total))
                    .padding(.leading, Theme.Space.lg)
            }
            .fixedSize(horizontal: false, vertical: true)
            Hairline()
        }
    }

    // MARK: Chart

    private var chartCard: some View {
        VStack(alignment: .leading, spacing: Theme.Space.lg) {
            HStack(alignment: .firstTextBaseline) {
                MicroLabel(period == .year ? "By month" : period == .month ? "By week" : "By day")
                Spacer()
                Text("Latest highlighted")
                    .ledgerType(.bodySm)
                    .foregroundStyle(Theme.onSurfaceMuted)
            }
            SpendBars(buckets: period.buckets)
                .id(period)
                .transition(.opacity)
        }
        .ledgerCard()
    }

    // MARK: Breakdown

    private var breakdown: some View {
        let cats = period.categories
        return VStack(alignment: .leading, spacing: 0) {
            SectionHeader(title: "Where it went", detail: "Top 5 of \(period.categoryCount)")
            RuledStack {
                ForEach(cats.indices, id: \.self) { i in
                    RankRow(rank: i + 1, share: cats[i], total: period.total, leader: cats[0].amount)
                }
            }
        }
    }
}

// MARK: - Pieces

private struct KPITile: View {
    let label: String
    let value: String
    let note: String

    var body: some View {
        VStack(alignment: .leading, spacing: Theme.Space.xs) {
            MicroLabel(label)
            Text(value)
                .ledgerType(.numeralLg)
                .foregroundStyle(Theme.onSurface)
                .lineLimit(1)
                .minimumScaleFactor(0.7)
                .contentTransition(.numericText())
            Text(note)
                .ledgerType(.bodySm)
                .monospacedDigit()
                .foregroundStyle(Theme.onSurfaceVariant)
                .lineLimit(1)
        }
        .frame(maxWidth: .infinity, alignment: .leading)
        .padding(.vertical, Theme.Space.lg)
        .accessibilityElement(children: .combine)
    }
}

/// Bar chart: muted ink bars, the latest in primary, dashed baseline only.
private struct SpendBars: View {
    let buckets: [Bucket]

    var body: some View {
        let latest = buckets.last?.id
        Chart {
            ForEach(buckets) { b in
                BarMark(
                    x: .value("Period", b.label),
                    y: .value("Spent", b.amount),
                    width: .ratio(0.56)
                )
                .foregroundStyle(b.id == latest ? Theme.primaryInk : Theme.onSurfaceMuted.opacity(0.45))
                .cornerRadius(2)
            }
            RuleMark(y: .value("Baseline", 0))
                .foregroundStyle(Theme.onSurfaceMuted)
                .lineStyle(StrokeStyle(lineWidth: 0.5, dash: [2, 3]))
        }
        .chartYAxis(.hidden)
        .chartYScale(domain: 0...((buckets.map(\.amount).max() ?? 1) * 1.08))
        .chartXAxis {
            AxisMarks { value in
                AxisValueLabel {
                    if let label = value.as(String.self) {
                        Text(label.uppercased())
                            .ledgerType(.labelSm)
                            .foregroundStyle(Theme.onSurfaceMuted)
                    }
                }
            }
        }
        .frame(height: 160)
        .accessibilityElement(children: .ignore)
        .accessibilityLabel("Bar chart of spending")
    }
}

private struct RankRow: View {
    let rank: Int
    let share: CategoryShare
    let total: Double
    let leader: Double

    var body: some View {
        VStack(alignment: .leading, spacing: Theme.Space.sm) {
            HStack(spacing: Theme.Space.md) {
                Text(String(format: "%02d", rank))
                    .font(.system(size: 13, weight: .regular, design: .monospaced))
                    .foregroundStyle(Theme.onSurfaceMuted)
                    .frame(width: 20, alignment: .leading)
                SymbolTile(symbol: share.symbol)
                Text(share.name)
                    .ledgerType(.bodyMd)
                    .foregroundStyle(Theme.onSurface)
                Spacer(minLength: Theme.Space.sm)
                VStack(alignment: .trailing, spacing: Theme.Space.xxs) {
                    AmountText(value: share.amount)
                    Text(String(format: "%.1f%%", 100 * share.amount / total))
                        .font(.system(size: 13, weight: .regular, design: .monospaced))
                        .foregroundStyle(Theme.onSurfaceMuted)
                }
            }
            LedgerProgress(fraction: share.amount / leader)
                .padding(.leading, 20 + 32 + 2 * Theme.Space.md)
        }
        .padding(.vertical, Theme.Space.md)
        .accessibilityElement(children: .combine)
    }
}

#Preview("Light") {
    StatsView()
}

#Preview("Dark") {
    StatsView().preferredColorScheme(.dark)
}

More screens