// // IHLineChartView.swift // Inhealth // // Created by weclouds on 2019/12/16. // Copyright © 2019 weclouds. All rights reserved. // import UIKit import Charts protocol IHLineChartViewDelegate : NSObjectProtocol{ func lineChartDidSelected(_ index:Int) //选择 func lineChartDeselected() //取消选择 } class IHLineChartView: UIView { weak var delegate :IHLineChartViewDelegate? var data:[String]? { didSet{ if let data = self.data { var yData = [Double]() for str in data { let a = String(format: "%.1f", Double(str)!) let y = Double(a) yData.append(y!) } rawDatas = [YRawData(yData: yData)] self.setDataCount() } } } var xVals:[String]? = [String]() var rawDatas :[YRawData]? = [YRawData]() var avarageValue:String?{ didSet{ if let avarage = self.avarageValue { limitline.label = avarage limitline.limit = Double(avarage)! } } } var unit:String?{ didSet{ if let unit = self.unit { unitLabel.text = unit } } } lazy var lazyTimeRullerView:DYScrollRulerView = { [unowned self] in let unitStr = "" let rulersHeight = DYScrollRulerView.rulerViewHeight print(rulersHeight) var timerView = DYScrollRulerView.init(frame: CGRect.init(x: 0, y: 270 - 60, width: Int(KSCREENWIDTH), height: rulersHeight()), tminValue: 0, tmaxValue: 23, tstep: 0.2, tunit: unitStr, tNum: 5, viewcontroller:nil) timerView.setDefaultValueAndAnimated(defaultValue: 11, animated: true) timerView.backgroundColor = .clear timerView.bgColor = UIColor.orange // #05CFAB timerView.triangleColor = UIColor(hexString: "#573F95") timerView.delegate = self timerView.scrollByHand = true return timerView }() lazy var unitLabel: UILabel = { let unitLabel = UILabel(frame: CGRect(x: 10, y: 0, width: 100, height: 15)) unitLabel.text = "kWh" unitLabel.textColor = UIColor(hexString: "#A3AFBB") unitLabel.font = UIFont(name: Alibaba_PuHuiTi_Regular, size: 11) return unitLabel }() //限制线 lazy var limitline: ChartLimitLine = { let limitline = ChartLimitLine(limit: 80, label: "80") limitline.lineWidth = 1.5 // limitline.lineColor = UIColor(hexString: "#05CFAB") limitline.lineColor = UIColor.init(red: 197/255.0, green: 194/255.0, blue: 213/255.0, alpha: 1.0) limitline.lineDashLengths = [4,2] limitline.labelPosition = .topRight // limitline.valueTextColor = UIColor(hexString: "#05CFAB") limitline.valueTextColor = UIColor(hexString: "#573F95") limitline.valueFont = UIFont(name: Alibaba_PuHuiTi_Regular, size: 11)! return limitline }() var chartView: LineChartView? override init(frame: CGRect) { super.init(frame: frame) configChartView() addSubview(lazyTimeRullerView) } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } func configChartView() { self.chartView = LineChartView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH , height: 240)) chartView?.backgroundColor = .white addSubview(chartView!) chartView!.addSubview(unitLabel) //设置间隙 chartView?.setExtraOffsets(left: 10, top: 10, right: 10, bottom: 20) //代理方法 chartView?.delegate = self chartView?.chartDescription?.enabled = false chartView?.scaleYEnabled = false chartView?.doubleTapToZoomEnabled = false chartView?.pinchZoomEnabled = false //s只滑动不滚动(系统gneiss默认先缩放后滑动)以及初始化时x轴就缩放1.5倍,就可以滑动了 let scaleX = chartView?.viewPortHandler scaleX?.setMinimumScaleX(1.5) chartView?.xAxis.drawGridLinesEnabled = false let leftAxis = chartView?.leftAxis leftAxis?.enabled = true //绘制右边轴 leftAxis!.valueFormatter = LargeValueFormatter() leftAxis!.labelTextColor = UIColor(hexString: "#A3AFBB") leftAxis!.axisMinimum = 0 //最小值从0开始 leftAxis!.spaceTop = 0.4 leftAxis?.axisLineColor = .clear leftAxis!.drawGridLinesEnabled = true leftAxis?.gridColor = UIColor(hexString: "#C6CDD5")! leftAxis?.gridLineDashLengths = [4, 2] leftAxis!.gridLineWidth = 0.5 //x轴对应网格线的大小 leftAxis?.drawLimitLinesBehindDataEnabled = true // //添加限制线 leftAxis!.labelCount = 4 leftAxis!.addLimitLine(limitline)//添加到U轴上 chartView?.rightAxis.enabled = false chartView?.legend.enabled = false //bottom Axis let bmxAxis = chartView?.xAxis bmxAxis?.enabled = true bmxAxis?.labelTextColor = UIColor.black bmxAxis?.labelPosition = .bottom bmxAxis?.forceLabelsEnabled = true bmxAxis?.avoidFirstLastClippingEnabled = false bmxAxis?.drawAxisLineEnabled = true // bmxAxis?.gridColor = UIColor(hexString: "#E3ECF5")! bmxAxis?.gridColor = .red bmxAxis?.gridLineDashLengths = [2, 1] bmxAxis?.drawLimitLinesBehindDataEnabled = false bmxAxis?.labelCount = 4 chartView?.animate(xAxisDuration: 2) // self.updateChartData() } func updateChartData() { var ydata = [Double]() for i in 0..<31 { let y = arc4random() % 100 xVals?.append("\(i)") ydata.append(Double(y)) } rawDatas = [YRawData(yData: ydata)] self.setDataCount() } fileprivate func setDataCount() { if xVals != nil { IHLineChartModel.share.gRealChartSetData(chartView!, xVals: xVals!, rawDatas: rawDatas!) chartView?.animate(xAxisDuration: 2) } } } extension IHLineChartView: ChartViewDelegate{ func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) { // chartView.setNeedsDisplay() // print("选中了一个数据 ") // // var chartDataSet = LineChartDataSet() // chartDataSet = (chartView.data?.dataSets[0] as? LineChartDataSet)! // let values = chartDataSet.entries // chartDataSet.circleColors.removeAll() // let index = values.index(where: {$0.x == highlight.x}) //获取索引 // for i in 0..