charts-3d
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese3D Charts with Swift Charts
基于Swift Charts的3D图表
Create 3D data visualizations using and . Covers math-driven surfaces, data-driven surfaces, interactive camera pose control, surface styling, and camera projection modes.
Chart3DSurfacePlot使用和创建3D数据可视化。涵盖数学驱动曲面、数据驱动曲面、交互式相机姿态控制、曲面样式设置以及相机投影模式。
Chart3DSurfacePlotWhen This Skill Activates
技能适用场景
Use this skill when the user:
- Wants to create a 3D chart or 3D data visualization
- Asks about ,
Chart3D, or 3D surface plotsSurfacePlot - Needs to visualize a mathematical function as a 3D surface
- Wants interactive drag-to-rotate on a 3D chart
- Asks about 3D chart camera angles, pose, or projection
- Needs to style 3D surfaces with gradients or height-based coloring
- Wants to render multiple surfaces in a single 3D chart
- Asks about data-driven 3D plots from an array of points
当用户有以下需求时,可使用本技能:
- 想要创建3D图表或3D数据可视化
- 询问、
Chart3D或3D曲面图相关内容SurfacePlot - 需要将数学函数可视化为3D曲面
- 希望3D图表支持拖拽旋转交互
- 询问3D图表的相机角度、姿态或投影方式
- 需要通过渐变或基于高度的着色来设置3D曲面样式
- 想要在单个3D图表中渲染多个曲面
- 询问如何通过点数组创建数据驱动的3D图
Decision Tree
决策树
What 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 { }你需要3D图表的哪些功能?
|
+-- 可视化数学函数f(x, y) -> z
| +-- 使用SurfacePlot(x:y:z:function:)
|
+-- 将数据点可视化为曲面
| +-- 使用Chart3D(data) { point in SurfacePlot(...) }
|
+-- 支持拖拽旋转交互
| +-- 通过@State var pose: Chart3DPose绑定pose: .chart3DPose($pose)
|
+-- 固定视角(无交互)
| +-- 使用只读姿态:.chart3DPose(Chart3DPose.front)或自定义姿态
|
+-- 设置曲面颜色
| +-- 纯色 -> .foregroundStyle(Color.blue)
| +-- 渐变 -> .foregroundStyle(LinearGradient(...))
| +-- 基于高度 -> .foregroundStyle(.heightBased(gradient, yRange:))
| +-- 基于法线 -> .foregroundStyle(.normalBased)
|
+-- 相机投影
| +-- 透视(带深度) -> .chart3DCameraProjection(.perspective)
| +-- 正交(平面) -> .chart3DCameraProjection(.orthographic)
| +-- 系统默认 -> .chart3DCameraProjection(.automatic)
|
+-- 单图表多曲面
+-- 在单个Chart3D { }中放置多个SurfacePlot调用API Availability
API兼容性
| 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 | | |
| API | 最低版本要求 | 导入包 | 说明 |
|---|---|---|---|
| iOS 26 / macOS 26 | | 3D图表主容器 |
| iOS 26 / macOS 26 | | 3D曲面标记 |
| iOS 26 / macOS 26 | | 视角控制 |
| iOS 26 / macOS 26 | | 包含 |
| iOS 26 / macOS 26 | | 包含 |
Quick Start
快速入门
Math-Driven Surface
数学驱动曲面
Render a surface from a function :
f(x, y) -> zswift
import 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)
}
}
}通过函数渲染曲面:
f(x, y) -> zswift
import 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)
}
}
}Data-Driven Surface
数据驱动曲面
Render a surface from an array of data points:
swift
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)
)
}
}
}通过数据点数组渲染曲面:
swift
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)
)
}
}
}Interactive Rotation
交互式旋转
Allow the user to drag to rotate the chart:
swift
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)
}
}允许用户拖拽旋转图表:
swift
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)
}
}Surface Styling Patterns
曲面样式设置方案
Solid Color
纯色
swift
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in x * z })
.foregroundStyle(.blue)swift
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in x * z })
.foregroundStyle(.blue)Linear Gradient
线性渐变
swift
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in x * z })
.foregroundStyle(
LinearGradient(
colors: [.blue, .green, .yellow],
startPoint: .bottom,
endPoint: .top
)
)swift
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in x * z })
.foregroundStyle(
LinearGradient(
colors: [.blue, .green, .yellow],
startPoint: .bottom,
endPoint: .top
)
)Height-Based Surface Style
基于高度的曲面样式
Color the surface based on height values, mapping a gradient across the y-axis range:
swift
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
)
)根据高度值为曲面着色,将渐变映射到y轴范围:
swift
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
)
)Normal-Based Surface Style
基于法线的曲面样式
Color based on surface normals, giving a lighting-aware appearance:
swift
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in sin(x) * cos(z) })
.foregroundStyle(Chart3DSurfaceStyle.normalBased)根据曲面法线着色,呈现光影感知效果:
swift
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in sin(x) * cos(z) })
.foregroundStyle(Chart3DSurfaceStyle.normalBased)Surface Roughness
曲面粗糙度
Control how shiny or matte the surface appears. A value of 0 is perfectly smooth (reflective), and 1 is fully rough (matte):
swift
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in sin(x) * cos(z) })
.foregroundStyle(.blue)
.roughness(0.3)控制曲面的光泽度或哑光程度。值为0时完全光滑(反光),值为1时完全粗糙(哑光):
swift
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in sin(x) * cos(z) })
.foregroundStyle(.blue)
.roughness(0.3)Interactive Pose Control
交互式姿态控制
Preset Poses
预设姿态
Chart3DPoseswift
.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 viewChart3DPoseswift
.chart3DPose(.default) // 标准斜角视角
.chart3DPose(.front) // 正面视角
.chart3DPose(.back) // 背面视角
.chart3DPose(.top) // 俯视视角
.chart3DPose(.bottom) // 仰视视角
.chart3DPose(.right) // 右侧视角
.chart3DPose(.left) // 左侧视角Custom Pose
自定义姿态
Specify exact azimuth (horizontal rotation) and inclination (vertical tilt):
swift
.chart3DPose(
Chart3DPose(azimuth: .degrees(45), inclination: .degrees(30))
)指定精确的方位角(水平旋转)和倾斜角(垂直倾斜):
swift
.chart3DPose(
Chart3DPose(azimuth: .degrees(45), inclination: .degrees(30))
)Read-Only vs Interactive
只读模式 vs 交互模式
swift
// ✅ 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)swift
// ✅ 只读模式 — 用户无法旋转图表
.chart3DPose(Chart3DPose.front)
// ✅ 交互模式 — 用户可拖拽旋转,姿态自动更新
@State private var pose = Chart3DPose.default
// ...
.chart3DPose($pose)Anti-Patterns
反模式示例
swift
// ❌ 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)swift
// ❌ 传递字面量作为交互所需的绑定值
.chart3DPose(.default) // 此为只读模式,拖拽手势无效
// ✅ 使用@State绑定实现交互式旋转
@State private var pose = Chart3DPose.default
// ...
.chart3DPose($pose)Camera Projection
相机投影
Control how 3D depth is rendered:
swift
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 decides- Perspective: Gives natural depth perception. Objects farther from the camera appear smaller. Good for presentations and visual appeal.
- Orthographic: No size distortion with distance. Good for precise data reading and scientific visualization.
- Automatic: System selects based on context.
控制3D深度的渲染方式:
swift
Chart3D {
SurfacePlot(x: "X", y: "Y", z: "Z", function: { x, z in sin(x) * cos(z) })
.foregroundStyle(.blue)
}
.chart3DCameraProjection(.perspective) // 远处物体看起来更小
// .chart3DCameraProjection(.orthographic) // 无透视变形
// .chart3DCameraProjection(.automatic) // 系统自动选择- 透视投影:呈现自然的深度感知,距离相机越远的物体看起来越小,适合演示和视觉展示。
- 正交投影:物体大小不受距离影响,适合精确数据读取和科学可视化。
- 自动模式:系统根据上下文选择投影方式。
Multiple Surfaces
多曲面渲染
Render multiple surfaces in a single chart for comparison:
swift
import 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)
}
}在单个图表中渲染多个曲面以便对比:
swift
import 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)
}
}Complete Example
完整示例
A full-featured 3D chart with height-based coloring, interactive rotation, and perspective projection:
swift
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()
}
}一个具备基于高度着色、交互式旋转和透视投影的全功能3D图表:
swift
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()
}
}Top Mistakes
常见错误
| # | 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 |
| 序号 | 错误内容 | 修复方案 |
|---|---|---|
| 1 | 忘记 | 同时需要导入 |
| 2 | 使用 | 使用 |
| 3 | 设置的 | 将 |
| 4 | 未设置 | 粗糙度会修改现有曲面外观,需先设置前景样式 |
| 5 | 在演示场景中使用正交投影 | 优先选择 |
Review Checklist
检查清单
Imports and Setup
导入与设置
- Both and
import SwiftUIare presentimport Charts - Deployment target is iOS 26 / macOS 26 or later
- wraps all
Chart3DcontentSurfacePlot
- 已同时导入和
import SwiftUIimport Charts - 部署目标为iOS 26 / macOS 26或更高版本
- 所有内容均被
SurfacePlot包裹Chart3D
Surface Configuration
曲面配置
- Axis labels (,
x:,y:) are descriptive and meaningfulz: - applied to each
foregroundStylefor clear visual distinctionSurfacePlot - in
yRangematches the actual output range of the function.heightBased() - value makes sense for the use case (0 = reflective, 1 = matte)
roughness
- 轴标签(、
x:、y:)描述清晰且有意义z: - 每个都已应用
SurfacePlot以明确区分foregroundStyle - 中的
.heightBased()与函数实际输出范围匹配yRange - 粗糙度值符合使用场景(0=反光,1=哑光)
Interactivity
交互性
- Pose is a binding if drag-to-rotate is intended
@State - Pose is a constant (non-binding) if rotation should be locked
- Initial pose angle provides a good default view of the data
- 若需拖拽旋转,姿态为绑定类型
@State - 若需锁定旋转,姿态为常量(非绑定)
- 初始姿态角度能提供良好的数据默认视图
Camera
相机设置
- Camera projection set appropriately (for visual,
.perspectivefor precision).orthographic - If using , verified the system choice looks acceptable
.automatic
- 相机投影设置合适(用于视觉展示,
.perspective用于精确场景).orthographic - 若使用,已确认系统选择的效果符合预期
.automatic
Multiple Surfaces
多曲面
- Each surface has a distinct color or opacity to differentiate
- Axis labels are unique per surface or shared where appropriate
- 每个曲面有独特的颜色或透明度以区分
- 轴标签根据情况设置为每个曲面独有或共享