@@ -11,75 +11,90 @@ import AAInfographics
1111class AAMixedChartComposer {
1212
1313 static func barMixedColumnrangeWithPatternFillChart( ) -> AAOptions {
14- // 配置常量 - 使用二级枚举
15- let Colors = [
16- " darker " : [ " #603EAC " , " #7560B1 " , " #4390AD " , " #AF8D0E " ] ,
17- " actual " : [ " #8B5CF6 " , " #A78BFA " , " #60CDF5 " , " #FACC15 " ]
18- ]
14+ enum SleepStage : Int , CaseIterable {
15+ case deep = 0
16+ case light
17+ case rem
18+ case awake
19+
20+ var category : String {
21+ switch self {
22+ case . deep: return " 深睡 "
23+ case . light: return " 浅睡 "
24+ case . rem: return " REM "
25+ case . awake: return " 清醒 "
26+ }
27+ }
28+ }
1929
20- let Dimensions = [
21- " pointWidth " : 20 as Float ,
22- " capHeight " : 32 as Float ,
23- " capWidth " : 2 as Float
24- ]
25-
26- // 原始数据
27- let sleepData = [
28- " ideal " : [
29- [ " low " : 20 , " high " : 32 , " category " : " 深睡 " ] ,
30- [ " low " : 40 , " high " : 60 , " category " : " 浅睡 " ] ,
31- [ " low " : 10 , " high " : 25 , " category " : " REM " ] ,
32- [ " low " : 0 , " high " : 5 , " category " : " 清醒 " ]
33- ] ,
34- " actual " : [
35- [ " value " : 27 , " label " : " 1小时22分钟(27%) " ] ,
36- [ " value " : 53 , " label " : " 3小时42分钟(53%) " ] ,
37- [ " value " : 18 , " label " : " 1小时49分钟(18%) " ] ,
38- [ " value " : 2 , " label " : " 5分钟(2%) " ]
30+ struct IdealSleep {
31+ let low : Int
32+ let high : Int
33+ let stage : SleepStage
34+ }
35+
36+ struct ActualSleep {
37+ let value : Int
38+ let label : String
39+ let stage : SleepStage
40+ }
41+
42+ struct Config {
43+ static let darkerColors = [ " #603EAC " , " #7560B1 " , " #4390AD " , " #AF8D0E " ]
44+ static let actualColors = [ " #8B5CF6 " , " #A78BFA " , " #60CDF5 " , " #FACC15 " ]
45+ static let pointWidth : Float = 20
46+ static let capHeight : Float = 32
47+ static let capWidth : Float = 2
48+
49+ static let ideal : [ IdealSleep ] = [
50+ IdealSleep ( low: 20 , high: 32 , stage: . deep) ,
51+ IdealSleep ( low: 40 , high: 60 , stage: . light) ,
52+ IdealSleep ( low: 10 , high: 25 , stage: . rem) ,
53+ IdealSleep ( low: 0 , high: 5 , stage: . awake)
3954 ]
40- ]
55+
56+ static let actual : [ ActualSleep ] = [
57+ ActualSleep ( value: 27 , label: " 1小时22分钟(27%) " , stage: . deep) ,
58+ ActualSleep ( value: 53 , label: " 3小时42分钟(53%) " , stage: . light) ,
59+ ActualSleep ( value: 18 , label: " 1小时49分钟(18%) " , stage: . rem) ,
60+ ActualSleep ( value: 2 , label: " 5分钟(2%) " , stage: . awake)
61+ ]
62+ }
4163
4264 // 数据处理函数
4365 func createCategories( ) -> [ String ] {
44- return sleepData [ " ideal " ] !. map { item in
45- return item [ " category " ] as! String
46- }
66+ return Config . ideal. map { $0. stage. category }
4767 }
4868
4969 func createBoxplotData( ) -> [ [ String : Any ] ] {
50- return sleepData [ " ideal " ] !. enumerated ( ) . map { index, item in
51- let low = item [ " low " ] as! Int
52- let high = item [ " high " ] as! Int
53- return [
70+ return Config . ideal. enumerated ( ) . map { index, item in
71+ [
5472 " x " : index,
55- " low " : low,
56- " q1 " : low,
57- " median " : low,
58- " q3 " : high,
59- " high " : high,
73+ " low " : item . low,
74+ " q1 " : item . low,
75+ " median " : item . low,
76+ " q3 " : item . high,
77+ " high " : item . high,
6078 " fillColor " : " url(#customPattern \( index) ) " ,
61- " whiskerColor " : Colors [ " darker " ] ! [ index]
79+ " whiskerColor " : Config . darkerColors [ index]
6280 ]
6381 }
6482 }
6583
6684 func createActualData( ) -> [ [ String : Any ] ] {
67- return sleepData [ " actual " ] !. enumerated ( ) . map { index, item in
68- let value = item [ " value " ] as! Int
69- let label = item [ " label " ] as! String
70- return [
71- " y " : value,
72- " color " : Colors [ " actual " ] ![ index] ,
73- " label " : label
85+ return Config . actual. enumerated ( ) . map { index, item in
86+ [
87+ " y " : item. value,
88+ " color " : Config . actualColors [ index] ,
89+ " label " : item. label
7490 ]
7591 }
7692 }
7793
78- // 创建plotLines函数
7994 func createPlotLines( ) -> [ AAPlotLinesElement ] {
80- return sleepData [ " ideal " ] ! . enumerated ( ) . map { index, item in
81- let category = item [ " category " ] as! String
82- let label = sleepData [ " actual " ] ! [ index] [ " label " ] as! String
95+ return Config . ideal. enumerated ( ) . map { index, idealItem in
96+ let category = idealItem . stage . category
97+ let label = Config . actual [ index] . label
8398 return AAPlotLinesElement ( )
8499 . value ( Double ( index) )
85100 . width ( 0 )
@@ -151,7 +166,7 @@ class AAMixedChartComposer {
151166 . bar ( AABar ( )
152167 . grouping ( false )
153168 . borderWidth ( 0 )
154- . pointWidth ( Dimensions [ " pointWidth " ] as! Float )
169+ . pointWidth ( Config . pointWidth)
155170 . dataLabels ( AADataLabels ( )
156171 . enabled ( false ) ) )
157172 . boxplot ( AABoxplot ( )
@@ -160,9 +175,9 @@ class AAMixedChartComposer {
160175 . medianWidth ( 0 )
161176 . medianColor ( " transparent " )
162177 . stemWidth ( 0 )
163- . pointWidth ( Dimensions [ " pointWidth " ] as! Float )
164- . whiskerLength ( Dimensions [ " capHeight " ] as! Float )
165- . whiskerWidth ( Dimensions [ " capWidth " ] as! Float )
178+ . pointWidth ( Config . pointWidth)
179+ . whiskerLength ( Config . capHeight)
180+ . whiskerWidth ( Config . capWidth)
166181 . whiskerColor ( " transparent " ) )
167182
168183 let aaTooltip = AATooltip ( )
0 commit comments