Loading...
Loading...
3D chart visualization with Swift Charts using Chart3D, SurfacePlot, interactive pose control, and surface styling. Use when creating 3D data visualizations.
npx skill4agent add rshankras/claude-code-apple-skills charts-3dChart3DSurfacePlotChart3DSurfacePlotWhat 3D chart feature do you need?
|
+-- Visualize a math function f(x, y) -> z
| +-- Use SurfacePlot(x:y:z:function:)
|
+-- Visualize data points as a surface
| +-- Use Chart3D(data) { point in SurfacePlot(...) }
|
+-- Interactive drag-to-rotate
| +-- Bind pose: .chart3DPose($pose) with @State var pose: Chart3DPose
|
+-- Fixed viewing angle (no interaction)
| +-- Read-only pose: .chart3DPose(Chart3DPose.front) or custom
|
+-- Style the surface color
| +-- Solid color -> .foregroundStyle(Color.blue)
| +-- Gradient -> .foregroundStyle(LinearGradient(...))
| +-- Height-based -> .foregroundStyle(.heightBased(gradient, yRange:))
| +-- Normal-based -> .foregroundStyle(.normalBased)
|
+-- Camera projection
| +-- Perspective (depth) -> .chart3DCameraProjection(.perspective)
| +-- Orthographic (flat) -> .chart3DCameraProjection(.orthographic)
| +-- System default -> .chart3DCameraProjection(.automatic)
|
+-- Multiple surfaces in one chart
+-- Place multiple SurfacePlot calls inside a single Chart3D { }| API | Minimum Version | Import | Notes |
|---|---|---|---|
| iOS 26 / macOS 26 | | Main 3D chart container |
| iOS 26 / macOS 26 | | 3D surface mark |
| iOS 26 / macOS 26 | | Viewing angle control |
| iOS 26 / macOS 26 | | |
| iOS 26 / macOS 26 | | |
f(x, y) -> zimport SwiftUI
import Charts
struct WaveSurfaceView: View {
var body: some View {
Chart3D {
SurfacePlot(
x: "X",
y: "Height",
z: "Z",
function: { x, z in
sin(x) * cos(z)
}
)
.foregroundStyle(.blue)
}
}
}import SwiftUI
import Charts
struct DataPoint: Identifiable {
let id = UUID()
let x: Double
let y: Double
let z: Double
}
struct DataSurfaceView: View {
let points: [DataPoint]
var body: some View {
Chart3D(points) { point in
SurfacePlot(
x: .value("X", point.x),
y: .value("Height", point.y),
z: .value("Z", point.z)
)
}
}
}import SwiftUI
import Charts
struct InteractiveChartView: View {
@State private var pose = Chart3DPose.default
var body: some View {
Chart3D {
SurfacePlot(
x: "X",
y: "Height",
z: "Z",
function: { x, z in
sin(x) * cos(z)
}
)
.foregroundStyle(.blue)
}
.chart3DPose($pose)
}
}SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in x * z })
.foregroundStyle(.blue)SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in x * z })
.foregroundStyle(
LinearGradient(
colors: [.blue, .green, .yellow],
startPoint: .bottom,
endPoint: .top
)
)SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in sin(x) * cos(z) })
.foregroundStyle(
Chart3DSurfaceStyle.heightBased(
Gradient(colors: [.blue, .cyan, .green, .yellow, .red]),
yRange: -1...1
)
)SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in sin(x) * cos(z) })
.foregroundStyle(Chart3DSurfaceStyle.normalBased)SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in sin(x) * cos(z) })
.foregroundStyle(.blue)
.roughness(0.3)Chart3DPose.chart3DPose(.default) // Standard 3/4 angle
.chart3DPose(.front) // Viewing from front
.chart3DPose(.back) // Viewing from back
.chart3DPose(.top) // Top-down view
.chart3DPose(.bottom) // Bottom-up view
.chart3DPose(.right) // Right side view
.chart3DPose(.left) // Left side view.chart3DPose(
Chart3DPose(azimuth: .degrees(45), inclination: .degrees(30))
)// ✅ Read-only — user cannot rotate the chart
.chart3DPose(Chart3DPose.front)
// ✅ Interactive — user can drag to rotate, pose updates automatically
@State private var pose = Chart3DPose.default
// ...
.chart3DPose($pose)// ❌ Passing a literal where a binding is needed for interactivity
.chart3DPose(.default) // This is read-only; drag gestures will not work
// ✅ Use a @State binding for interactive rotation
@State private var pose = Chart3DPose.default
// ...
.chart3DPose($pose)Chart3D {
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in sin(x) * cos(z) })
.foregroundStyle(.blue)
}
.chart3DCameraProjection(.perspective) // Objects farther away appear smaller
// .chart3DCameraProjection(.orthographic) // No perspective distortion
// .chart3DCameraProjection(.automatic) // System decidesimport SwiftUI
import Charts
struct ComparisonChartView: View {
@State private var pose = Chart3DPose.default
var body: some View {
Chart3D {
SurfacePlot(
x: "X",
y: "Wave A",
z: "Z",
function: { x, z in sin(x) * cos(z) }
)
.foregroundStyle(.blue.opacity(0.8))
SurfacePlot(
x: "X",
y: "Wave B",
z: "Z",
function: { x, z in cos(x) * sin(z) }
)
.foregroundStyle(.red.opacity(0.8))
}
.chart3DPose($pose)
.chart3DCameraProjection(.perspective)
}
}import SwiftUI
import Charts
struct TerrainView: View {
@State private var pose = Chart3DPose(
azimuth: .degrees(30),
inclination: .degrees(25)
)
var body: some View {
VStack {
Text("Terrain Visualization")
.font(.headline)
Chart3D {
SurfacePlot(
x: "Longitude",
y: "Elevation",
z: "Latitude",
function: { x, z in
let distance = sqrt(x * x + z * z)
return sin(distance) / max(distance, 0.1)
}
)
.foregroundStyle(
Chart3DSurfaceStyle.heightBased(
Gradient(colors: [
.blue, .cyan, .green, .yellow, .orange, .red
]),
yRange: -0.5...1.0
)
)
.roughness(0.4)
}
.chart3DPose($pose)
.chart3DCameraProjection(.perspective)
}
.padding()
}
}| # | Mistake | Fix |
|---|---|---|
| 1 | Forgetting to | Both |
| 2 | Using | Use a |
| 3 | Setting | Match the |
| 4 | Applying | Roughness modifies existing surface appearance; set a foreground style first |
| 5 | Using orthographic projection for presentation/demo contexts | Prefer |
import SwiftUIimport ChartsChart3DSurfacePlotx:y:z:foregroundStyleSurfacePlotyRange.heightBased()roughness@State.perspective.orthographic.automatic