@@ -13,10 +13,6 @@ struct CategoryListView: View {
13
13
@Environment ( \. modelContext) var modelContext
14
14
@Query ( IOCategory . allOrderByName) var cates : [ IOCategory ]
15
15
@State var selectCate : IOCategory ?
16
- @State var catesByCount : [ IOCategory ] = [ IOCategory] ( )
17
-
18
- @State var maxCount : Int = 0
19
- @State var averageCount : Int = 0
20
16
21
17
let linearGradient = LinearGradient ( gradient: Gradient ( colors: [ Color . pink. opacity ( 0.8 ) , Color . pink. opacity ( 0.01 ) ] ) ,
22
18
startPoint: . top,
@@ -32,79 +28,9 @@ struct CategoryListView: View {
32
28
}
33
29
}
34
30
}
35
- GroupBox ( " 分类排序 " ) {
36
- Chart ( catesByCount) { cate in
37
- BarMark (
38
- x: . value( " 名字 " , cate. name) ,
39
- y: . value( " 数量 " , cate. infos? . count ?? 0 )
40
- )
41
- . foregroundStyle ( linearGradient)
42
- // .interpolationMethod(.catmullRom) // 设置曲线啥的
43
- . annotation ( position: . overlay, alignment: . top, spacing: 3 ) {
44
- VStack {
45
- Text ( " \( cate. infos? . count ?? 0 ) " )
46
- . font ( . footnote)
47
- . foregroundColor ( light: . white, dark: . white)
48
- . padding ( EdgeInsets ( top: 0 , leading: 0 , bottom: 10 , trailing: 0 ) )
49
- Text ( cate. name)
50
- . font ( . footnote)
51
- . foregroundColor ( light: . white, dark: . white)
52
- . rotationEffect ( . degrees( 45 ) )
53
- // .fixedSize(horizontal: false, vertical: true)
54
-
55
- }
56
- }
57
- RuleMark ( y: . value( " 均线 " , averageCount) )
58
- . foregroundStyle ( Color . secondary)
59
- . lineStyle ( StrokeStyle ( lineWidth: 0.8 , dash: [ 10 ] ) )
60
- . annotation ( alignment: . topLeading) {
61
- Text ( " 均线: \( averageCount) " )
62
- . font ( . footnote) . bold ( )
63
- . padding ( . trailing, 32 )
64
- . foregroundStyle ( Color . secondary)
65
- }
66
- // SectorMark(angle: .value("数量", cate.infos?.count ?? 0), innerRadius: .ratio(0.5),
67
- // angularInset: 1.5)
68
- // .foregroundStyle(by: .value("名字", cate.name))
69
- // .annotation(position: .overlay, alignment: .trailing, spacing: 3) {
70
- // Text(cate.name)
71
- // .font(.footnote)
72
- // .foregroundColor(light: .white, dark: .white)
73
- // }
74
- }
75
- // .chartYScale(domain: 0...maxCount)
76
- . aspectRatio ( 1 , contentMode: . fit)
77
- // .chartLegend(.hidden)
78
- // .chartXAxis(.hidden)
79
- . chartScrollableAxes ( . horizontal)
80
- // .chartScrollPosition(initialY: 8)
81
- // .chartXVisibleDomain(length: 5) // 调整可见数据点数
82
- // .chartYAxis(.hidden)
83
-
84
- }
85
- . onAppear ( perform: {
86
- sortCates ( )
87
- } )
88
- . onChange ( of: cates) { oldValue, newValue in
89
- sortCates ( )
90
- }
31
+
91
32
}
92
33
93
- func sortCates( ) {
94
- catesByCount = cates. sorted ( by: { l, r in
95
- l. infos? . count ?? 0 > r. infos? . count ?? 0
96
- } )
97
-
98
- // 找出最大
99
- var cateCounts = [ Int] ( )
100
- for cate in cates {
101
- if let cateCount = cate. infos? . count {
102
- cateCounts. append ( cateCount)
103
- }
104
- }
105
- maxCount = cateCounts. max ( ) ?? 0
106
- let sum = cateCounts. reduce ( 0 , + )
107
- averageCount = Int ( Double ( sum) / Double( cateCounts. count) )
108
- }
34
+
109
35
}
110
36
0 commit comments