-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProfileTrendChartView.swift
More file actions
215 lines (189 loc) · 7.62 KB
/
ProfileTrendChartView.swift
File metadata and controls
215 lines (189 loc) · 7.62 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
//
// ProfileTrendChartView.swift
// DevLog
//
// Created by opfic on 3/6/26.
//
import Charts
import SwiftUI
struct ProfileTrendChartView: View {
let trendPoints: [ProfileWeeklyTrendPoint]
let selectedActivityTypes: Set<ProfileActivityType>
private let chartHeight: CGFloat = 190
private let createdColor = Color.orange
private let completedColor = Color.blue
var body: some View {
VStack(alignment: .leading, spacing: 12) {
HStack(alignment: .firstTextBaseline) {
Text(String(localized: "profile_weekly_trend"))
.font(.subheadline)
.bold()
Spacer()
HStack(spacing: 10) {
if selectedActivityTypes.contains(.created) {
legendItem(title: ProfileActivityType.created.title, color: createdColor)
}
if selectedActivityTypes.contains(.completed) {
legendItem(title: ProfileActivityType.completed.title, color: completedColor)
}
}
}
if hasVisibleActivity {
Chart {
if selectedActivityTypes.contains(.created) {
createdSeries
}
if selectedActivityTypes.contains(.completed) {
completedSeries
}
}
.chartLegend(.hidden)
.chartXScale(
domain: xDomain,
range: .plotDimension(startPadding: 4, endPadding: 14)
)
.chartXAxis {
AxisMarks(values: axisWeekIndices) { value in
AxisValueLabel {
if let weekIndex = value.as(Int.self) {
Text("\(weekIndex)")
.font(.caption2)
.fixedSize()
}
}
if let weekIndex = value.as(Int.self), weekIndex != xDomain.upperBound {
AxisGridLine(stroke: StrokeStyle(lineWidth: 0.5))
.foregroundStyle(.quaternary)
AxisTick(stroke: StrokeStyle(lineWidth: 0.5))
.foregroundStyle(.quaternary)
}
}
}
.chartYAxis {
AxisMarks(position: .leading, values: yAxisValues) { _ in
AxisGridLine(stroke: StrokeStyle(lineWidth: 0.5))
.foregroundStyle(.quaternary)
AxisTick(stroke: StrokeStyle(lineWidth: 0.5))
.foregroundStyle(.quaternary)
AxisValueLabel()
}
}
.chartYScale(domain: yDomain)
.chartPlotStyle { plotArea in
plotArea
.background(Color(.systemGray6).opacity(0.6))
.clipShape(RoundedRectangle(cornerRadius: 10))
}
.frame(height: chartHeight)
Text(String(localized: "profile_week_axis"))
.font(.caption2)
.foregroundStyle(.secondary)
} else {
VStack(spacing: 6) {
Image(systemName: "chart.line.uptrend.xyaxis")
.font(.title3)
.foregroundStyle(.secondary)
Text(String(localized: "profile_quarter_empty"))
.font(.caption)
.foregroundStyle(.secondary)
}
.frame(maxWidth: .infinity, minHeight: chartHeight)
.background(
RoundedRectangle(cornerRadius: 12)
.fill(Color(.systemGray6).opacity(0.6))
)
}
}
}
private var axisWeekIndices: [Int] {
let weekIndices = trendPoints.map(\.weekIndex)
guard weekIndices.count > 6 else { return weekIndices }
var labels = Set<Int>()
let lastIndex = weekIndices.count - 1
for (offset, weekIndex) in weekIndices.enumerated() {
if offset == 0 || offset == lastIndex || offset.isMultiple(of: 2) {
labels.insert(weekIndex)
}
}
return labels.sorted()
}
private var visibleCounts: [Int] {
trendPoints.flatMap { point in
ProfileActivityType.allCases.compactMap { activityType in
guard selectedActivityTypes.contains(activityType) else { return nil }
return point.count(for: activityType)
}
}
}
private var maxVisibleCount: Int {
max(visibleCounts.max() ?? 0, 1)
}
private var xDomain: ClosedRange<Int> {
let upperBound = trendPoints.map(\.weekIndex).max() ?? 1
return 1...upperBound
}
private var yAxisValues: [Int] {
if maxVisibleCount <= 4 {
return Array(0...maxVisibleCount)
}
let step = max(Int(ceil(Double(maxVisibleCount) / 4)), 1)
var values = Array(stride(from: 0, through: maxVisibleCount, by: step))
if values.last != maxVisibleCount {
values.append(maxVisibleCount)
}
return values
}
private var yDomain: ClosedRange<Double> {
let upperBound = Double(maxVisibleCount)
let upperPadding = max(0.8, upperBound * 0.15)
let lowerPadding = max(0.6, upperBound * 0.08)
return -lowerPadding...(upperBound + upperPadding)
}
private var hasVisibleActivity: Bool {
visibleCounts.contains { $0 > 0 }
}
@ChartContentBuilder
private var createdSeries: some ChartContent {
ForEach(trendPoints) { point in
LineMark(
x: .value(String(localized: "profile_week_axis"), point.weekIndex),
y: .value(ProfileActivityType.created.title, point.createdCount),
series: .value(String(localized: "profile_activity_axis"), ProfileActivityType.created.title)
)
.foregroundStyle(createdColor)
.lineStyle(StrokeStyle(lineWidth: 2.5))
PointMark(
x: .value(String(localized: "profile_week_axis"), point.weekIndex),
y: .value(ProfileActivityType.created.title, point.createdCount)
)
.foregroundStyle(createdColor)
}
}
@ChartContentBuilder
private var completedSeries: some ChartContent {
ForEach(trendPoints) { point in
LineMark(
x: .value(String(localized: "profile_week_axis"), point.weekIndex),
y: .value(ProfileActivityType.completed.title, point.completedCount),
series: .value(String(localized: "profile_activity_axis"), ProfileActivityType.completed.title)
)
.foregroundStyle(completedColor)
.lineStyle(StrokeStyle(lineWidth: 2.5))
PointMark(
x: .value(String(localized: "profile_week_axis"), point.weekIndex),
y: .value(ProfileActivityType.completed.title, point.completedCount)
)
.foregroundStyle(completedColor)
}
}
private func legendItem(title: String, color: Color) -> some View {
HStack(spacing: 4) {
Circle()
.fill(color)
.frame(width: 8, height: 8)
Text(title)
.font(.caption)
.foregroundStyle(.secondary)
}
}
}