-
Notifications
You must be signed in to change notification settings - Fork 0
/
AnimationFunctions.swift
147 lines (79 loc) · 2.84 KB
/
AnimationFunctions.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
//
// AnimationFunctions.swift
// LineChartTest
//
// Created by Tyler Falcoff on 2/9/19.
// Copyright © 2019 Tyler Falcoff. All rights reserved.
//
import Foundation
import UIKit
func drawSeries(CurrentVC: UIView, ySeries:[Double]) {
drawRect(CurrentVC: CurrentVC)
let fitY = scaleYSeries(CurrentVC: CurrentVC, ySeries: ySeries)
var xSeries:[Double] = []
for i in 1...ySeries.count {
xSeries.append(Double(i))
}
let fitX = scaleXSeries(CurrentVC: CurrentVC, xSeries: xSeries)
for i in 1..<fitX.count {
drawLine(CurrentVC: CurrentVC, start: CGPoint(x: fitX[i-1], y: fitY[i-1]), end: CGPoint(x: fitX[i], y: fitY[i]))
}
}
func drawLine(CurrentVC: UIView, start: CGPoint, end: CGPoint) {
//design the path
let path = UIBezierPath()
path.move(to: start)
path.addLine(to: end)
//design path in layer
let shapeLayer = CAShapeLayer()
shapeLayer.path = path.cgPath
shapeLayer.strokeColor = UIColor.red.cgColor
shapeLayer.lineWidth = 1.0
CurrentVC.layer.addSublayer(shapeLayer)
}
func scaleXSeries(CurrentVC: UIView, xSeries: [Double]) -> [Double] {
var scaledXSeries: [Double] = []
let screenSize = CurrentVC.frame.size
let max = findMax(series: xSeries)
let min = findMin(series: xSeries)
let scaleFactor = Double(screenSize.width)/(max-min)
for i in xSeries {
scaledXSeries.append((i-min)*scaleFactor)
}
return scaledXSeries
}
func scaleYSeries(CurrentVC: UIView, ySeries: [Double]) -> [Double] {
var scaledYSeries: [Double] = []
let screenSize = CurrentVC.frame.size
let max = findMax(series: ySeries)
let min = findMin(series: ySeries)
let scaleFactor = Double(screenSize.height)/(max-min)
for i in ySeries {
scaledYSeries.append(Double(screenSize.height) - (i-min)*scaleFactor)
}
return scaledYSeries
}
func findMax(series: [Double]) -> Double {
var max = series[0]
for i in series {
if (i > max) {
max = i
}
}
return max
}
func findMin(series: [Double]) -> Double {
var min = series[0]
for i in series {
if (i < min) {
min = i
}
}
return min
}
func drawRect(CurrentVC: UIView) {
let screenSize = CurrentVC.frame.size
let img = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0),size: CGSize(width: screenSize.width, height: screenSize.height)))
img.backgroundColor = UIColor(red: 158/255, green: 215/255, blue: 245/255, alpha: 1)
CurrentVC.addSubview(img)
}