forked from calleric/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarChartSection.swift
More file actions
235 lines (212 loc) · 7.49 KB
/
BarChartSection.swift
File metadata and controls
235 lines (212 loc) · 7.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
///
/// BarChartSection.swift
/// Display the top section of a Bar Chart using yStart, yEnd and chartYScale in SwiftUI Charts
///
/// Xcode version 14.0.1
/// Swift version 5
///
/// Created by Eric Callanan on 23/October/2022.
///
///
/// see more
/// https://swdevnotes.com/swift/2022/display-top-section-of-bar-charts-with-swift-charts/
///
import SwiftUI
import Charts
struct BarChartSection: View {
var mean: Double {
return Double(chartData.reduce(0) { $0 + $1.value }) / Double(chartData.count)
}
var body: some View {
ScrollView {
VStack {
BarTopSectionView()
.frame(height: 330)
RectangleMarkTopView()
.frame(height: 330)
LineChartTopView()
.frame(height: 330)
Spacer()
}
}
}
}
struct BarChartSection_Previews: PreviewProvider {
static var previews: some View {
BarChartSection()
}
}
struct BarTopSectionView: View {
var mean: Double {
return Double(chartData.reduce(0) { $0 + $1.value }) / Double(chartData.count)
}
let minValue = 40
let maxValue = 48
var body: some View {
GroupBox("Results - top bars - yStart & yEnd") {
Chart(chartData) { values in
BarMark(
x: .value("name", values.name),
yStart: .value("value", minValue),
yEnd: .value("value", values.value)
)
.foregroundStyle(Colors.barColor)
.cornerRadius(5.0)
.annotation(position: .overlay,
alignment: .top) {
Text("\(values.value)")
.font(.footnote)
.foregroundColor(.white)
.fontWeight(.bold)
.offset(y:30)
}
RuleMark(y: .value("mean", mean))
.foregroundStyle(Colors.lineColor)
.annotation(position: .overlay,
alignment: .bottomTrailing,
spacing: 5) {
VStack {
Text("mean")
Text("\(String(format: "%.1f", mean))")
}
.foregroundColor(Colors.lineColor)
}
}
.chartYAxis {
AxisMarks(position: .leading)
}
.padding()
.chartYScale(domain: minValue...maxValue)
}
.groupBoxStyle(YellowGroupBoxStyle())
}
}
struct RectangleMarkTopView: View {
var mean: Double {
return Double(chartData.reduce(0) { $0 + $1.value }) / Double(chartData.count)
}
let minValue = 40
let maxValue = 48
var body: some View {
VStack {
GroupBox("Results - RectangleMark") {
Chart(chartData) { values in
RectangleMark(
x: .value("name", values.name),
yStart: .value("value", minValue),
yEnd: .value("value", values.value)
)
.foregroundStyle(Colors.barColor)
.cornerRadius(5.0)
.annotation(position: .overlay,
alignment: .top) {
Text("\(values.value)")
.font(.footnote)
.foregroundColor(.white)
.fontWeight(.bold)
.offset(y:30)
}
RuleMark(y: .value("mean", mean))
.foregroundStyle(Colors.lineColor)
.annotation(position: .overlay,
alignment: .bottomTrailing,
spacing: 5) {
VStack {
Text("mean")
Text("\(String(format: "%.1f", mean))")
}
.foregroundColor(Colors.lineColor)
}
}
.chartYAxis {
AxisMarks(position: .leading)
}
.padding()
.chartYScale(domain: minValue...maxValue)
}
.groupBoxStyle(YellowGroupBoxStyle())
}
}
}
struct LineChartTopView: View {
var mean: Double {
return Double(chartData.reduce(0) { $0 + $1.value }) / Double(chartData.count)
}
let minValue = 40
let maxValue = 48
var body: some View {
GroupBox("Results - LineMark") {
Chart(chartData) { values in
LineMark(
x: .value("name", values.name),
y: .value("value", values.value)
)
.foregroundStyle(Colors.barColor)
.symbol(by: .value("data", "value"))
.symbolSize(150)
.annotation(position: .overlay,
alignment: .top) {
Text("\(values.value)")
.font(.footnote)
.foregroundColor(.white)
.fontWeight(.bold)
.offset(y:30)
}
RuleMark(y: .value("mean", mean))
.foregroundStyle(Colors.lineColor)
.annotation(position: .overlay,
alignment: .bottomTrailing,
spacing: 5) {
VStack {
Text("mean")
Text("\(String(format: "%.1f", mean))")
}
.foregroundColor(Colors.lineColor)
}
}
.chartYAxis {
AxisMarks(position: .leading)
}
.chartLegend(.hidden)
.padding()
.chartYScale(domain: minValue...maxValue)
}
.groupBoxStyle(YellowGroupBoxStyle())
}
}
struct Data: Identifiable {
var name: String
var value: Int
var id = UUID()
}
let chartData: [Data] = [.init(name: "Square", value: 45),
.init(name: "Circle", value: 42),
.init(name: "Triangle", value: 46),
.init(name: "Hexagon", value: 43)]
struct Colors {
static let barColor = Color(hue: 0.8, saturation: 0.7, brightness: 0.5)
static let lineColor = Color(hue: 0.4, saturation: 0.5, brightness: 0.5)
static let bgPlotColor = Color(hue: 0.12, saturation: 0.10, brightness: 0.92)
static let bgGradient = LinearGradient(
gradient: Gradient(colors: [
Color(hue: 0.10, saturation: 0.10, brightness: 1.0),
Color(hue: 0.10, saturation: 0.20, brightness: 0.95)
]),
startPoint: .topLeading,
endPoint: .bottomTrailing)
}
struct YellowGroupBoxStyle: GroupBoxStyle {
func makeBody(configuration: Configuration) -> some View {
configuration.content
.padding(.top, 20)
.padding(10)
.background(Colors.bgGradient)
.cornerRadius(15)
.shadow(radius: 6.0, x: 6.0, y: 8.0)
.overlay(
configuration.label.padding(12),
alignment: .topLeading
)
.padding()
}
}