-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineGraph.swift
203 lines (151 loc) · 6.53 KB
/
LineGraph.swift
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
//
// LineGraph.swift
// CustomScrollViewBottomShee (iOS)
//
// Created by Ridhima on 6/18/22.
//
import SwiftUI
// Custom View....
struct LineGraph: View {
// Number of plots...
var data: [CGFloat]
@State var currentPlot = ""
// Offset...
@State var offset: CGSize = .zero
@State var showPlot = false
@State var translation: CGFloat = 0
@GestureState var isDrag: Bool = false
var body: some View {
GeometryReader{proxy in
let height = proxy.size.height
let width = (proxy.size.width) / CGFloat(data.count - 1)
let maxPoint = (data.max() ?? 0) + 100
let points = data.enumerated().compactMap { item -> CGPoint in
// getting progress and multiplyinh with height...
let progress = item.element / maxPoint
let pathHeight = progress * height
// width..
let pathWidth = width * CGFloat(item.offset)
// Since we need peak to top not bottom...
return CGPoint(x: pathWidth, y: -pathHeight + height)
}
ZStack{
// Converting plot as points....
// Path....
Path{path in
// drawing the points..
path.move(to: CGPoint(x: 0, y: 0))
path.addLines(points)
}
.strokedPath(StrokeStyle(lineWidth: 2.5, lineCap: .round, lineJoin: .round))
.fill(
// Gradient...
LinearGradient(colors: [
Color("Gradient1"),
Color("Gradient2"),
], startPoint: .leading, endPoint: .trailing)
)
// Path Bacground Coloring...
FillBG()
// Clipping the shape...
.clipShape(
Path{path in
// drawing the points..
path.move(to: CGPoint(x: 0, y: 0))
path.addLines(points)
path.addLine(to: CGPoint(x: proxy.size.width, y: height))
path.addLine(to: CGPoint(x: 0, y: height))
}
)
//.padding(.top,15)
}
.overlay(
// Drag Indiccator...
VStack(spacing: 0){
Text(currentPlot)
.font(.caption.bold())
.foregroundColor(.white)
.padding(.vertical,6)
.padding(.horizontal,10)
.background(Color("Gradient1"),in: Capsule())
.offset(x: translation < 10 ? 30 : 0)
.offset(x: translation > (proxy.size.width - 60) ? -30 : 0)
Rectangle()
.fill(Color("Gradient1"))
.frame(width: 1,height: 40)
.padding(.top)
Circle()
.fill(Color("Gradient1"))
.frame(width: 22, height: 22)
.overlay(
Circle()
.fill(.white)
.frame(width: 10, height: 10)
)
Rectangle()
.fill(Color("Gradient1"))
.frame(width: 1,height: 50)
}
// Fixed Frame..
// For Gesture Calculation...
.frame(width: 80,height: 170)
// 170 / 2 = 85 - 15 => circle ring size...
.offset(y: 70)
.offset(offset)
.opacity(showPlot ? 1 : 0),
alignment: .bottomLeading
)
.contentShape(Rectangle())
.gesture(DragGesture().onChanged({ value in
withAnimation{showPlot = true}
let translation = value.location.x - 20
// Getting index...
let index = max(min(Int((translation / width).rounded() + 1), data.count - 1), 0)
currentPlot = "$ \(data[index])"
self.translation = translation
// removing half width...
offset = CGSize(width: points[index].x - 40, height: points[index].y - height)
}).onEnded({ value in
withAnimation{showPlot = false}
}).updating($isDrag, body: { value, out, _ in
out = true
}))
}
.background(
VStack(alignment: .leading){
let max = data.max() ?? 0
Text("$ \(Int(max))")
.font(.caption.bold())
.offset(y: -5)
Spacer()
Text("$ 0")
.font(.caption.bold())
.offset(y: 5)
}
.frame(maxWidth: .infinity,alignment: .leading)
)
.padding(.horizontal,10)
.onChange(of: isDrag) { newValue in
if !isDrag{showPlot = false}
}
}
@ViewBuilder
func FillBG()->some View{
LinearGradient(colors: [
Color("Gradient2")
.opacity(0.3),
Color("Gradient2")
.opacity(0.2),
Color("Gradient2")
.opacity(0.1)]
+ Array(repeating: Color("Gradient1")
.opacity(0.1), count: 4)
+ Array(repeating: Color.clear, count: 2)
, startPoint: .top, endPoint: .bottom)
}
}
struct LineGraph_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}