-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathLinePlot.swift
More file actions
149 lines (112 loc) · 5.14 KB
/
LinePlot.swift
File metadata and controls
149 lines (112 loc) · 5.14 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
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
import UIKit
open class LinePlot : Plot {
// Public settings for the LinePlot
// ################################
/// Specifies how thick the graph of the line is. In points.
open var lineWidth: CGFloat = 2
/// The color of the graph line. UIColor.
open var lineColor: UIColor = UIColor.black
/// Whether the line is straight or curved.
open var lineStyle_: Int {
get { return lineStyle.rawValue }
set {
if let enumValue = ScrollableGraphViewLineStyle(rawValue: newValue) {
lineStyle = enumValue
}
}
}
/// Whether or not the line should be rendered using bezier curves are straight lines.
open var lineStyle = ScrollableGraphViewLineStyle.straight
/// How each segment in the line should connect. Takes any of the Core Animation LineJoin values.
open var lineJoin: String = convertFromCAShapeLayerLineJoin(CAShapeLayerLineJoin.round)
/// The line caps. Takes any of the Core Animation LineCap values.
open var lineCap: String = convertFromCAShapeLayerLineCap(CAShapeLayerLineCap.round)
open var lineCurviness: CGFloat = 0.5
// Fill Settings
// #############
/// Specifies whether or not the plotted graph should be filled with a colour or gradient.
open var shouldFill: Bool = false
/// Specifies whether or not the plotted graph should be started at zero position.
open var shouldStartAtZero: Bool = true
/// Specifies whether or not the plotted graph should be ended at zero position.
open var shouldEndAtZero: Bool = true
var fillType_: Int {
get { return fillType.rawValue }
set {
if let enumValue = ScrollableGraphViewFillType(rawValue: newValue) {
fillType = enumValue
}
}
}
/// Specifies whether to fill the graph with a solid colour or gradient.
open var fillType = ScrollableGraphViewFillType.solid
/// If fillType is set to .Solid then this colour will be used to fill the graph.
open var fillColor: UIColor = UIColor.black
/// If fillType is set to .Gradient then this will be the starting colour for the gradient.
open var fillGradientStartColor: UIColor = UIColor.white
/// If fillType is set to .Gradient, then this will be the ending colour for the gradient.
open var fillGradientEndColor: UIColor = UIColor.black
open var fillGradientType_: Int {
get { return fillGradientType.rawValue }
set {
if let enumValue = ScrollableGraphViewGradientType(rawValue: newValue) {
fillGradientType = enumValue
}
}
}
/// If fillType is set to .Gradient, then this defines whether the gradient is rendered as a linear gradient or radial gradient.
open var fillGradientType = ScrollableGraphViewGradientType.linear
// Private State
// #############
private var lineLayer: LineDrawingLayer?
private var fillLayer: FillDrawingLayer?
private var gradientLayer: GradientDrawingLayer?
public init(identifier: String) {
super.init()
self.identifier = identifier
}
override func layers(forViewport viewport: CGRect) -> [ScrollableGraphViewDrawingLayer?] {
createLayers(viewport: viewport)
return [lineLayer, fillLayer, gradientLayer]
}
private func createLayers(viewport: CGRect) {
// Create the line drawing layer.
lineLayer = LineDrawingLayer(frame: viewport, lineWidth: lineWidth, lineColor: lineColor, lineStyle: lineStyle, lineJoin: lineJoin, lineCap: lineCap, shouldFill: shouldFill, shouldStartAtZero: shouldStartAtZero, shouldEndAtZero: shouldEndAtZero, lineCurviness: lineCurviness)
// Depending on whether we want to fill with solid or gradient, create the layer accordingly.
// Gradient and Fills
switch (self.fillType) {
case .solid:
if(shouldFill) {
// Setup fill
fillLayer = FillDrawingLayer(frame: viewport, fillColor: fillColor, lineDrawingLayer: lineLayer!)
}
case .gradient:
if(shouldFill) {
gradientLayer = GradientDrawingLayer(frame: viewport, startColor: fillGradientStartColor, endColor: fillGradientEndColor, gradientType: fillGradientType, lineDrawingLayer: lineLayer!)
}
}
lineLayer?.owner = self
fillLayer?.owner = self
gradientLayer?.owner = self
}
}
@objc public enum ScrollableGraphViewLineStyle : Int {
case straight
case smooth
}
@objc public enum ScrollableGraphViewFillType : Int {
case solid
case gradient
}
@objc public enum ScrollableGraphViewGradientType : Int {
case linear
case radial
}
// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromCAShapeLayerLineJoin(_ input: CAShapeLayerLineJoin) -> String {
return input.rawValue
}
// Helper function inserted by Swift 4.2 migrator.
fileprivate func convertFromCAShapeLayerLineCap(_ input: CAShapeLayerLineCap) -> String {
return input.rawValue
}