Chart
Native chart composition, package imports, and editable Swift source.
Preview


Use it
Import SwiftCN to use CNChart directly. This complete example also runs in the native component gallery.
import SwiftUI
import SwiftCN
import Charts
struct ChartExample: View {
var body: some View {
VStack(alignment: .leading, spacing: 12) {
CNChart("h-[200]") {
ForEach(Array([12, 18, 14, 24].enumerated()), id: \.offset) { index, value in
BarMark(x: .value("Week", "Week \(index + 1)"), y: .value("Downloads", value), width: .ratio(0.55))
}
}.accessibilityLabel("Downloads by week")
}
}
}Native behavior
Requires Apple Swift Charts, included in the SDK. Supply native chart marks. Classes style the surface and control tint; mark, axis, and scale APIs remain native Swift Charts APIs.
Style and compose
Visual recipes use .tw classes. Append classes: to override a recipe locally, or change the recipe in TWGlobalRules.named for your subtree. Native layout and interaction modifiers remain SwiftUI APIs. For control colors, append cn-tint-[primary], another theme token, or an interpolated Color.
Motion recipes are editable too: control-motion uses a 150 ms smooth response, disclosure-motion uses a 250 ms spring, and feedback-motion uses a 200 ms smooth response. Set a recipe to animate-none in your global rules to turn it off. Presets respect Reduce Motion. Static components stay still; native scrolling, drag tracking, and platform presentation keep their own gestures.
Action groups and card, dialog, and questionnaire footers stack when their content does not fit. The native layout preserves the same child controls during this change.
View builders preserve native child content. Native Button roles, labels, keyboard shortcuts, submit actions, and presentation modifiers still work. A string recipe does not replace a native control API or a typed chart mark.
Own the source
Copy the complete file below into your app alongside import SwiftCN. The file contains related composable parts from the same family. Its dependencies use public package APIs, so copied and imported components can coexist.
To own the styling engine too, download the complete source bundle. It contains every component, the .tw engine, its recipes, and the MIT license. Keep those files in one module and remove import SwiftCN from the usage example.
import SwiftUI
import SwiftCN
import Charts
/// Swift Charts keeps its scales, axes, marks, accessibility and selection APIs native.
/// Pass any ChartContent; style the shared surface with classes and the native marks in the builder.
public struct CNChart<Content: ChartContent>: View {
private let classes: TWClasses
private let content: Content
public init(_ classes: TWClasses = "", @ChartContentBuilder content: () -> Content) {
self.classes = classes; self.content = content()
}
public var body: some View { Chart { content }.tw(cn("chart", classes)).cnControlUtilities() }
}