LineChartView.swift
7.1 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
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
//
// LineChartView.swift
// ParentAssistant
//
// Created by Cao yang on 2018/3/23.
// Copyright © 2018年 HANGZHOUTEAM. All rights reserved.
//
import UIKit
class LineChartView: UIView {
//默认横坐标
private var xLabArr = ["行为","文明","课堂","作业","礼仪"]
//设置纵坐标
private let yCount = 6
//横坐标个数
private let xCount = 5
//最大绩点分数
private let GPA = 5
//储存横纵坐标,画线用
private var xCenterArr = Array<CGFloat>()
private var yCenterArr = Array<CGFloat>()
//判断是否重复点击
private var btnTouch = 10
//虚线
private let lineLayer = CAShapeLayer()
//横坐标下划线
private let xMoveLayer = CAShapeLayer()
//纵坐标Lab数组
private var yMoveLaArr = Array<UILabel>()
//折线图数据数组
private var dateArr = Array<Array<Int>>()
//显示的数字
var yLabArray = Array<UILabel>()
override init(frame: CGRect) {
super.init(frame: frame)
print("frame==\(frame)")
}
func drawUI(xLableArr:Array<String>?){
if xLableArr != nil {
xLabArr = xLableArr!
}
drawX()
drawY()
}
//设置纵坐标
private func drawY() {
let yHeight = (self.bounds.height-60)/CGFloat(yCount)
for index in 0..<yCount{
let yLab = UILabel.init(frame: CGRect.init(x: 10, y: CGFloat(index)*yHeight, width: 20, height: 20))
yLab.text = "\(GPA-index)"
yLab.textColor = UIColorWithRGB(R: 119, G: 119, B: 119)
yLab.font = UIFont.systemFont(ofSize: 14)
self.addSubview(yLab)
let lineLab = UILabel.init(frame: CGRect.init(x: 30, y: yLab.center.y, width: screenWidth-60, height: 0.4))
lineLab.backgroundColor = UIColorWithRGB(R: 190, G: 190, B: 190)
self.addSubview(lineLab)
yMoveLaArr.append(yLab)
yCenterArr.append(yLab.frame.origin.y+10)
}
}
//设置横坐标
private func drawX() {
let xWidth = (screenWidth-20)/CGFloat(yCount)
for index in 0..<xCount {
let nameBtn = UIButton.init(frame: CGRect.init(x: xWidth+CGFloat(index)*xWidth, y: self.bounds.height-70, width: 40, height: 25))
nameBtn.setTitle(xLabArr[index], for: UIControlState.normal)
nameBtn.setTitleColor(UIColorWithRGB(R: 119, G: 119, B: 119), for: UIControlState.normal)
nameBtn.titleLabel!.textAlignment = .center
nameBtn.titleLabel!.font = UIFont.systemFont(ofSize: 13)
// nameBtn.backgroundColor = UIColor.orange
nameBtn.tag = index
self.addSubview(nameBtn)
nameBtn.addTarget(self, action: #selector(touchXAction(tap:)), for: UIControlEvents.touchUpInside)
xCenterArr.append(nameBtn.frame.origin.x+20)
}
}
//MARK: - 画折线
func drawLine(point:Array<Int>,color:UIColor){
let pointArr = swichPoint(arr: point)
let drawLayer = DrawLayer()
drawLayer.xPointArr = xCenterArr
drawLayer.yPointArr = [yCenterArr[pointArr[0]],yCenterArr[pointArr[1]],yCenterArr[pointArr[2]],yCenterArr[pointArr[3]],yCenterArr[pointArr[4]]]
drawLayer.bounds = self.bounds
drawLayer.position = self.center
drawLayer.lineColor = color
self.layer.addSublayer(drawLayer)
drawLayer.setNeedsDisplay()
dateArr.append(point)
}
//MARK: - 点击横坐标
@objc func touchXAction(tap:UIButton){
let index = tap.tag
if btnTouch != index {
//竖虚线
lineLayer.strokeColor = UIColorWithRGB(R: 92, G: 145, B: 230).cgColor
lineLayer.lineWidth = 1
let arr = [5,5]
lineLayer.lineDashPhase = 0
lineLayer.lineDashPattern = arr as [NSNumber]
self.layer.addSublayer(lineLayer)
//X轴下划线
let movePath = CGMutablePath()
movePath.move(to: CGPoint.init(x: xCenterArr[index], y: yCenterArr[0]))
movePath.addLine(to: CGPoint.init(x: xCenterArr[index], y: 10+5*(self.bounds.height-60)/CGFloat(yCount)))
lineLayer.path = movePath
xMoveLayer.strokeColor = UIColorWithRGB(R: 92, G: 145, B: 230).cgColor
xMoveLayer.lineWidth = 2
self.layer.addSublayer(xMoveLayer)
let movePath2 = CGMutablePath()
movePath2.move(to: CGPoint.init(x: xCenterArr[index]-15, y: self.bounds.height-45))
movePath2.addLine(to: CGPoint.init(x: xCenterArr[index]+15, y: self.bounds.height-45))
xMoveLayer.path = movePath2
if yLabArray.count == dateArr.count {
for label in yLabArray {
label.removeFromSuperview()
}
}
yLabArray.removeAll()
//数字显示
for i in 0..<dateArr.count {
let arr = dateArr[i]
//y坐标数值
let y = arr[index]
let yLab = yMoveLaArr[GPA-y]
var rect = yLab.frame
rect.origin.x = xCenterArr[index]+10
let newLab = UILabel()
newLab.text = yLab.text
newLab.textColor = yLab.textColor
newLab.font = yLab.font
newLab.frame = rect
self.addSubview(newLab)
yLabArray.append(newLab)
}
}else{
print(yLabArray)
}
btnTouch = index
}
//转换y坐标
func swichPoint(arr:Array<Int>)->Array<Int>{
var array = Array<Int>()
for index in 0..<arr.count {
let value = GPA - arr[index]
array.append(value)
}
return array
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
/*
// Only override draw() if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
override func draw(_ rect: CGRect) {
// Drawing code
}
*/
}
class DrawLayer: CALayer {
var yPointArr = Array<CGFloat>()
var xPointArr = Array<CGFloat>()
var lineColor = UIColor()
override func draw(in ctx: CGContext) {
let context = ctx
//点
context.setFillColor(lineColor.cgColor)
for i in 0..<xPointArr.count {
context.fillEllipse(in: CGRect.init(x: xPointArr[i]-5, y: yPointArr[i]-5, width: 10, height: 10))
}
//折线
context.setStrokeColor(lineColor.cgColor)
context.setLineWidth(1.5)
for i in 0..<xPointArr.count {
if i == 0 {
context.move(to: CGPoint.init(x: xPointArr[0] , y: yPointArr[0]))
}else{
context.addLine(to: CGPoint.init(x: xPointArr[i] , y: yPointArr[i]))
}
}
context.strokePath()
}
}