-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathBarDrawingLayer.swift
More file actions
80 lines (58 loc) · 2.41 KB
/
BarDrawingLayer.swift
File metadata and controls
80 lines (58 loc) · 2.41 KB
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
import UIKit
// MARK: Drawing the bars
internal class BarDrawingLayer: ScrollableGraphViewDrawingLayer {
private var barPath = UIBezierPath()
private var barWidth: CGFloat = 4
private var shouldRoundCorners = false
override init(layer: Any) {
super.init(layer: layer)
}
init(frame: CGRect, barWidth: CGFloat, barColor: UIColor, barLineWidth: CGFloat, barLineColor: UIColor, shouldRoundCorners: Bool) {
super.init(viewportWidth: frame.size.width, viewportHeight: frame.size.height)
self.barWidth = barWidth
self.lineWidth = barLineWidth
self.strokeColor = barLineColor.cgColor
self.fillColor = barColor.cgColor
self.shouldRoundCorners = shouldRoundCorners
self.lineJoin = lineJoin
self.lineCap = lineCap
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func createBarPath(centre: CGPoint) -> UIBezierPath {
let barWidthOffset: CGFloat = self.barWidth / 2
let origin = CGPoint(x: centre.x - barWidthOffset, y: centre.y)
let size = CGSize(width: barWidth, height: zeroYPosition - centre.y)
let rect = CGRect(origin: origin, size: size)
let barPath: UIBezierPath = {
if shouldRoundCorners {
return UIBezierPath(roundedRect: rect, cornerRadius: barWidthOffset)
} else {
return UIBezierPath(rect: rect)
}
}()
return barPath
}
private func createPath () -> UIBezierPath {
barPath.removeAllPoints()
// We can only move forward if we can get the data we need from the delegate.
guard let
activePointsInterval = self.owner?.graphViewDrawingDelegate?.intervalForActivePoints()
else {
return barPath
}
for i in activePointsInterval {
var location = CGPoint.zero
if let pointLocation = owner?.graphPoint(forIndex: i).location {
location = pointLocation
}
let pointPath = createBarPath(centre: location)
barPath.append(pointPath)
}
return barPath
}
override func updatePath() {
self.path = createPath ().cgPath
}
}