Skip to content

Commit

Permalink
refactor(Turnips): start using Alignment Guide for Chart Legend
Browse files Browse the repository at this point in the history
  • Loading branch information
renaudjenny committed May 9, 2020
1 parent 1b936dd commit 2afa994
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,51 @@ 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()

let (minY, maxY, ratioY, ratioX) = predictions.minMax?.roundedMinMaxAndRatios(rect: rect) ?? (0, 0, 0, 0)
let count = predictions.minMax?.count ?? 0

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(.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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct TurnipsChartView_Previews: PreviewProvider {
TurnipsChartView(
predictions: predictions,
animateCurves: .constant(true)
).padding()
)
}

static let predictions = TurnipPredictions(
Expand Down

0 comments on commit 2afa994

Please sign in to comment.