@@ -10,13 +10,16 @@ import AAInfographics
1010
1111class AAMixedChartComposer {
1212
13+ /// 构建带有图案填充的混合柱状/箱线图的 AAOptions 配置
1314 static func barMixedColumnrangeWithPatternFillChart( ) -> AAOptions {
15+ /// 睡眠阶段枚举,便于类型安全访问
1416 enum SleepStage : Int , CaseIterable {
1517 case deep = 0
1618 case light
1719 case rem
1820 case awake
1921
22+ /// 阶段对应的中文名称
2023 var category : String {
2124 switch self {
2225 case . deep: return " 深睡 "
@@ -27,21 +30,24 @@ class AAMixedChartComposer {
2730 }
2831 }
2932
33+ /// 理想睡眠区间数据结构
3034 struct IdealSleep {
3135 let low : Int
3236 let high : Int
3337 let stage : SleepStage
3438 }
3539
40+ /// 实际睡眠数据结构
3641 struct ActualSleep {
3742 let value : Int
3843 let label : String
3944 let stage : SleepStage
4045 }
4146
47+ /// 配置常量,包含颜色、尺寸和数据
4248 struct Config {
43- static let darkerColors = [ " #603EAC " , " #7560B1 " , " #4390AD " , " #AF8D0E " ]
44- static let actualColors = [ " #8B5CF6 " , " #A78BFA " , " #60CDF5 " , " #FACC15 " ]
49+ static let darkerColors = [ " #603EAC " , " #7560B1 " , " #4390AD " , " #AF8D0E " ] // 理想区间颜色
50+ static let actualColors = [ " #8B5CF6 " , " #A78BFA " , " #60CDF5 " , " #FACC15 " ] // 实际数据颜色
4551 static let pointWidth : Float = 20
4652 static let capHeight : Float = 32
4753 static let capWidth : Float = 2
@@ -61,11 +67,14 @@ class AAMixedChartComposer {
6167 ]
6268 }
6369
64- // 数据处理函数
70+ // MARK: - 数据处理函数
71+
72+ /// 获取所有阶段的中文名称,用于 X 轴分类
6573 func createCategories( ) -> [ String ] {
6674 return Config . ideal. map { $0. stage. category }
6775 }
6876
77+ /// 构建箱线图(理想区间)数据
6978 func createBoxplotData( ) -> [ [ String : Any ] ] {
7079 return Config . ideal. enumerated ( ) . map { index, item in
7180 [
@@ -81,6 +90,7 @@ class AAMixedChartComposer {
8190 }
8291 }
8392
93+ /// 构建实际睡眠数据
8494 func createActualData( ) -> [ [ String : Any ] ] {
8595 return Config . actual. enumerated ( ) . map { index, item in
8696 [
@@ -91,6 +101,7 @@ class AAMixedChartComposer {
91101 }
92102 }
93103
104+ /// 构建每个阶段的 plotLine,用于显示阶段和实际时长标签
94105 func createPlotLines( ) -> [ AAPlotLinesElement ] {
95106 return Config . ideal. enumerated ( ) . map { index, idealItem in
96107 let category = idealItem. stage. category
@@ -109,7 +120,9 @@ class AAMixedChartComposer {
109120 }
110121 }
111122
112- // 图表配置
123+ // MARK: - 图表配置
124+
125+ // 配置图表类型、事件(用于注入 SVG pattern)
113126 let aaChart = AAChart ( )
114127 . type ( . columnrange)
115128 . inverted ( true )
@@ -146,13 +159,15 @@ class AAMixedChartComposer {
146159 }
147160""" ) )
148161
162+ // 配置 X 轴
149163 let aaXAxis = AAXAxis ( )
150164 . categories ( createCategories ( ) )
151165 . lineWidth ( 0 )
152166 . labels ( AALabels ( )
153167 . enabled ( false ) )
154168 . plotLines ( createPlotLines ( ) )
155169
170+ // 配置 Y 轴
156171 let aaYAxis = AAYAxis ( )
157172 . min ( 0 )
158173 . max ( 100 )
@@ -162,27 +177,30 @@ class AAMixedChartComposer {
162177 . labels ( AALabels ( )
163178 . enabled ( false ) )
164179
180+ // 配置图表选项
165181 let aaPlotOptions = AAPlotOptions ( )
166182 . bar ( AABar ( )
167- . grouping ( false )
168- . borderWidth ( 0 )
169- . pointWidth ( Config . pointWidth)
183+ . grouping ( false ) // 禁用分组,确保每个系列独立显示
184+ . borderWidth ( 0 ) // 柱状图无边框
185+ . pointWidth ( Config . pointWidth) // 设置柱宽
170186 . dataLabels ( AADataLabels ( )
171- . enabled ( false ) ) )
187+ . enabled ( false ) ) ) // 不显示柱状图数据标签
172188 . boxplot ( AABoxplot ( )
173- . grouping ( false )
174- . lineWidth ( 0 )
175- . medianWidth ( 0 )
176- . medianColor ( " transparent " )
177- . stemWidth ( 0 )
178- . pointWidth ( Config . pointWidth)
179- . whiskerLength ( Config . capHeight)
180- . whiskerWidth ( Config . capWidth)
181- . whiskerColor ( " transparent " ) )
189+ . grouping ( false ) // 禁用分组,确保每个系列独立显示
190+ . lineWidth ( 0 ) // 箱线图主线宽度为0
191+ . medianWidth ( 0 ) // 中位线宽度为0
192+ . medianColor ( " transparent " ) // 中位线颜色透明
193+ . stemWidth ( 0 ) // 茎线宽度为0
194+ . pointWidth ( Config . pointWidth) // 设置箱线宽度
195+ . whiskerLength ( Config . capHeight) // 须长度
196+ . whiskerWidth ( Config . capWidth) // 须宽度
197+ . whiskerColor ( " transparent " ) ) // 须颜色透明
182198
199+ // 配置 tooltip
183200 let aaTooltip = AATooltip ( )
184201 . enabled ( false )
185202
203+ // 配置数据系列
186204 let aaSeries = [
187205 AASeriesElement ( )
188206 . name ( " 实际睡眠 " )
@@ -198,6 +216,7 @@ class AAMixedChartComposer {
198216 . clip ( false )
199217 ]
200218
219+ // 汇总所有配置
201220 let aaOptions = AAOptions ( )
202221 . chart ( aaChart)
203222 . title ( AATitle ( ) . text ( " 睡眠阶段 vs 理想区间 " ) )
0 commit comments