-
-
Notifications
You must be signed in to change notification settings - Fork 6k
/
ChartUtils.swift
298 lines (241 loc) · 8.82 KB
/
ChartUtils.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//
// Utils.swift
// Charts
//
// Copyright 2015 Daniel Cohen Gindi & Philipp Jahoda
// A port of MPAndroidChart for iOS
// Licensed under Apache License 2.0
//
// https://github.com/danielgindi/Charts
//
import Foundation
import CoreGraphics
extension Comparable
{
func clamped(to range: ClosedRange<Self>) -> Self
{
if self > range.upperBound
{
return range.upperBound
}
else if self < range.lowerBound
{
return range.lowerBound
}
else
{
return self
}
}
}
extension FloatingPoint
{
var DEG2RAD: Self
{
return self * .pi / 180
}
var RAD2DEG: Self
{
return self * 180 / .pi
}
/// - Note: Value must be in degrees
/// - Returns: An angle between 0.0 < 360.0 (not less than zero, less than 360)
var normalizedAngle: Self
{
let angle = truncatingRemainder(dividingBy: 360)
return (sign == .minus) ? angle + 360 : angle
}
}
extension CGSize
{
func rotatedBy(degrees: CGFloat) -> CGSize
{
let radians = degrees.DEG2RAD
return rotatedBy(radians: radians)
}
func rotatedBy(radians: CGFloat) -> CGSize
{
return CGSize(
width: abs(width * cos(radians)) + abs(height * sin(radians)),
height: abs(width * sin(radians)) + abs(height * cos(radians))
)
}
}
extension Double
{
/// Rounds the number to the nearest multiple of it's order of magnitude, rounding away from zero if halfway.
func roundedToNextSignificant() -> Double
{
guard
!isInfinite,
!isNaN,
self != 0
else { return self }
let d = ceil(log10(self < 0 ? -self : self))
let pw = 1 - Int(d)
let magnitude = pow(10.0, Double(pw))
let shifted = (self * magnitude).rounded()
return shifted / magnitude
}
var decimalPlaces: Int
{
guard
!isNaN,
!isInfinite,
self != 0.0
else { return 0 }
let i = roundedToNextSignificant()
guard
!i.isInfinite,
!i.isNaN
else { return 0 }
return Int(ceil(-log10(i))) + 2
}
}
extension CGPoint
{
/// Calculates the position around a center point, depending on the distance from the center, and the angle of the position around the center.
func moving(distance: CGFloat, atAngle angle: CGFloat) -> CGPoint
{
return CGPoint(x: x + distance * cos(angle.DEG2RAD),
y: y + distance * sin(angle.DEG2RAD))
}
}
extension CGContext
{
public func drawImage(_ image: NSUIImage, atCenter center: CGPoint, size: CGSize)
{
var drawOffset = CGPoint()
drawOffset.x = center.x - (size.width / 2)
drawOffset.y = center.y - (size.height / 2)
NSUIGraphicsPushContext(self)
if image.size.width != size.width && image.size.height != size.height
{
let key = "resized_\(size.width)_\(size.height)"
// Try to take scaled image from cache of this image
var scaledImage = objc_getAssociatedObject(image, key) as? NSUIImage
if scaledImage == nil
{
// Scale the image
NSUIGraphicsBeginImageContextWithOptions(size, false, 0.0)
image.draw(in: CGRect(origin: .zero, size: size))
scaledImage = NSUIGraphicsGetImageFromCurrentImageContext()
NSUIGraphicsEndImageContext()
// Put the scaled image in a cache owned by the original image
objc_setAssociatedObject(image, key, scaledImage, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
scaledImage?.draw(in: CGRect(origin: drawOffset, size: size))
}
else
{
image.draw(in: CGRect(origin: drawOffset, size: size))
}
NSUIGraphicsPopContext()
}
public func drawText(_ text: String, at point: CGPoint, align: TextAlignment, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5), angleRadians: CGFloat = 0.0, attributes: [NSAttributedString.Key : Any]?)
{
let drawPoint = getDrawPoint(text: text, point: point, align: align, attributes: attributes)
if (angleRadians == 0.0)
{
NSUIGraphicsPushContext(self)
(text as NSString).draw(at: drawPoint, withAttributes: attributes)
NSUIGraphicsPopContext()
}
else
{
drawText(text, at: drawPoint, anchor: anchor, angleRadians: angleRadians, attributes: attributes)
}
}
public func drawText(_ text: String, at point: CGPoint, anchor: CGPoint = CGPoint(x: 0.5, y: 0.5), angleRadians: CGFloat, attributes: [NSAttributedString.Key : Any]?)
{
var drawOffset = CGPoint()
NSUIGraphicsPushContext(self)
if angleRadians != 0.0
{
let size = text.size(withAttributes: attributes)
// Move the text drawing rect in a way that it always rotates around its center
drawOffset.x = -size.width * 0.5
drawOffset.y = -size.height * 0.5
var translate = point
// Move the "outer" rect relative to the anchor, assuming its centered
if anchor.x != 0.5 || anchor.y != 0.5
{
let rotatedSize = size.rotatedBy(radians: angleRadians)
translate.x -= rotatedSize.width * (anchor.x - 0.5)
translate.y -= rotatedSize.height * (anchor.y - 0.5)
}
saveGState()
translateBy(x: translate.x, y: translate.y)
rotate(by: angleRadians)
(text as NSString).draw(at: drawOffset, withAttributes: attributes)
restoreGState()
}
else
{
if anchor.x != 0.0 || anchor.y != 0.0
{
let size = text.size(withAttributes: attributes)
drawOffset.x = -size.width * anchor.x
drawOffset.y = -size.height * anchor.y
}
drawOffset.x += point.x
drawOffset.y += point.y
(text as NSString).draw(at: drawOffset, withAttributes: attributes)
}
NSUIGraphicsPopContext()
}
private func getDrawPoint(text: String, point: CGPoint, align: TextAlignment, attributes: [NSAttributedString.Key : Any]?) -> CGPoint
{
var point = point
if align == .center
{
point.x -= text.size(withAttributes: attributes).width / 2.0
}
else if align == .right
{
point.x -= text.size(withAttributes: attributes).width
}
return point
}
func drawMultilineText(_ text: String, at point: CGPoint, constrainedTo size: CGSize, anchor: CGPoint, knownTextSize: CGSize, angleRadians: CGFloat, attributes: [NSAttributedString.Key : Any]?)
{
var rect = CGRect(origin: .zero, size: knownTextSize)
NSUIGraphicsPushContext(self)
if angleRadians != 0.0
{
// Move the text drawing rect in a way that it always rotates around its center
rect.origin.x = -knownTextSize.width * 0.5
rect.origin.y = -knownTextSize.height * 0.5
var translate = point
// Move the "outer" rect relative to the anchor, assuming its centered
if anchor.x != 0.5 || anchor.y != 0.5
{
let rotatedSize = knownTextSize.rotatedBy(radians: angleRadians)
translate.x -= rotatedSize.width * (anchor.x - 0.5)
translate.y -= rotatedSize.height * (anchor.y - 0.5)
}
saveGState()
translateBy(x: translate.x, y: translate.y)
rotate(by: angleRadians)
(text as NSString).draw(with: rect, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
restoreGState()
}
else
{
if anchor.x != 0.0 || anchor.y != 0.0
{
rect.origin.x = -knownTextSize.width * anchor.x
rect.origin.y = -knownTextSize.height * anchor.y
}
rect.origin.x += point.x
rect.origin.y += point.y
(text as NSString).draw(with: rect, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
}
NSUIGraphicsPopContext()
}
func drawMultilineText(_ text: String, at point: CGPoint, constrainedTo size: CGSize, anchor: CGPoint, angleRadians: CGFloat, attributes: [NSAttributedString.Key : Any]?)
{
let rect = text.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: attributes, context: nil)
drawMultilineText(text, at: point, constrainedTo: size, anchor: anchor, knownTextSize: rect.size, angleRadians: angleRadians, attributes: attributes)
}
}