-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathBarChartView.swift
More file actions
386 lines (349 loc) · 13.4 KB
/
Copy pathBarChartView.swift
File metadata and controls
386 lines (349 loc) · 13.4 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import SwiftUI
import Charts
struct BarChartView: View {
let data: ChartData
@Binding var selectedBarDate: Date?
@State private var selectedDataPoints: SelectedDataPoints?
@State private var isDragging = false
private var tappedDataPoint: DataPoint? {
guard let selectedBarDate else { return nil }
return data.currentData.first { $0.date == selectedBarDate }
}
init(data: ChartData, selectedBarDate: Binding<Date?> = .constant(nil)) {
self.data = data
self._selectedBarDate = selectedBarDate
}
@Environment(\.context) var context
@Environment(\.colorScheme) var colorScheme
@Environment(\.showComparison) private var showComparison
private var valueFormatter: StatsValueFormatter {
StatsValueFormatter(metric: data.metric)
}
private var currentAverage: Double {
guard !data.currentData.isEmpty else { return 0 }
return Double(data.currentTotal) / Double(data.currentData.count)
}
var body: some View {
Chart {
if showComparison {
previousPeriodBars
}
currentPeriodBars
averageLine
significantPointAnnotations
tappedBarAnnotation
selectionIndicatorMarks
}
.chartXAxis { xAxis }
.chartYAxis { yAxis }
.chartXScale(domain: xAxisDomain)
.chartYScale(domain: yAxisDomain)
.chartLegend(.hidden)
.environment(\.timeZone, context.timeZone)
.animation(.spring, value: ObjectIdentifier(data))
.animation(.snappy, value: selectedBarDate)
.chartOverlay { proxy in
makeGesturesOverlayView(proxy: proxy)
}
.dynamicTypeSize(...DynamicTypeSize.xxxLarge)
.accessibilityElement()
.accessibilityLabel(Strings.Accessibility.chartContainer)
.accessibilityHint(Strings.Accessibility.viewChartData)
}
// MARK: - Chart Marks
@ChartContentBuilder
private var currentPeriodBars: some ChartContent {
ForEach(data.currentData) { point in
let isIncomplete = context.calendar.isIncompleteDataPeriod(for: point.date, granularity: data.granularity)
BarMark(
x: .value("Date", point.date, unit: data.granularity.component, calendar: context.calendar),
y: .value("Value", point.value),
width: .automatic
)
.foregroundStyle(isIncomplete ? AnyShapeStyle(incompleteBarPattern) : AnyShapeStyle(barGradient))
.cornerRadius(5)
.opacity(getOpacityForPeriodBar(for: point))
}
}
private var barGradient: LinearGradient {
LinearGradient(
colors: [data.metric.primaryColor, lighten(data.metric.primaryColor)],
startPoint: .top,
endPoint: .bottom
)
}
/// A tiling diagonal-stripe pattern for today's incomplete bar.
private var incompleteBarPattern: ImagePaint {
let color = UIColor(data.metric.primaryColor)
let tileSize: CGFloat = 10
let renderer = UIGraphicsImageRenderer(size: CGSize(width: tileSize, height: tileSize))
let image = renderer.image { ctx in
color.withAlphaComponent(0.33).setFill()
ctx.fill(CGRect(x: 0, y: 0, width: tileSize, height: tileSize))
let cg = ctx.cgContext
cg.setStrokeColor(color.withAlphaComponent(0.5).cgColor)
cg.setLineWidth(1.5)
// Three parallel lines for seamless tiling
cg.move(to: CGPoint(x: -tileSize, y: tileSize))
cg.addLine(to: CGPoint(x: tileSize, y: -tileSize))
cg.move(to: CGPoint(x: 0, y: tileSize))
cg.addLine(to: CGPoint(x: tileSize, y: 0))
cg.move(to: CGPoint(x: 0, y: tileSize * 2))
cg.addLine(to: CGPoint(x: tileSize * 2, y: 0))
cg.strokePath()
}
return ImagePaint(image: Image(uiImage: image), scale: 1)
}
private func lighten(_ color: Color) -> Color {
if #available(iOS 18, *) {
color.mix(with: Color(.systemBackground), by: colorScheme == .light ? 0.2 : 0.1)
} else {
color.opacity(0.5)
}
}
private func getOpacityForPeriodBar(for point: DataPoint) -> CGFloat {
if let tappedDataPoint, tappedDataPoint.id == point.id {
return 1.0
}
guard let selectedDataPoints else {
if tappedDataPoint != nil {
return 0.15
}
return 1
}
return (selectedDataPoints.current?.id == point.id || selectedDataPoints.previous?.id == point.id) ? 1.0 : 0.25
}
@ChartContentBuilder
private var previousPeriodBars: some ChartContent {
ForEach(data.mappedPreviousData) { point in
BarMark(
x: .value("Date", point.date, unit: data.granularity.component, calendar: context.calendar),
y: .value("Value", point.value),
width: .automatic,
stacking: .unstacked
)
.foregroundStyle(Color.secondary.opacity(0.25))
.cornerRadius(5)
.opacity(getOpacityForPeriodBar(for: point))
}
}
@ChartContentBuilder
private var averageLine: some ChartContent {
if currentAverage > 0 {
RuleMark(y: .value("Average", currentAverage))
.foregroundStyle(Color.secondary.opacity(0.33))
.lineStyle(StrokeStyle(lineWidth: 1, dash: [6, 6]))
.annotation(position: .trailing, alignment: .trailing) {
ChartAverageAnnotation(value: Int(currentAverage), formatter: valueFormatter)
}
}
}
@ChartContentBuilder
private var significantPointAnnotations: some ChartContent {
if tappedDataPoint == nil, let maxPoint = data.significantPoints.currentMax, !data.currentData.isEmpty {
PointMark(
x: .value("Date", maxPoint.date, unit: data.granularity.component, calendar: context.calendar),
y: .value("Value", maxPoint.value)
)
.opacity(0)
.annotation(position: .top, spacing: 8) {
SignificantPointAnnotation(
value: maxPoint.value,
metric: data.metric
)
// Important for drag selection to work correctly.
.opacity(selectedDataPoints == nil ? 1 : 0)
}
}
}
@ChartContentBuilder
private var tappedBarAnnotation: some ChartContent {
if let tappedDataPoint, selectedDataPoints == nil {
PointMark(
x: .value("Date", tappedDataPoint.date, unit: data.granularity.component, calendar: context.calendar),
y: .value("Value", tappedDataPoint.value)
)
.opacity(0)
.annotation(position: .top, spacing: 8) {
SignificantPointAnnotation(
value: tappedDataPoint.value,
metric: data.metric
)
}
}
}
@ChartContentBuilder
private var selectionIndicatorMarks: some ChartContent {
if let selectedDataPoints {
if let currentPoint = selectedDataPoints.current {
RuleMark(x: .value("Selected", currentPoint.date))
.foregroundStyle(Color.clear)
.lineStyle(StrokeStyle(lineWidth: 1))
.offset(yStart: 48)
.zIndex(3)
.annotation(
position: .top,
spacing: 0,
overflowResolution: .init(
x: .fit(to: .chart),
y: .disabled
)
) {
tooltipView
}
} else if let previousPoint = selectedDataPoints.previous {
RuleMark(x: .value("Selected", previousPoint.date))
.foregroundStyle(Color.clear)
.lineStyle(StrokeStyle(lineWidth: 1))
.offset(yStart: 48)
.zIndex(3)
.annotation(
position: .top,
spacing: 0,
overflowResolution: .init(
x: .fit(to: .chart),
y: .disabled
)
) {
tooltipView
}
}
}
}
// MARK: - Axis Configuration
private var xAxis: some AxisContent {
ChartHelper.makeXAxis(
domain: xAxisDomain,
granularity: data.granularity,
calendar: context.calendar
)
}
private var yAxis: some AxisContent {
AxisMarks(values: .automatic) { value in
if let value = value.as(Int.self) {
AxisGridLine()
.foregroundStyle(Color.secondary.opacity(0.33))
AxisValueLabel {
if value > 0 {
Text(valueFormatter.format(value: value, context: .compact))
.font(.caption2.weight(.medium))
.foregroundColor(.secondary)
}
}
}
}
}
private var xAxisDomain: ClosedRange<Date> {
ChartHelper.xAxisDomain(for: data, calendar: context.calendar)
}
private var yAxisDomain: ClosedRange<Int> {
// If all values are zero, show a reasonable range
if data.maxValue == 0 {
return 0...100
}
guard data.maxValue > 0 else {
return data.maxValue...0 // Just in case; should never happen
}
// Add some padding above the max value
let padding = max(Int(Double(data.maxValue) * 0.33), 1)
return 0...(data.maxValue + padding)
}
// MARK: - Helper Views
@ViewBuilder
private var tooltipView: some View {
if let selectedPoints = selectedDataPoints {
ChartValueTooltipView(
selectedPoints: selectedPoints,
metric: data.metric,
granularity: data.granularity
)
}
}
// MARK: - Gestures
private func makeGesturesOverlayView(proxy: ChartProxy) -> some View {
GeometryReader { geometry in
ChartGestureOverlay(
onTap: { location in
handleTapGesture(at: location, proxy: proxy, geometry: geometry)
},
onInteractionUpdate: { location in
isDragging = true
selectedDataPoints = getSelectedDataPoints(at: location, proxy: proxy, geometry: geometry)
},
onInteractionEnd: {
isDragging = false
selectedDataPoints = nil
}
)
}
}
private func handleTapGesture(at location: CGPoint, proxy: ChartProxy, geometry: GeometryProxy) {
// Only handle tap if not dragging or long pressing
guard !isDragging else { return }
guard let selection = getSelectedDataPoints(at: location, proxy: proxy, geometry: geometry),
let point = selection.current ?? selection.previous else {
selectedBarDate = nil
return
}
selectedBarDate = (selectedBarDate == point.date) ? nil : point.date
}
private func getSelectedDataPoints(at location: CGPoint, proxy: ChartProxy, geometry: GeometryProxy) -> SelectedDataPoints? {
guard let frame = proxy.plotFrame else {
return nil
}
let plotFrame = geometry[frame]
let adjustedX = max(0, min(location.x - plotFrame.origin.x, plotFrame.width))
guard let date: Date = proxy.value(atX: adjustedX) else {
return nil
}
// `proxy.value(atX:)` returns a precise date at the tap location.
// Resolve it to the start of the containing calendar period so it
// matches data point dates (which are period starts).
guard let periodStart = context.calendar.dateInterval(of: data.granularity.component, for: date)?.start else {
return nil
}
return SelectedDataPoints.compute(for: periodStart, data: data)
}
}
// MARK: - Preview
#if DEBUG
#Preview {
ScrollView {
LazyVGrid(
columns: [
GridItem(.adaptive(minimum: 350, maximum: 450), spacing: 16)
],
spacing: 16
) {
ForEach(ChartData.previewExamples) { example in
previewCard(example.title) {
BarChartView(data: example.data)
.environment(\.showComparison, example.showComparison)
}
}
}
.padding()
}
.background(Constants.Colors.background)
}
private func previewCard<Content: View>(
_ title: String,
@ViewBuilder content: () -> Content
) -> some View {
VStack(alignment: .leading, spacing: 8) {
Text(title)
.font(.caption.weight(.medium))
.foregroundStyle(.secondary)
content()
.padding(.horizontal)
.frame(height: 220)
}
.padding()
.background(Color(.systemBackground))
.clipShape(RoundedRectangle(cornerRadius: 6))
.overlay(
RoundedRectangle(cornerRadius: 6)
.stroke(Color(.separator), lineWidth: 0.5)
)
}
#endif