Files
ChartView/Sources/SwiftUICharts/Base/Grid/ChartGrid.swift
Andras Samu ac7680320f feat(core): refactoring chart dispalying
now it is possible to add background lines precisely as charts are displayed at correct size
also rewrote basics to conform with Shapes and Animatable protocol
2021-04-04 15:54:15 +02:00

54 lines
1.3 KiB
Swift

import SwiftUI
public struct ChartGrid<Content: View>: View, ChartBase {
public var chartData = ChartData()
let content: () -> Content
let numberOfHorizontalLines = 4
@EnvironmentObject var data: ChartData
@EnvironmentObject var style: ChartStyle
public init(@ViewBuilder content: @escaping () -> Content) {
self.content = content
}
public var body: some View {
HStack {
ZStack {
VStack {
ForEach(0..<numberOfHorizontalLines) { _ in
GridElement()
Spacer()
}
}
self.content()
}
}
}
}
struct GridElement: View {
var body: some View {
DashedLine()
.frame(maxHeight: 2, alignment: .center)
}
}
struct DashedLine: View {
func line(frame: CGRect) -> Path {
let baseLine: CGFloat = CGFloat(frame.height / 2)
var hLine = Path()
hLine.move(to: CGPoint(x:0, y: baseLine))
hLine.addLine(to: CGPoint(x: frame.width, y: baseLine))
return hLine
}
var body: some View {
GeometryReader { geometry in
line(frame: geometry.frame(in: .local))
.stroke(Color(white: 0.3), style: StrokeStyle(lineWidth: 1, lineCap: .round, dash: [5, 10]))
}
}
}