Skip to content
Comparison Bars preview
Rendered from the SwiftUI source on this page.

Comparison Bars

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

Horizontal this-vs-last comparison bars.

Charts · SwiftUI · iOS 17

  • charts
  • swiftui

The actual source

ComparisonBars.swift
// Comparison Bars · syxUI · https://syxui.dev/components/chart2-comparison
// Free in any project. Keep this line and credit syxUI where a person can read it.

import SwiftUI

let chBlue = Color(red: 0.0, green: 0.47, blue: 1.0)

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)
    }
}

// Horizontal comparison bars (this vs last)
struct ComparisonBars: View {
    let rows: [(String, Double, Double)] = [("Jan", 0.7, 0.5), ("Feb", 0.85, 0.6), ("Mar", 0.6, 0.75), ("Apr", 0.9, 0.7)]
    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            Text("Monthly sales").font(.system(size: 15, weight: .semibold))
            ForEach(Array(rows.enumerated()), id: \.offset) { _, r in
                HStack(spacing: 10) {
                    Text(r.0).font(.system(size: 12)).foregroundStyle(.secondary).frame(width: 28, alignment: .leading)
                    VStack(alignment: .leading, spacing: 4) {
                        Capsule().fill(chBlue).frame(width: CGFloat(220 * r.1), height: 8)
                        Capsule().fill(Color.primary.opacity(0.15)).frame(width: CGFloat(220 * r.2), height: 8)
                    }
                }
            }
        }.formaCard().frame(width: 320)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17