Introduction
Relevant source files
The following files were used as context for generating this wiki page:
- README.md
- Sources/SwiftDitherKit/SwiftDitherKit.docc/SwiftDitherKit.md
- Sources/SwiftDitherKit/DitherCartesianCharts.swift
- Sources/SwiftDitherKit/DitherTypes.swift
- Sources/SwiftDitherKit/DitherMath.swift
- Sources/SwiftDitherKit/DitherMotion.swift
- Sources/SwiftDitherKit/DitherAccessibility.swift
- Sources/SwiftDitherKit/SwiftDitherKit.docc/InteractionAndAccessibility.md
- Sources/SwiftDitherKit/SwiftDitherKit.docc/GettingStarted.md
SwiftDitherKit is a dependency-free SwiftUI library designed for rendering charts using ordered-dither textures. It provides a variety of chart types, including area, line, bar, pie, donut, radar, and sparkline views. The library is based on Dither Kit and focuses on providing high-performance, aesthetically unique visualizations by rendering and caching dithered images away from the main actor.
Sources: README.md:1-10, Sources/SwiftDitherKit/SwiftDitherKit.docc/SwiftDitherKit.md:1-15
The architecture separates data definitions, geometric calculations, and asynchronous rasterization. It emphasizes native integration with SwiftUI, supporting features like dynamic selection, accessibility descriptors, and motion effects that respect system settings.
Sources: Sources/SwiftDitherKit/SwiftDitherKit.docc/InteractionAndAccessibility.md:36-45, Sources/SwiftDitherKit/DitherCartesianCharts.swift:346-355
Core Chart Components
The library categorizes charts into two primary families: Cartesian and Polar. These components share a common design pattern where high-level SwiftUI views wrap specialized renderers and interaction layers.
Supported Chart Families
| Category | Chart Types | Description |
|---|---|---|
| Cartesian | Area, Line, Bar, Sparkline | Uses X and Y axes for data representation. Supports stacking and overlaid modes. |
| Polar | Pie, Donut, Radar | Uses radial and angular coordinates. Supports inner radius ratios for donut charts. |
Sources: Sources/SwiftDitherKit/SwiftDitherKit.docc/SwiftDitherKit.md:17-30, Sources/SwiftDitherKit/DitherCartesianCharts.swift:1-180
Data Structures
The system relies on three primary data structures to define chart content:
DitherDatum: Represents a single category (e.g., a month on the x-axis) and contains a dictionary of values mapped to series IDs.DitherSeries: Defines the visual configuration (color and texture variant) for a specific data series across multiple data points.DitherSlice: Used for polar charts (Pie/Donut) to represent individual segments.
Sources: Sources/SwiftDitherKit/DitherTypes.swift:80-136
Architecture and Data Flow
SwiftDitherKit utilizes an asynchronous rendering pipeline to ensure the main thread remains responsive. When data or view size changes, the library computes the necessary geometry and requests a new rasterized image.
This diagram illustrates the flow from data input in a SwiftUI view to the final display of a dithered raster image.
Sources: Sources/SwiftDitherKit/SwiftDitherKit.docc/SwiftDitherKit.md:11-15, Sources/SwiftDitherKit/DitherCartesianCharts.swift:346-368
Interaction and Selection Logic
Charts support interaction through tap gestures on marks (polygons, bars, or wedges) or via the legend. Selection state is managed through SwiftUI Binding<String?>.
The sequence diagram shows how user interactions trigger state changes that lead to visual updates.
Sources: Sources/SwiftDitherKit/SwiftDitherKit.docc/InteractionAndAccessibility.md:1-20, Sources/SwiftDitherKit/DitherCartesianCharts.swift:274-315
Visual Customization
The library offers extensive options for controlling the appearance and behavior of charts through the DitherCartesianOptions and DitherPolarOptions structures.
Formatting and Bloom
DitherValueFormat: Provides locale-aware formatting for axes, tooltips, and VoiceOver. Options include numeric, compact, percent, and currency formats.DitherBloom: Adds a glow effect behind the rasterized chart. Levels includeoff,low,high, andaura.DitherVariant: Controls the fill pattern of the dithered area, such asgradient,dotted,hatched, orsolid.
Sources: Sources/SwiftDitherKit/DitherTypes.swift:51-66, Sources/SwiftDitherKit/DitherCartesianCharts.swift:5-30
Motion and Transitions
Charts feature built-in animations for entrance and data changes.
- Crossfades: Used when data changes but the chart type remains the same.
- Sparkles: Area, line, and sparkline charts display occasional idle "sparkles" for visual interest.
- Reduce Motion Support: When system "Reduce Motion" is enabled, movements are replaced by short opacity fades, and sparkles stop blinking.
Sources: Sources/SwiftDitherKit/DitherMotion.swift:1-30, Sources/SwiftDitherKit/DitherSparkles.swift:1-127
Accessibility
SwiftDitherKit integrates with Apple's chart accessibility descriptor APIs. This ensures that even though the charts are rendered as pixel rasters, assistive technologies like VoiceOver can interpret the data points, series names, and selection status.
Configuration
Developers use DitherAccessibility to provide semantic context:
title: Concise name for the chart.summary: Description of the chart's purpose.categoryAxisLabel: Label for categories (e.g., "Months").valueAxisLabel: Label for the numeric axis (e.g., "Revenue").
Sources: Sources/SwiftDitherKit/DitherAccessibility.swift:1-24, Sources/SwiftDitherKit/SwiftDitherKit.docc/InteractionAndAccessibility.md:22-34
Conclusion
SwiftDitherKit provides a specialized charting solution for SwiftUI developers looking for a unique "ordered-dither" aesthetic without sacrificing performance or accessibility. By combining background rasterization with standard SwiftUI interaction patterns, it offers a robust framework for both live application interfaces and static offscreen image generation.
Sources: Sources/SwiftDitherKit/SwiftDitherKit.docc/GettingStarted.md:46-55