### Swift: Use ACDataProvider for AxisContribution Source: https://context7.com/jasudev/axiscontribution/llms.txt Shows how to use the shared ACDataProvider to map source data into the format required by AxisContribution. This example generates random dates and counts, then uses `ACDataProvider.shared.mappedData` to prepare it for the `AxisContribution` view. ```swift import SwiftUI import AxisContribution struct DataProviderExample: View { @State private var mappedData: [[ACData]] = [] var body: some View { VStack { if !mappedData.isEmpty { Text("Generated \(mappedData.count) weeks of data") .font(.caption) AxisContribution( constant: .init(axisMode: .horizontal), external: mappedData ) .frame(height: 150) } } .padding() .onAppear { let constant = ACConstant( from: Calendar.current.date(byAdding: .year, value: -1, to: Date()), to: Date(), spacing: 4, levelSpacing: 3, axisMode: .horizontal ) var sourceDates: [Date: ACData] = [: ] for i in 0..<300 { let date = Date.randomDate(daysBack: 365) sourceDates[date.startOfDay] = ACData( date: date.startOfDay, count: Int.random(in: 0...12) ) } mappedData = ACDataProvider.shared.mappedData( constant: constant, source: sourceDates ) } } } extension Date { static func randomDate(daysBack: Int) -> Date { let randomDays = Int.random(in: 0...daysBack) return Calendar.current.date(byAdding: .day, value: -randomDays, to: Date()) ?? Date() } } ``` -------------------------------- ### Customizable AxisContribution with Custom Views in SwiftUI Source: https://context7.com/jasudev/axiscontribution/llms.txt Illustrates how to create a highly customized AxisContribution component in SwiftUI by providing custom background and foreground views using closures. This example also configures specific styling options like font size and level label format, and generates random contribution data. ```swift import SwiftUI import AxisContribution struct CustomContributionView: View { @Environment(\.colorScheme) private var colorScheme @State private var contributions: [Date: ACData] = [:] var body: some View { AxisContribution( constant: .init( axisMode: .horizontal, spacing: 4, levelSpacing: 3, font: .system(size: 9), showLevelView: true, levelLabel: .number ), source: contributions ) { indexSet, data in // Custom background view Image(systemName: "heart.fill") .foregroundColor(Color(hex: colorScheme == .dark ? 0x171B21 : 0xF0F0F0)) .font(.system(size: 11)) .frame(width: 11, height: 11) } foreground: { indexSet, data in // Custom foreground view with contribution data Image(systemName: "heart.fill") .foregroundColor(Color(hex: 0x6CD164)) .font(.system(size: 11)) .frame(width: 11, height: 11) } .padding() .onAppear { contributions = generateContributions() } } private func generateContributions() -> [Date: ACData] { var data: [Date: ACData] = [:] let today = Date() for i in 0..<200 { let randomDays = Int.random(in: 0...365) if let date = Calendar.current.date(byAdding: .day, value: -randomDays, to: today) { data[date.startOfDay] = ACData( date: date.startOfDay, count: Int.random(in: 1...12) ) } } return data } } // Color extension for hex values extension Color { init(hex: UInt, alpha: Double = 1.0) { self.init( .sRGB, red: Double((hex >> 16) & 0xff) / 255, green: Double((hex >> 8) & 0xff) / 255, blue: Double((hex & 0xff)) / 255, opacity: alpha ) } } ``` -------------------------------- ### Basic AxisContribution Usage in SwiftUI Source: https://context7.com/jasudev/axiscontribution/llms.txt Demonstrates the basic implementation of the AxisContribution component in SwiftUI, showing how to initialize it with default styling and populate it with sample contribution data. It utilizes the 'ACData' model to represent daily contributions and displays them in a horizontal axis mode. ```swift import SwiftUI import AxisContribution struct MyContributionView: View { @State private var contributions: [Date: ACData] = [:] var body: some View { // Basic usage with default styling AxisContribution( constant: .init( axisMode: .horizontal, spacing: 4, levelSpacing: 3, showLevelView: true ), source: contributions ) .onAppear { // Generate sample contribution data let calendar = Calendar.current let today = Date() var data: [Date: ACData] = [:] for dayOffset in 0..<365 { if let date = calendar.date(byAdding: .day, value: -dayOffset, to: today) { let count = Int.random(in: 0...15) data[date.startOfDay] = ACData(date: date.startOfDay, count: count) } } contributions = data } } } ``` -------------------------------- ### Configure AxisContribution Calendar in Swift Source: https://context7.com/jasudev/axiscontribution/llms.txt Demonstrates how to configure the AxisContribution calendar with various settings like date range, spacing, axis mode (horizontal/vertical), font, and level view options. It also includes a helper function to simulate realistic contribution data. ```swift import SwiftUI import AxisContribution struct ConfiguredContributionView: View { @State private var contributions: [Date: ACData] = [:]; var body: some View { VStack(spacing: 20) { // Horizontal mode with custom date range AxisContribution( constant: .init( from: Calendar.current.date(byAdding: .month, value: -6, to: Date()), to: Date(), spacing: 3, levelSpacing: 5, axisMode: .horizontal, font: .system(size: 8), showLevelView: true, levelLabel: .number, showYearForJan: true ), source: contributions ) .frame(height: 150) // Vertical mode AxisContribution( constant: .init( from: Calendar.current.date(byAdding: .month, value: -3, to: Date()), to: Date(), spacing: 4, levelSpacing: 3, axisMode: .vertical, font: .system(size: 9), showLevelView: true, levelLabel: .moreOrLess ), source: contributions ) .frame(width: 200) } .padding() .onAppear { contributions = loadContributionData() } } private func loadContributionData() -> [Date: ACData] { var data: [Date: ACData] = [:]; let calendar = Calendar.current; let today = Date(); // Simulate realistic contribution pattern for dayOffset in 0..<180 { if let date = calendar.date(byAdding: .day, value: -dayOffset, to: today) { // Higher activity on weekdays let weekday = calendar.component(.weekday, from: date); let isWeekday = weekday >= 2 && weekday <= 6; let maxCount = isWeekday ? 20 : 5; let count = Bool.random() ? Int.random(in: 1...maxCount) : 0; if count > 0 { data[date.startOfDay] = ACData(date: date.startOfDay, count: count); } } } return data; } } ``` -------------------------------- ### Manage ACData Model for Contributions in Swift Source: https://context7.com/jasudev/axiscontribution/llms.txt Shows how to use the ACData model to represent contribution entries and dynamically update the AxisContribution calendar. Includes functions to add new contributions and clear all data, along with a Date extension for utility. ```swift import SwiftUI import AxisContribution struct ContributionDataExample: View { @State private var contributions: [Date: ACData] = [:]; @State private var totalContributions: Int = 0; var body: some View { VStack(spacing: 16) { Text("Total Contributions: \(totalContributions)") .font(.headline) AxisContribution( constant: .init( axisMode: .horizontal, levelSpacing: 3 ), source: contributions ) .frame(height: 150) Button("Add Today's Contribution") { addContribution() } Button("Clear All") { contributions.removeAll() totalContributions = 0 } } .padding() } private func addContribution() { let today = Date().startOfDay; if let existing = contributions[today] { // Increment existing contribution contributions[today] = ACData( date: today, count: existing.count + 1 ); } else { // Create new contribution contributions[today] = ACData( date: today, count: 1 ); } // Recalculate total totalContributions = contributions.values.reduce(0) { $0 + $1.count }; } } // Date extension for start of day extension Date { var startOfDay: Date { Calendar.current.startOfDay(for: self); } } ``` -------------------------------- ### Initialize AxisContribution in Swift Source: https://github.com/jasudev/axiscontribution/blob/main/README.md Initializes the AxisContribution view with custom constants, a source array of dates, and optional custom views for foreground and background elements. Supports iOS and macOS. ```swift AxisContribution(constant: .init(), source: [Date(), Date()]) // or AxisContribution(constant: .init(), source: [Date(), Date()]) { indexSet, data in Image(systemName: "heart.fill") .foregroundColor(Color(hex: colorScheme == .dark ? 0x171B21 : 0xF0F0F0)) .font(.system(size: rowSize)) .frame(width: rowSize, height: rowSize) } foreground: { indexSet, data in Image(systemName: "heart.fill") .foregroundColor(Color(hex: 0x6CD164)) .font(.system(size: rowSize)) .frame(width: rowSize, height: rowSize) } ``` -------------------------------- ### SwiftUI Interactive Contribution Calendar Source: https://context7.com/jasudev/axiscontribution/llms.txt A SwiftUI View that displays an interactive contribution calendar. It allows users to generate random contributions, simulate a year's activity, clear data, and customize the calendar's appearance and orientation. Dependencies include SwiftUI and AxisContribution. ```swift import SwiftUI import AxisContribution struct InteractiveContributionView: View { @Environment(\.colorScheme) private var colorScheme @State private var contributions: [Date: ACData] = [: ] @State private var axisMode: ACAxisMode = .horizontal @State private var rowSize: CGFloat = 11 @State private var useCustomViews: Bool = false @State private var selectedDateInfo: String = "No selection" var body: some View { VStack(spacing: 20) { Text(selectedDateInfo) .font(.headline) .padding() if useCustomViews { AxisContribution( constant: .init( spacing: max(2, rowSize / 3), levelSpacing: 3, axisMode: axisMode, showLevelView: true, levelLabel: .number ), source: contributions ) { indexSet, data in Circle() .fill(Color.gray.opacity(0.2)) .frame(width: rowSize, height: rowSize) } foreground: { indexSet, data in Circle() .fill(Color.blue) .frame(width: rowSize, height: rowSize) .onTapGesture { if let data = data { selectedDateInfo = formatDate(data.date) + " - " + String(data.count) + " contributions" } } } } else { AxisContribution( constant: .init( spacing: 4, levelSpacing: 3, axisMode: axisMode, showLevelView: true ), source: contributions ) } Spacer() VStack(spacing: 12) { Toggle("Custom Views", isOn: $useCustomViews) if useCustomViews { HStack { Text("Size: " + String(Int(rowSize))) Slider(value: $rowSize, in: 8...30, step: 1) } } Picker("Orientation", selection: $axisMode) { Text("Horizontal").tag(ACAxisMode.horizontal) Text("Vertical").tag(ACAxisMode.vertical) } .pickerStyle(.segmented) HStack { Button("Generate Random") { contributions = generateRandomContributions() } Button("Simulate Year") { contributions = simulateYearActivity() } Button("Clear") { contributions.removeAll() selectedDateInfo = "No selection" } } } .padding() } .padding() .onAppear { contributions = generateRandomContributions() } } private func generateRandomContributions() -> [Date: ACData] { var data: [Date: ACData] = [: ] for _ in 0..<200 { let randomDays = Int.random(in: 0...365) if let date = Calendar.current.date(byAdding: .day, value: -randomDays, to: Date()) { data[date.startOfDay] = ACData( date: date.startOfDay, count: Int.random(in: 1...20) ) } } return data } private func simulateYearActivity() -> [Date: ACData] { var data: [Date: ACData] = [: ] let calendar = Calendar.current for dayOffset in 0..<365 { if let date = calendar.date(byAdding: .day, value: -dayOffset, to: Date()) { let weekday = calendar.component(.weekday, from: date) let isWeekend = weekday == 1 || weekday == 7 // 70% chance of contribution on weekdays, 30% on weekends let shouldContribute = isWeekend ? Double.random(in: 0...1) < 0.3 : Double.random(in: 0...1) < 0.7 if shouldContribute { data[date.startOfDay] = ACData( date: date.startOfDay, count: Int.random(in: 1...15) ) } } } return data } private func formatDate(_ date: Date) -> String { let formatter = DateFormatter() formatter.dateStyle = .medium return formatter.string(from: date) } } ``` -------------------------------- ### Swift: Use External Data with AxisContribution Source: https://context7.com/jasudev/axiscontribution/llms.txt Demonstrates how to provide pre-formatted external data to the AxisContribution view. It generates 52 weeks of sample data, with each week containing 7 days of random counts, and passes this to the `external` parameter of the AxisContribution view. ```swift import SwiftUI import AxisContribution struct ExternalDataExample: View { @State private var externalData: [[ACData]] = [] var body: some View { AxisContribution( constant: .init( axisMode: .horizontal, spacing: 4, levelSpacing: 4 ), external: externalData ) .frame(height: 150) .padding() .onAppear { generateExternalData() } } private func generateExternalData() { let calendar = Calendar.current let today = Date() var weeklyData: [[ACData]] = [] // Create 52 weeks of data for weekOffset in (0..<52).reversed() { var weekData: [ACData] = [] // Create 7 days per week for dayOfWeek in 0..<7 { let totalOffset = -(weekOffset * 7 + dayOfWeek) if let date = calendar.date(byAdding: .day, value: totalOffset, to: today) { let count = Int.random(in: 0...15) weekData.append(ACData(date: date.startOfDay, count: count)) } } weeklyData.append(weekData) } externalData = weeklyData } } ``` -------------------------------- ### Add AxisContribution as Swift Package Manager Dependency Source: https://github.com/jasudev/axiscontribution/blob/main/README.md Adds the AxisContribution library as a dependency to your Swift project using Swift Package Manager. This is the recommended way to integrate the library. ```swift dependencies: [ .package(url: "https://github.com/jasudev/AxisContribution.git", .branch("main")) ] ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.