-
Notifications
You must be signed in to change notification settings - Fork 0
/
PNLineChart.swift
executable file
·477 lines (386 loc) · 20.2 KB
/
PNLineChart.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
//
// PNLineChart.swift
// PNChartSwift
//
// Created by YiChen Zhou on 11/10/16.
// Copyright © 2016 YiChen Zhou. All rights reserved.
//
import UIKit
import QuartzCore
class PNLineChart: UIView{
public var xLabels: NSArray = []{
didSet {
if showLabel {
xLabelWidth = chartCavanWidth! / CGFloat(xLabels.count)
for index in 0..<xLabels.count {
let labelText = xLabels[index] as! String
let labelX = 2.0 * chartMargin + (CGFloat(index) * xLabelWidth) - (xLabelWidth / 2.0)
let label: PNChartLabel = PNChartLabel(frame: CGRect(x: CGFloat(labelX), y: CGFloat(chartMargin + chartCavanHeight!), width: CGFloat(xLabelWidth), height: CGFloat(chartMargin)))
label.textAlignment = NSTextAlignment.center
label.text = labelText
addSubview(label)
}
} else {
xLabelWidth = frame.size.width / CGFloat(xLabels.count)
}
}
}
public var yLabels: NSArray = []{
didSet {
yLabelNum = CGFloat(yLabels.count)
let ySetp: CGFloat = (yValueMax - yValueMin) / CGFloat(yLabelNum)
let yStepHeight: CGFloat = chartCavanHeight / CGFloat(yLabelNum)
var index: CGFloat = 0.0
for _ in yLabels {
let labelY = chartCavanHeight - (index * yStepHeight)
let label: PNChartLabel = PNChartLabel(frame: CGRect(x: CGFloat(0.0), y: labelY, width: chartMargin + 5.0, height: CGFloat(yLabelHeight)))
label.textAlignment = .right
label.text = String(format: yLabelFormat, Double(yValueMin + (ySetp * index)))
index += 1
addSubview(label)
}
}
}
// Array of LineChartData objects, one for eacg line
public var chartData: NSArray = []{
didSet{
let yLabelsArray: NSMutableArray = NSMutableArray(capacity: chartData.count)
var yMax: CGFloat = 0.0
var yMin: CGFloat = CGFloat.infinity
var yValue: CGFloat!
// remove all shape layers before adding new ones
for layer in chartLineArray {
(layer as! CALayer).removeFromSuperlayer()
}
for layer in chartPointArray {
(layer as! CALayer).removeFromSuperlayer()
}
chartLineArray = NSMutableArray(capacity: chartData.count)
chartPointArray = NSMutableArray(capacity: chartData.count)
// Set for point stroken
let circleStrokeWidth: CGFloat = 2.0
let lineWidth: CGFloat = 3.0
for chart in chartData {
// Create as many chart line layers as number of data
let chartObj = chart as! PNLineChartData
let chartLine: CAShapeLayer = CAShapeLayer()
chartLine.lineCap = kCALineCapButt
chartLine.lineJoin = kCALineJoinMiter
chartLine.fillColor = UIColor.white.cgColor
chartLine.lineWidth = lineWidth
chartLine.strokeEnd = 0.0
layer.addSublayer(chartLine)
chartLineArray.add(chartLine)
// Create as many chart point layers as number of data
let pointLayer: CAShapeLayer = CAShapeLayer()
pointLayer.strokeColor = chartObj.color.cgColor
pointLayer.lineCap = kCALineCapRound
pointLayer.lineJoin = kCALineJoinBevel
pointLayer.fillColor = nil
pointLayer.lineWidth = circleStrokeWidth
layer.addSublayer(pointLayer)
chartPointArray.add(pointLayer)
for index in 0..<chartObj.itemCount {
yValue = CGFloat(chartObj.getData(index).y)
yLabelsArray.add(String(format: "%2f", yValue))
yMax = fmax(yMax, yValue)
yMin = fmin(yMin, yValue)
}
}
// Min value for Y label
if yMax < 5 {
yMax = 5.0
}
if yMin < 0 {
yMin = 0.0
}
yValueMin = yMin
yValueMax = yMax
if showLabel {
yLabels = yLabelsArray as NSArray
}
setNeedsDisplay()
}
}
var pathPoints: NSMutableArray = []
// X-Axis Info
public var xLabelWidth: CGFloat = 0.0
// Y-Axis Info
public var yValueMax: CGFloat = 10.0
public var yValueMin: CGFloat = 1.0
public var yLabelNum: CGFloat = 0.0
public var yLabelHeight: CGFloat = 12.0
// Chart Info
public var showLabel: Bool = true
public var showCoordinateAxis: Bool = true
public var chartCavanHeight: CGFloat!
public var chartCavanWidth: CGFloat!
public var chartMargin: CGFloat = 25.0
// For Axis
public var axisColor: UIColor = PNGrey
public var axisWidth: CGFloat = 1.0
public var xUnit: NSString!
public var yUnit: NSString!
// String Format for float values in y labels.
public var yLabelFormat = "%1.1f"
var chartLineArray: NSMutableArray = []
var chartPointArray: NSMutableArray = []
var chartPaths: NSMutableArray = []
var pointPaths: NSMutableArray = []
// Initialize Methods
override init(frame: CGRect) {
super.init(frame: frame)
setDefaultValues()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: Functions
func setDefaultValues() {
self.backgroundColor = UIColor.white
self.clipsToBounds = true
self.chartLineArray = NSMutableArray()
self.showLabel = false
self.pathPoints = NSMutableArray()
self.isUserInteractionEnabled = true
self.chartCavanWidth = self.frame.size.width - (chartMargin * 2.0)
self.chartCavanHeight = self.frame.size.height - (chartMargin * 2.0)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
touchPoint(touches: touches as NSSet, withEvent: event!)
touchKeyPoint(touches: touches as NSSet, withEvent: event!)
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
touchPoint(touches: touches as NSSet, withEvent: event!)
touchKeyPoint(touches: touches as NSSet, withEvent: event!)
}
func touchPoint(touches: NSSet, withEvent event: UIEvent) {
let touch: UITouch = touches.anyObject() as! UITouch
let touchPoint = touch.location(in: self)
for linePoints in pathPoints {
let linePointsArray = linePoints as! Array<Any>
for index in 0..<(linePointsArray.count - 1) {
let p1: CGPoint = (linePointsArray[index] as! PNValue).point
let p2: CGPoint = (linePointsArray[index + 1] as! PNValue).point
// The closest distance from point to value
var distance: CGFloat = fabs(((p2.x - p1.x) * (touchPoint.y - p1.y)) - ((p1.x - touchPoint.x) * (p1.y - p2.y)))
distance = distance / hypot(p2.x - p1.x, p1.y - p2.y)
if distance <= 5.0 {
for path in chartPaths {
if (path as AnyObject).contains(p1) {
userClickedOnLinePoint(point: touchPoint, lineIndex: chartPaths.index(of: path))
}
}
}
}
}
}
func touchKeyPoint(touches: NSSet, withEvent event: UIEvent) {
let touch: UITouch = touches.anyObject() as! UITouch
let touchPoint = touch.location(in: self)
for linePoints in pathPoints {
let linePointsArray = pathPoints as NSArray
for index in 0..<(linePointsArray.count - 1) {
let p1: CGPoint = (linePointsArray[index] as! PNValue).point
let p2: CGPoint = (linePointsArray[index + 1] as! PNValue).point
let distanceToP1: CGFloat = fabs(CGFloat(hypot(touchPoint.x - p1.x , touchPoint.y - p1.y )))
let distanceToP2: CGFloat = hypot( touchPoint.x - p2.x, touchPoint.y - p2.y)
let distance: CGFloat = fmin(distanceToP1, distanceToP2)
if distance <= 10.0 {
userClickedOnLineKeyPoint(point: touchPoint, lineIndex: pathPoints.index(of: linePoints), keyPointIndex: (distance == distanceToP2 ? index + 1 : index))
}
}
}
}
// This method will be called and stroke the line in animation
func strokeChart() {
let chartPaths = NSMutableArray()
let pointPaths = NSMutableArray()
for lineIndex in 0..<chartData.count {
let chartData: PNLineChartData = self.chartData[lineIndex] as! PNLineChartData
let chartLine = chartLineArray[lineIndex] as! CAShapeLayer
let pointLayer = chartPointArray[lineIndex] as! CAShapeLayer
var yValue: CGFloat!
var innerGrade: CGFloat!
UIGraphicsBeginImageContext(self.frame.size)
let progressLine = UIBezierPath()
progressLine.lineWidth = chartData.lineWidth
progressLine.lineCapStyle = .round
progressLine.lineJoinStyle = .round
let pointPath = UIBezierPath()
pointPath.lineWidth = chartData.lineWidth
chartPaths.add(progressLine)
pointPaths.add(pointPath)
if !showLabel {
chartCavanHeight = self.frame.size.height - 2.0 * yLabelHeight
chartCavanWidth = self.frame.size.width
chartMargin = 0.0
xLabelWidth = (chartCavanWidth / CGFloat(xLabels.count - 1))
}
let linePointsArray = NSMutableArray()
var lastX: CGFloat = 0.0
var lastY: CGFloat = 0.0
let inflexionWidth = chartData.inflexionPointWidth
for index in 0..<chartData.itemCount {
yValue = CGFloat(chartData.getData(index).y)
innerGrade = (yValue - yValueMin) / (yValueMax - yValueMin)
let x: CGFloat = chartMargin * 2.0 + (CGFloat(index) * xLabelWidth)
let y: CGFloat = chartCavanHeight - (innerGrade * chartCavanHeight!)
switch chartData.inflexPointStyle {
// Cycle Style Point
case .Cycle:
let circleRect = CGRect(x: x - inflexionWidth/2.0, y: y - inflexionWidth/2.0, width: inflexionWidth, height: inflexionWidth)
let circleCenter = CGPoint(x: circleRect.origin.x + circleRect.size.width / 2.0, y: circleRect.origin.y + circleRect.size.height / 2.0)
pointPath.move(to: CGPoint(x: circleCenter.x + inflexionWidth / 2.0, y: circleCenter.y))
pointPath.addArc(withCenter: circleCenter, radius: CGFloat(inflexionWidth/2.0), startAngle: 0.0, endAngle: CGFloat(Double.pi*2.0), clockwise: true)
if index != 0 {
// Calculate the point for line
let distance = CGFloat(sqrt(pow(Double(x - lastX), 2.0) + pow(Double(y - lastY), 2.0)))
let lastX1 = lastX + (inflexionWidth/2.0) / distance * (x - lastX)
let lastY1 = lastY + (inflexionWidth/2.0) / distance * (y - lastY)
let x1 = x - (inflexionWidth/2.0) / distance * (x - lastX)
let y1 = y - (inflexionWidth/2.0) / distance * (y - lastY)
progressLine.move(to: CGPoint(x: lastX1, y: lastY1))
progressLine.addLine(to: CGPoint(x: x1, y: y1))
}
lastX = x
lastY = y
// Square Style Point
case .Square:
let squareRect = CGRect(x: x - inflexionWidth/2.0, y: y - inflexionWidth/2.0, width: inflexionWidth, height: inflexionWidth)
let squareCenter = CGPoint(x: squareRect.origin.x + (squareRect.size.width / 2.0), y: squareRect.origin.y + (squareRect.size.height / 2.0))
pointPath.move(to: CGPoint(x: squareCenter.x - (inflexionWidth/2.0), y: squareCenter.y - (inflexionWidth/2.0)))
pointPath.addLine(to: CGPoint(x: squareCenter.x + (inflexionWidth/2.0), y: squareCenter.y - (inflexionWidth/2.0)))
pointPath.addLine(to: CGPoint(x: squareCenter.x + (inflexionWidth/2.0), y: squareCenter.y + (inflexionWidth/2.0)))
pointPath.addLine(to: CGPoint(x: squareCenter.x - (inflexionWidth/2.0), y: squareCenter.y + (inflexionWidth/2.0)))
pointPath.close()
if index != 0 {
// Calculate the point for line
let distance = CGFloat(sqrt(pow(x - lastX, 2.0) + pow(y - lastY, 2.0)))
let lastX1 = lastX + (inflexionWidth / 2.0)
let lastY1 = lastY + (inflexionWidth / 2.0) / distance * (y - lastY)
let x1 = x - (inflexionWidth / 2.0)
let y1 = y - (inflexionWidth / 2.0) / distance * (y - lastY)
progressLine.move(to: CGPoint(x: lastX1, y: lastY1))
progressLine.addLine(to: CGPoint(x: x1, y: y1))
}
lastX = x
lastY = y
// Triangle Style Point
case .Triangle:
if index != 0 {
progressLine.addLine(to: CGPoint(x: x, y: y))
}
progressLine.move(to: CGPoint(x: x, y: y))
default:
if index != 0 {
progressLine.addLine(to: CGPoint(x: x, y: y))
}
progressLine.move(to: CGPoint(x: x, y: y))
}
linePointsArray.add(PNValue(point: CGPoint(x: x, y: y)))
}
pathPoints.add(linePointsArray)
// Setup color for chart line
if chartData.color != UIColor.black {
chartLine.strokeColor = chartData.color.cgColor
pointLayer.strokeColor = chartData.color.cgColor
} else {
chartLine.strokeColor = PNGreen.cgColor
chartLine.strokeColor = PNGreen.cgColor
}
progressLine.stroke()
chartLine.path = progressLine.cgPath
pointLayer.path = pointPath.cgPath
CATransaction.begin()
let path = CABasicAnimation(keyPath: "strokeEnd")
path.duration = 1.0
path.timingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
path.fromValue = 0.0
path.toValue = 1.0
chartLine.add(path, forKey: "strokeEndAnimation")
chartLine.strokeEnd = 1.0
if chartData.inflexPointStyle != .None {
pointLayer.add(path, forKey: "strokeEndAnimation")
}
CATransaction.commit()
UIGraphicsEndImageContext()
}
}
override func draw(_ rect: CGRect) {
if showCoordinateAxis {
let yAxisOffset: CGFloat = 10.0
let ctx = UIGraphicsGetCurrentContext()
UIGraphicsPushContext(ctx!)
ctx?.setLineWidth(axisWidth)
ctx?.setStrokeColor(axisColor.cgColor)
let xAxisWidth: CGFloat = rect.width - chartMargin / 2.0
let yAxisHeight: CGFloat = chartMargin + chartCavanHeight
// Draw coordinate axis
ctx?.move(to: CGPoint(x: chartMargin + yAxisOffset, y: 0.0))
ctx?.addLine(to: CGPoint(x: chartMargin + yAxisOffset, y: yAxisHeight))
ctx?.addLine(to: CGPoint(x: xAxisWidth, y: yAxisHeight))
ctx?.strokePath()
// Draw y axis arrow
ctx?.move(to: CGPoint(x: chartMargin + yAxisOffset - 3.0, y: 6.0))
ctx?.addLine(to: CGPoint(x: chartMargin + yAxisOffset, y: 0.0))
ctx?.addLine(to: CGPoint(x: chartMargin + yAxisOffset + 3.0, y: 6.0))
ctx?.strokePath()
// Draw x axis arrow
ctx?.move(to: CGPoint(x: xAxisWidth - 6.0, y: yAxisHeight - 3.0))
ctx?.addLine(to: CGPoint(x: xAxisWidth, y: yAxisHeight))
ctx?.addLine(to: CGPoint(x: xAxisWidth - 6.0, y: yAxisHeight + 3.0))
ctx?.strokePath()
if showLabel {
// Draw x axis separator
var point: CGPoint!
for index in 0..<xLabels.count {
point = CGPoint(x: 2.0 * chartMargin + CGFloat(index) * xLabelWidth, y: chartMargin + chartCavanHeight!)
ctx?.move(to: CGPoint(x: point.x, y: point.y - 2.0))
ctx?.addLine(to: CGPoint(x: point.x, y: point.y))
ctx?.strokePath()
}
// Draw y axis separator
let yStepHeight = chartCavanHeight / yLabelNum
for index in 0..<xLabels.count {
point = CGPoint(x: chartMargin + yAxisOffset, y: (chartCavanHeight - CGFloat(index) * yStepHeight + yLabelHeight / 2.0))
ctx?.move(to: CGPoint(x: point.x, y: point.y))
ctx?.addLine(to: CGPoint(x: point.x + 2.0, y: point.y))
ctx?.strokePath()
}
}
let font = UIFont(name: "Avenir-Medium", size: 11.0)
// Draw y unit
if yUnit != nil {
let height = heightOfString(text: yUnit, width: 30.0, font: font!)
let drawRect = CGRect(x: chartMargin + 15.0, y: 0.0, width: 30.0, height: height)
drawTextInContext(ctx: ctx!, text: yUnit, rect: drawRect, font: font!)
}
if xUnit != nil {
let height = heightOfString(text: xUnit, width: 30.0, font: font!)
let drawRect = CGRect(x: rect.width - chartMargin + 5.0, y: chartMargin + chartCavanHeight - height / 2.0, width: 25.0, height: height)
drawTextInContext(ctx: ctx!, text: xUnit, rect: drawRect, font: font!)
}
}
super.draw(rect)
}
}
extension PNLineChart {
func heightOfString(text: NSString, width: CGFloat, font: UIFont) -> CGFloat {
let size = CGSize(width: width, height: CGFloat.greatestFiniteMagnitude)
let rect = text.boundingRect(with: size, options: .usesLineFragmentOrigin, attributes: [NSFontAttributeName: font], context: nil)
return rect.size.height
}
func drawTextInContext(ctx: CGContext, text: NSString, rect: CGRect, font: UIFont) {
let priceParagraphStyle: NSMutableParagraphStyle = NSParagraphStyle.default as! NSMutableParagraphStyle
priceParagraphStyle.lineBreakMode = .byTruncatingTail
priceParagraphStyle.alignment = .left
text.draw(in: rect, withAttributes: [NSParagraphStyleAttributeName: priceParagraphStyle])
}
func userClickedOnLineKeyPoint(point: CGPoint, lineIndex: Int, keyPointIndex: Int) {
print("Click Key on line \(point.x), \(point.y) line index is \(lineIndex) and point index is \(keyPointIndex)")
}
func userClickedOnLinePoint(point: CGPoint, lineIndex: Int) {
print("Click Key on line \(point.x), \(point.y) line index is \(lineIndex)")
}
}