-
Notifications
You must be signed in to change notification settings - Fork 63
/
BarGraphView.swift
155 lines (124 loc) · 4.97 KB
/
BarGraphView.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
//
// BarGraphView.swift
// Graphs
//
// Created by HiraiKokoro on 2016/06/03.
// Copyright © 2016年 Recruit Holdings Co., Ltd. All rights reserved.
//
import UIKit
public enum GraphColorType {
case
Mat(UIColor),
Gradation(UIColor, UIColor)
}
extension UIColor {
func matColor() -> GraphColorType {
return .Mat(self)
}
}
public struct BarGraphViewConfig {
public var barColor: GraphColorType
public var textColor: UIColor
public var textFont: UIFont
public var textVisible: Bool
public var zeroLineVisible: Bool
public var barWidthScale: CGFloat
public var contentInsets: UIEdgeInsets
public init(
barColor: UIColor? = nil,
textColor: UIColor? = nil,
textFont: UIFont? = nil,
barWidthScale: CGFloat? = nil,
zeroLineVisible: Bool? = nil,
textVisible: Bool? = nil,
contentInsets: UIEdgeInsets? = nil
) {
self.barColor = (barColor ?? DefaultColorType.Bar.color()).matColor()
self.textColor = textColor ?? DefaultColorType.BarText.color()
self.textFont = textFont ?? UIFont.systemFontOfSize(10.0)
self.barWidthScale = barWidthScale ?? 0.8
self.zeroLineVisible = zeroLineVisible ?? true
self.textVisible = textVisible ?? true
self.contentInsets = contentInsets ?? UIEdgeInsetsZero
}
}
internal class BarGraphView<T: Hashable, U: NumericType>: UIView {
internal var graph: BarGraph<T, U>?
private var config = BarGraphViewConfig()
init(frame: CGRect, graph: BarGraph<T, U>?) {
self.graph = graph
super.init(frame: frame)
self.backgroundColor = UIColor.clearColor()
self.setNeedsDisplay()
}
func setBarGraphViewConfig(config: BarGraphViewConfig?) {
self.config = config ?? BarGraphViewConfig()
self.setNeedsDisplay()
}
private func graphFrame() -> CGRect {
return CGRect(
x: self.config.contentInsets.left,
y: self.config.contentInsets.top,
width: self.frame.size.width - self.config.contentInsets.horizontalMarginsTotal(),
height: self.frame.size.height - self.config.contentInsets.verticalMarginsTotal()
)
}
override func drawRect(rect: CGRect) {
super.drawRect(rect)
guard let graph = self.graph else { return }
let total = graph.units.map{ $0.value }.reduce(U(0)){ $0 + $1 }
let rect = self.graphFrame()
let min = graph.range.min
let max = graph.range.max
let sectionWidth = rect.size.width / CGFloat(graph.units.count)
let width = sectionWidth * self.config.barWidthScale
let zero = rect.size.height / CGFloat((max - min).floatValue()) * CGFloat(min.floatValue())
graph.units.enumerate().forEach({ (index, u) in
switch self.config.barColor {
case let .Mat(color): color.setFill()
case .Gradation(_, _): break
}
let height = { () -> CGFloat in
switch u.value {
case let n where n > U(0):
return rect.size.height * CGFloat(
u.value.floatValue() / (max - min).floatValue()
)
case let n where n < U(0):
return rect.size.height * CGFloat(
-(u.value).floatValue() / (max - min).floatValue()
)
case _:
return 0.0
}
}()
let path = UIBezierPath(
rect: CGRect(
x: sectionWidth * CGFloat(index) + (sectionWidth - width) / 2.0 + rect.origin.x,
y: (u.value >= U(0) ? rect.size.height - height : rect.size.height) + zero + rect.origin.y,
width: width,
height: height
)
)
path.fill()
if let str = self.graph?.graphTextDisplay()(unit: u, totalValue: total) {
let attrStr = NSAttributedString.graphAttributedString(str, color: self.config.textColor, font: self.config.textFont)
let size = attrStr.size()
attrStr.drawInRect(
CGRect(
origin: CGPoint(
x: sectionWidth * CGFloat(index) + rect.origin.x,
y: u.value >= U(0)
? rect.size.height - height + zero - size.height - 3.0 + rect.origin.y
: rect.size.height + zero + height + 3.0 + rect.origin.y
),
size: CGSize(
width: sectionWidth,
height: size.height
)
)
)
}
})
}
}