Skip to content

Commit 72dc623

Browse files
committed
Live Activity: anchor preset label inside the plot area (top-trailing)
Use .chartPlotStyle's overlay (alignment: .topTrailing) to render the preset label inside the plot rectangle rather than via an outer ZStack. The plot area excludes axis labels, so right-aligning inside it puts the label flush with the right edge of the chart without overlapping the y-axis numbers (175/150/125/100) that render along the trailing axis. Removed the surrounding ZStack and the trailing/.top vs leading/.top alignment juggling — not needed anymore.
1 parent 6f848c7 commit 72dc623

1 file changed

Lines changed: 25 additions & 25 deletions

File tree

Loop Widget Extension/Live Activity/ChartView.swift

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ struct ChartView: View {
9898
}
9999

100100
var body: some View {
101-
ZStack(alignment: Alignment(horizontal: .leading, vertical: .top)){
102-
Chart {
101+
Chart {
103102
if let preset = self.preset, (preset.minValue > 0 || preset.maxValue > 0), predicatedData.count > 0, preset.endDate > Date.now.addingTimeInterval(.hours(-6)) {
104103
let (presetMin, presetMax) = adjustedRange(min: preset.minValue, max: preset.maxValue)
105104
RectangleMark(
@@ -146,9 +145,24 @@ struct ChartView: View {
146145
"Low": Self.colorBelowRange,
147146
"Default": Color("glucose")
148147
])
149-
.chartPlotStyle { plotContent in
150-
plotContent.background(.cyan.opacity(0.15))
151-
}
148+
.chartPlotStyle { plotContent in
149+
plotContent
150+
.background(.cyan.opacity(0.15))
151+
.overlay(alignment: .topTrailing) {
152+
if let preset = self.preset, preset.endDate > Date.now {
153+
Group {
154+
if let symbolName = preset.iconSystemSymbolName {
155+
Text(Image(systemName: symbolName)) + Text(" ") + Text(preset.title)
156+
} else {
157+
Text(preset.title)
158+
}
159+
}
160+
.font(.footnote)
161+
.padding(.trailing, 4)
162+
.padding(.top, 2)
163+
}
164+
}
165+
}
152166
.chartLegend(.hidden)
153167
.chartYScale(domain: [yAxisMarks.first ?? 0, yAxisMarks.last ?? 0])
154168
.chartYAxis {
@@ -161,26 +175,12 @@ struct ChartView: View {
161175
.foregroundStyle(Color.primary)
162176
}
163177
}
164-
.chartXAxis {
165-
AxisMarks(position: .automatic, values: .stride(by: .hour)) { _ in
166-
AxisValueLabel(format: .dateTime.hour(.twoDigits(amPM: .narrow)), anchor: .top)
167-
.foregroundStyle(Color.primary)
168-
AxisGridLine(stroke: .init(lineWidth: 0.1, dash: [2, 3]))
169-
.foregroundStyle(Color.primary)
170-
}
171-
}
172-
173-
if let preset = self.preset, preset.endDate > Date.now {
174-
Group {
175-
if let symbolName = preset.iconSystemSymbolName {
176-
Text(Image(systemName: symbolName)) + Text(" ") + Text(preset.title)
177-
} else {
178-
Text(preset.title)
179-
}
180-
}
181-
.font(.footnote)
182-
.padding(.leading, 5)
183-
.padding(.top, 2)
178+
.chartXAxis {
179+
AxisMarks(position: .automatic, values: .stride(by: .hour)) { _ in
180+
AxisValueLabel(format: .dateTime.hour(.twoDigits(amPM: .narrow)), anchor: .top)
181+
.foregroundStyle(Color.primary)
182+
AxisGridLine(stroke: .init(lineWidth: 0.1, dash: [2, 3]))
183+
.foregroundStyle(Color.primary)
184184
}
185185
}
186186
}

0 commit comments

Comments
 (0)