refactor(Turnips): start using Alignment Guide for Chart Legend
MODIFY
ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartGrid.swift
ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartGrid.swift
@@ -11,6 +11,8 @@ import Backend
struct TurnipsChartGrid: Shape {
let predictions: TurnipPredictions
+ // TODO: put only .vertical after tests
+ let displays: Set<Display> = [.vertical, .horizontal]
func path(in rect: CGRect) -> Path {
var path = Path()
@@ -18,12 +20,42 @@ struct TurnipsChartGrid: Shape {
let (minY, maxY, ratioY, ratioX) = predictions.minMax?.roundedMinMaxAndRatios(rect: rect) ?? (0, 0, 0, 0)
let count = predictions.minMax?.count ?? 0
+ if displays.contains(.vertical) {
for offset in 0..<count {
let offset = CGFloat(offset)
path.move(to: CGPoint(x: offset * ratioX, y: rect.minY))
path.addLine(to: CGPoint(x: offset * ratioX, y: ratioY * (maxY - minY)))
}
+ }
+
+ if displays.contains(.horizontal) {
+ let min = Int(minY)
+ let max = Int(maxY) + TurnipsChart.steps
+ let lines = Array(stride(from: min, to: max, by: TurnipsChart.steps))
+ for line in lines {
+ let line = CGFloat(line)
+ path.move(to: CGPoint(x: rect.minX, y: ratioY * line))
+ path.addLine(to: CGPoint(x: rect.maxX, y: ratioY * line))
+ }
+ }
return path
}
}
+
+extension TurnipsChartGrid {
+ enum Display { case vertical, horizontal }
+}
+
+extension VerticalAlignment {
+ private enum VerticalLegendAlignment: AlignmentID {
+ static func defaultValue(in context: ViewDimensions) -> CGFloat {
+ context[VerticalAlignment.center]
+ }
+ }
+ static let verticalLegendAlignment = VerticalAlignment(VerticalLegendAlignment.self)
+}
+
+extension Alignment {
+ static let verticalLegendAlignment = Alignment(horizontal: .trailing, vertical: .verticalLegendAlignment)
+}

MODIFY
ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartVerticalLegend.swift
ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartVerticalLegend.swift
@@ -20,14 +20,18 @@ struct TurnipsChartVerticalLegend: View {
let frame = geometry.frame(in: .local)
let (minY, maxY, ratioY, _) = predictions.minMax?.roundedMinMaxAndRatios(rect: frame) ?? (0, 0, 0, 0)
- return ZStack {
- ForEach(Array(stride(from: Int(minY), to: Int(maxY), by: TurnipsChart.steps)), id: \.self) { value in
+ let min = Int(minY)
+ let max = Int(maxY) + TurnipsChart.steps
+ let values = Array(stride(from: min, to: max, by: TurnipsChart.steps))
+
+ return ZStack(alignment: .verticalLegendAlignment) {
+ ForEach(values, id: \.self) { value in
Text("\(value)")
.font(.footnote)
.fontWeight(.semibold)
.foregroundColor(.text)
- .position(x: frame.midX, y: (maxY - CGFloat(value)) * ratioY)
- }
- }
+ .alignmentGuide(.verticalLegendAlignment) { d in CGFloat(value) * ratioY }
+ }.background(Color.red)
+ }.background(Color.blue)
}
}

MODIFY
ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartView.swift
ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartView.swift
@@ -66,7 +66,7 @@ struct TurnipsChartView_Previews: PreviewProvider {
TurnipsChartView(
predictions: predictions,
animateCurves: .constant(true)
- ).padding()
+ )
}
static let predictions = TurnipPredictions(

refactor(Turnips): start using Alignment Guide for Chart Legend