LineChartView.swift 7.1 KB
//
//  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()
    }
    
}