我有一个复杂的图表,可以以不同的风格呈现各种图形,并且正在将这些变体抽象为单独的视图组件,其中一个例子是
import SwiftUI
import Charts
struct ExerciseChartPlot: View {
// Public Variables
let isLineChart: Bool
let xLabel: String
let yLabel: String
let calendarUnit: Calendar.Component
let date: Date
let value: Double
// Body
var body: some View {
if isLineChart {
LineMark(x: .value(xLabel, date, unit: calendarUnit), y: .value(yLabel, value))
.foregroundStyle(Color._green)
.symbol(.circle)
AreaMark(x: .value(xLabel, date, unit: calendarUnit), y: .value(yLabel, value))
.foregroundStyle(LinearGradient(colors: [._green.opacity(0.2), .clear], startPoint: .top, endPoint: .bottom))
} else {
BarMark(x: .value(xLabel, date, unit: calendarUnit), y: .value(yLabel, value))
.foregroundStyle(Color._green.gradient)
}
}
}
但它导致了“静态方法'buildExpression'要求'some ChartContent'符合'View'”的错误
我想这是因为我在没有包装的情况下退回了组件
Chart
.我想保留
图表
ui树中“更高层”的组件,只有这些较小的组件返回需要在其中呈现的图表元素。
有可能以某种方式实现这一点吗?