Commit 55f4e53f08b2b386c95a6e01e90a8e950383c640
1 parent
79394633
Exists in
parentassistant
点击折线图添加数字显示
Showing
2 changed files
with
40 additions
and
10 deletions
 
Show diff stats
ParentAssistant/ParentAssistant.xcworkspace/xcuserdata/caoyang.xcuserdatad/UserInterfaceState.xcuserstate
No preview for this file type
ParentAssistant/ParentAssistant/Classes/controllers/report/View/LineChartView.swift
| @@ -29,9 +29,11 @@ class LineChartView: UIView { | @@ -29,9 +29,11 @@ class LineChartView: UIView { | ||
| 29 | private let xMoveLayer = CAShapeLayer() | 29 | private let xMoveLayer = CAShapeLayer() | 
| 30 | //纵坐标Lab数组 | 30 | //纵坐标Lab数组 | 
| 31 | private var yMoveLaArr = Array<UILabel>() | 31 | private var yMoveLaArr = Array<UILabel>() | 
| 32 | - | ||
| 33 | - | ||
| 34 | - | 32 | + //折线图数据数组 | 
| 33 | + private var dateArr = Array<Array<Int>>() | ||
| 34 | + //显示的数字 | ||
| 35 | + var yLabArray = Array<UILabel>() | ||
| 36 | + | ||
| 35 | override init(frame: CGRect) { | 37 | override init(frame: CGRect) { | 
| 36 | super.init(frame: frame) | 38 | super.init(frame: frame) | 
| 37 | print("frame==\(frame)") | 39 | print("frame==\(frame)") | 
| @@ -68,7 +70,7 @@ class LineChartView: UIView { | @@ -68,7 +70,7 @@ class LineChartView: UIView { | ||
| 68 | private func drawX() { | 70 | private func drawX() { | 
| 69 | let xWidth = (screenWidth-20)/CGFloat(yCount) | 71 | let xWidth = (screenWidth-20)/CGFloat(yCount) | 
| 70 | for index in 0..<xCount { | 72 | for index in 0..<xCount { | 
| 71 | - let nameBtn = UIButton.init(frame: CGRect.init(x: xWidth+CGFloat(index)*xWidth, y: self.bounds.height-70, width: 40, height: 20)) | 73 | + let nameBtn = UIButton.init(frame: CGRect.init(x: xWidth+CGFloat(index)*xWidth, y: self.bounds.height-70, width: 40, height: 25)) | 
| 72 | nameBtn.setTitle(xLabArr[index], for: UIControlState.normal) | 74 | nameBtn.setTitle(xLabArr[index], for: UIControlState.normal) | 
| 73 | nameBtn.setTitleColor(UIColorWithRGB(R: 119, G: 119, B: 119), for: UIControlState.normal) | 75 | nameBtn.setTitleColor(UIColorWithRGB(R: 119, G: 119, B: 119), for: UIControlState.normal) | 
| 74 | nameBtn.titleLabel!.textAlignment = .center | 76 | nameBtn.titleLabel!.textAlignment = .center | 
| @@ -97,14 +99,18 @@ class LineChartView: UIView { | @@ -97,14 +99,18 @@ class LineChartView: UIView { | ||
| 97 | self.layer.addSublayer(drawLayer) | 99 | self.layer.addSublayer(drawLayer) | 
| 98 | drawLayer.setNeedsDisplay() | 100 | drawLayer.setNeedsDisplay() | 
| 99 | 101 | ||
| 102 | + dateArr.append(point) | ||
| 103 | + | ||
| 100 | } | 104 | } | 
| 101 | - //MARK: - 画竖线 | 105 | + //MARK: - 点击横坐标 | 
| 102 | @objc func touchXAction(tap:UIButton){ | 106 | @objc func touchXAction(tap:UIButton){ | 
| 103 | 107 | ||
| 104 | let index = tap.tag | 108 | let index = tap.tag | 
| 105 | - | 109 | + | 
| 110 | + | ||
| 106 | if btnTouch != index { | 111 | if btnTouch != index { | 
| 107 | 112 | ||
| 113 | + //竖虚线 | ||
| 108 | lineLayer.strokeColor = UIColorWithRGB(R: 92, G: 145, B: 230).cgColor | 114 | lineLayer.strokeColor = UIColorWithRGB(R: 92, G: 145, B: 230).cgColor | 
| 109 | lineLayer.lineWidth = 1 | 115 | lineLayer.lineWidth = 1 | 
| 110 | let arr = [5,5] | 116 | let arr = [5,5] | 
| @@ -112,6 +118,7 @@ class LineChartView: UIView { | @@ -112,6 +118,7 @@ class LineChartView: UIView { | ||
| 112 | lineLayer.lineDashPattern = arr as [NSNumber] | 118 | lineLayer.lineDashPattern = arr as [NSNumber] | 
| 113 | self.layer.addSublayer(lineLayer) | 119 | self.layer.addSublayer(lineLayer) | 
| 114 | 120 | ||
| 121 | + //X轴下划线 | ||
| 115 | let movePath = CGMutablePath() | 122 | let movePath = CGMutablePath() | 
| 116 | movePath.move(to: CGPoint.init(x: xCenterArr[index], y: yCenterArr[0])) | 123 | movePath.move(to: CGPoint.init(x: xCenterArr[index], y: yCenterArr[0])) | 
| 117 | movePath.addLine(to: CGPoint.init(x: xCenterArr[index], y: 10+5*(self.bounds.height-60)/CGFloat(yCount))) | 124 | movePath.addLine(to: CGPoint.init(x: xCenterArr[index], y: 10+5*(self.bounds.height-60)/CGFloat(yCount))) | 
| @@ -121,17 +128,40 @@ class LineChartView: UIView { | @@ -121,17 +128,40 @@ class LineChartView: UIView { | ||
| 121 | xMoveLayer.lineWidth = 2 | 128 | xMoveLayer.lineWidth = 2 | 
| 122 | self.layer.addSublayer(xMoveLayer) | 129 | self.layer.addSublayer(xMoveLayer) | 
| 123 | let movePath2 = CGMutablePath() | 130 | let movePath2 = CGMutablePath() | 
| 124 | - movePath2.move(to: CGPoint.init(x: xCenterArr[index]-15, y: self.bounds.height-50)) | ||
| 125 | - movePath2.addLine(to: CGPoint.init(x: xCenterArr[index]+15, y: self.bounds.height-50)) | 131 | + movePath2.move(to: CGPoint.init(x: xCenterArr[index]-15, y: self.bounds.height-45)) | 
| 132 | + movePath2.addLine(to: CGPoint.init(x: xCenterArr[index]+15, y: self.bounds.height-45)) | ||
| 126 | xMoveLayer.path = movePath2 | 133 | xMoveLayer.path = movePath2 | 
| 127 | 134 | ||
| 135 | + if yLabArray.count == dateArr.count { | ||
| 136 | + for label in yLabArray { | ||
| 137 | + label.removeFromSuperview() | ||
| 138 | + } | ||
| 139 | + } | ||
| 140 | + yLabArray.removeAll() | ||
| 141 | + //数字显示 | ||
| 142 | + for i in 0..<dateArr.count { | ||
| 143 | + let arr = dateArr[i] | ||
| 144 | + //y坐标数值 | ||
| 145 | + let y = arr[index] | ||
| 146 | + let yLab = yMoveLaArr[GPA-y] | ||
| 147 | + var rect = yLab.frame | ||
| 148 | + rect.origin.x = xCenterArr[index]+10 | ||
| 149 | + | ||
| 150 | + let newLab = UILabel() | ||
| 151 | + newLab.text = yLab.text | ||
| 152 | + newLab.textColor = yLab.textColor | ||
| 153 | + newLab.font = yLab.font | ||
| 154 | + newLab.frame = rect | ||
| 155 | + self.addSubview(newLab) | ||
| 156 | + yLabArray.append(newLab) | ||
| 157 | + } | ||
| 158 | + }else{ | ||
| 159 | + print(yLabArray) | ||
| 128 | } | 160 | } | 
| 129 | btnTouch = index | 161 | btnTouch = index | 
| 130 | 162 | ||
| 131 | 163 | ||
| 132 | } | 164 | } | 
| 133 | - //MARK: - 特定横坐标颜色,下划线 | ||
| 134 | - | ||
| 135 | //转换y坐标 | 165 | //转换y坐标 | 
| 136 | func swichPoint(arr:Array<Int>)->Array<Int>{ | 166 | func swichPoint(arr:Array<Int>)->Array<Int>{ | 
| 137 | var array = Array<Int>() | 167 | var array = Array<Int>() |