Skip to content
Line Chart preview
Rendered from the SwiftUI source on this page.

Line Chart

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

A hand-built line chart — self-contained SwiftUI, no chart framework, restyles from one accent.

Charts · SwiftUI · iOS 17

  • charts
  • swiftui

The actual source

CardHeader.swift
// Line Chart · syxUI · https://syxui.dev/components/chart-line
// 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())
            }
        }
    }
}
// 2. Line chart
struct LineChartCard: View {
    let pts: [Double] = [0.30, 0.42, 0.36, 0.52, 0.46, 0.72, 0.66, 0.9]
    var body: some View {
        VStack(alignment: .leading, spacing: 14) {
            CardHeader(label: "Revenue", value: "$48,290", trend: "+24%")
            GeometryReader { geo in
                let w = geo.size.width, h = geo.size.height
                let p = pts.enumerated().map { i, v in
                    CGPoint(x: w * CGFloat(i) / CGFloat(pts.count - 1), y: h * (1 - CGFloat(v)))
                }
                ZStack {
                    Path { path in
                        path.move(to: CGPoint(x: 0, y: h)); p.forEach { path.addLine(to: $0) }
                        path.addLine(to: CGPoint(x: w, y: h)); path.closeSubpath()
                    }.fill(LinearGradient(colors: [Color.primary.opacity(0.14), Color.primary.opacity(0)], startPoint: .top, endPoint: .bottom))
                    Path { path in path.move(to: p[0]); p.dropFirst().forEach { path.addLine(to: $0) } }
                        .stroke(Color.primary, style: .init(lineWidth: 2.5, lineCap: .round, lineJoin: .round))
                    Circle().fill(Color.primary).frame(width: 9, height: 9).position(p.last!)
                }
            }
            .frame(height: 96)
        }
        .formaCard()
    }
}
iOS 17 · 2 dependencies

Dependencies

SwiftUI
  • formaCard
  • CardHeader

Requires iOS 17