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

Line + Tooltip

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

A line chart with a highlighted point and value callout.

Charts · SwiftUI · iOS 17

  • charts
  • swiftui

The actual source

LineTooltipChart.swift
// Line + Tooltip · syxUI · https://syxui.dev/components/chart2-line-tooltip
// Free in any project. Keep this line and credit syxUI where a person can read it.

import SwiftUI

let chPurple = Color(red: 0.55, green: 0.40, blue: 0.90)

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

// Line chart with tooltip callout (TikTok/Spotify)
struct LineTooltipChart: View {
    let pts: [Double] = [0.3, 0.5, 0.4, 0.7, 0.55, 0.9, 0.75]
    var body: some View {
        VStack(alignment: .leading, spacing: 10) {
            Text("Impressions").font(.system(size: 15, weight: .semibold))
            GeometryReader { g in
                let w = g.size.width, h = g.size.height
                let step = w / CGFloat(pts.count - 1)
                ZStack {
                    Path { p in
                        for (i, v) in pts.enumerated() {
                            let pt = CGPoint(x: CGFloat(i) * step, y: h - CGFloat(v) * h)
                            if i == 0 { p.move(to: pt) } else { p.addLine(to: pt) }
                        }
                    }.stroke(chPurple, style: StrokeStyle(lineWidth: 2.5, lineCap: .round, lineJoin: .round))
                    let peak = CGPoint(x: 5 * step, y: h - CGFloat(pts[5]) * h)
                    Circle().fill(chPurple).frame(width: 9, height: 9).position(peak)
                    Text("1,204").font(.system(size: 11, weight: .bold)).foregroundStyle(.white).padding(.horizontal, 7).padding(.vertical, 3).background(Color.primary, in: RoundedRectangle(cornerRadius: 6)).position(x: peak.x, y: peak.y - 18)
                }
            }.frame(height: 110)
        }.formaCard().frame(width: 320)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17