Skip to content
Activity Heatmap preview
Rendered from the SwiftUI source on this page.

Activity Heatmap

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

A GitHub-style contribution heatmap grid.

Charts · SwiftUI · iOS 17

  • charts
  • swiftui

The actual source

HeatmapChart.swift
// Activity Heatmap · syxUI · https://syxui.dev/components/chart2-heatmap
// Free in any project. Keep this line and credit syxUI where a person can read it.

import SwiftUI

let chGreen = Color(red: 0.16, green: 0.66, blue: 0.40)

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

// Activity heatmap grid (GitHub-style)
struct HeatmapChart: View {
    let levels: [[Int]] = [[0,1,2,3,1,0,2],[1,3,4,2,0,1,3],[2,0,1,4,3,2,1],[0,2,3,1,4,3,0],[3,1,0,2,1,4,2]]
    func shade(_ l: Int) -> Color { l == 0 ? Color.primary.opacity(0.06) : chGreen.opacity(0.25 + Double(l) * 0.19) }
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            HStack { Text("Activity").font(.system(size: 15, weight: .semibold)); Spacer(); Text("Last 5 weeks").font(.system(size: 12)).foregroundStyle(.secondary) }
            VStack(spacing: 5) {
                ForEach(Array(levels.enumerated()), id: \.offset) { _, row in
                    HStack(spacing: 5) {
                        ForEach(Array(row.enumerated()), id: \.offset) { _, l in
                            RoundedRectangle(cornerRadius: 4).fill(shade(l)).frame(width: 30, height: 30)
                        }
                    }
                }
            }
        }.formaCard().frame(width: 300)
    }
}
iOS 17 · No dependencies

Dependencies

SwiftUI
No external dependencies

Requires iOS 17