-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathDotDrawingLayer.swift
More file actions
105 lines (77 loc) · 3.51 KB
/
DotDrawingLayer.swift
File metadata and controls
105 lines (77 loc) · 3.51 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
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
import UIKit
internal class DotDrawingLayer: ScrollableGraphViewDrawingLayer {
private var dataPointPath = UIBezierPath()
private var dataPointSize: CGFloat = 5
private var dataPointType: ScrollableGraphViewDataPointType = .circle
private var customDataPointPath: ((_ centre: CGPoint) -> UIBezierPath)?
override init(layer: Any) {
super.init(layer: layer)
}
init(frame: CGRect, fillColor: UIColor, dataPointType: ScrollableGraphViewDataPointType, dataPointSize: CGFloat, customDataPointPath: ((_ centre: CGPoint) -> UIBezierPath)? = nil) {
self.dataPointType = dataPointType
self.dataPointSize = dataPointSize
self.customDataPointPath = customDataPointPath
super.init(viewportWidth: frame.size.width, viewportHeight: frame.size.height)
self.fillColor = fillColor.cgColor
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func createDataPointPath() -> UIBezierPath {
dataPointPath.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 dataPointPath
}
let pointPathCreator = getPointPathCreator()
for i in activePointsInterval {
var location = CGPoint.zero
if let pointLocation = owner?.graphPoint(forIndex: i).location {
location = pointLocation
}
let pointPath = pointPathCreator(location)
dataPointPath.append(pointPath)
}
return dataPointPath
}
private func createCircleDataPoint(centre: CGPoint) -> UIBezierPath {
return UIBezierPath(arcCenter: centre, radius: dataPointSize, startAngle: 0, endAngle: CGFloat(2.0 * Double.pi), clockwise: true)
}
private func createSquareDataPoint(centre: CGPoint) -> UIBezierPath {
let squarePath = UIBezierPath()
squarePath.move(to: centre)
let topLeft = CGPoint(x: centre.x - dataPointSize, y: centre.y - dataPointSize)
let topRight = CGPoint(x: centre.x + dataPointSize, y: centre.y - dataPointSize)
let bottomLeft = CGPoint(x: centre.x - dataPointSize, y: centre.y + dataPointSize)
let bottomRight = CGPoint(x: centre.x + dataPointSize, y: centre.y + dataPointSize)
squarePath.move(to: topLeft)
squarePath.addLine(to: topRight)
squarePath.addLine(to: bottomRight)
squarePath.addLine(to: bottomLeft)
squarePath.addLine(to: topLeft)
return squarePath
}
private func getPointPathCreator() -> (_ centre: CGPoint) -> UIBezierPath {
switch(self.dataPointType) {
case .circle:
return createCircleDataPoint
case .square:
return createSquareDataPoint
case .custom:
if let customCreator = self.customDataPointPath {
return customCreator
}
else {
// We don't have a custom path, so just return the default.
fallthrough
}
default:
return createCircleDataPoint
}
}
override func updatePath() {
self.path = createDataPointPath().cgPath
}
}