// // IHQualityChartsCell.swift // Inhealth // // Created by weclouds on 2020/3/10. // Copyright © 2020 weclouds. All rights reserved. // import UIKit import Charts class IHQualityChartsCell: UITableViewCell { var averageAir:String?{ didSet{ self.valueLabel.text = self.averageAir } } var yData :[Double]? = [Double]() var dataList :[String]?{ didSet{ if let data = self.dataList { var yData = [Double]() for str in data { let a = String(format: "%.1f", Double(str)!) let y = Double(a) yData.append(y!) } self.yData = yData updateChartData() } } } var timeList:[String]?{ didSet{ if let timeList = self.timeList { chartView!.xAxis.valueFormatter = IndexAxisValueFormatter(values: timeList) } } } @IBOutlet weak var qualityLabel: UILabel! @IBOutlet weak var valueLabel: UILabel! @IBOutlet weak var bgView: UIView! @IBOutlet weak var chartsBox: UIView! var chartView: LineChartView? override func awakeFromNib() { super.awakeFromNib() setupUI() } override func layoutSubviews() { super.layoutSubviews() } override func setSelected(_ selected: Bool, animated: Bool) { super.setSelected(selected, animated: animated) } } extension IHQualityChartsCell{ func setupUI() { //渲染背景图 renderBgView() //设置折线图 configChartView() } func renderBgView() { // fillCode let bgLayer1 = CAGradientLayer() bgLayer1.colors = [UIColor(red: 0.64, green: 0.98, blue: 0.92, alpha: 1).cgColor, UIColor(red: 0.18, green: 0.65, blue: 0.89, alpha: 1).cgColor] bgLayer1.locations = [0, 1] bgLayer1.frame = CGRect(x: 0, y: 0, width: KSCREENWIDTH - 40, height: 132) bgLayer1.startPoint = CGPoint(x: 0, y: 0) bgLayer1.endPoint = CGPoint(x: 0, y: 1) bgView.layer.insertSublayer(bgLayer1, at: 0)//移到底层 } func configChartView() { self.chartView = LineChartView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH - 40 , height: chartsBox.bounds.size.height)) chartsBox.addSubview(chartView!) //设置间隙 // chartView?.setExtraOffsets(left: 10, top: 10, right: 10, bottom: 20) //代理方法 chartView?.delegate = self chartView?.chartDescription?.enabled = false chartView?.scaleYEnabled = false chartView?.doubleTapToZoomEnabled = false chartView?.dragEnabled = false chartView?.setScaleEnabled(false) chartView?.pinchZoomEnabled = false chartView?.xAxis.drawGridLinesEnabled = false chartView?.noDataTextColor = UIColor.white let marker = IHBalloonMarker(color: .clear, font: .systemFont(ofSize: 12), textColor: .white, insets: UIEdgeInsets(top: 8, left: 8, bottom: 20, right: 8)) marker.chartView = chartView marker.minimumSize = CGSize(width: 80, height: 40) chartView?.marker = marker let leftAxis = chartView?.leftAxis leftAxis?.enabled = false //绘制右边轴 leftAxis?.labelTextColor = UIColor.clear leftAxis?.axisLineColor = .clear leftAxis?.gridColor = UIColor.clear // leftAxis?.gridLineDashLengths = [2, 1] leftAxis?.drawLimitLinesBehindDataEnabled = false chartView?.rightAxis.enabled = false chartView?.legend.form = .line //bottom Axis let bmxAxis = chartView?.xAxis bmxAxis?.enabled = true bmxAxis?.labelPosition = .bottom bmxAxis!.labelCount = 7 bmxAxis?.labelTextColor = UIColor.white bmxAxis?.forceLabelsEnabled = true bmxAxis?.avoidFirstLastClippingEnabled = true bmxAxis?.drawAxisLineEnabled = true bmxAxis?.gridColor = UIColor(hexString: "#FFFFFF")! bmxAxis?.gridLineDashLengths = [2, 1] bmxAxis?.drawLimitLinesBehindDataEnabled = false bmxAxis?.axisLineColor = UIColor.clear chartView?.animate(xAxisDuration: 2) } func updateChartData() { self.setDataCount() } func setDataCount() { var values = [ChartDataEntry]() for i in 0..<(yData?.count)! { let yval = yData![i] let entry = ChartDataEntry(x: Double(i), y: yval) values.append(entry) } let set1 = LineChartDataSet(entries: values, label: "") set1.drawIconsEnabled = false // set1.lineDashLengths = [5, 2.5] // set1.highlightLineDashLengths = [5, 2.5] set1.setColor(.white) set1.lineWidth = 1 set1.circleRadius = 4 set1.circleColors = [UIColor(hexString: "#FFFFFF")!] set1.drawCircleHoleEnabled = true set1.circleHoleRadius = 2 set1.circleHoleColor = .clear set1.valueFont = .systemFont(ofSize: 9) set1.formLineDashLengths = [5, 2.5] set1.formLineWidth = 1 set1.formSize = 15 set1.mode = .cubicBezier set1.drawCirclesEnabled = false set1.drawValuesEnabled = false set1.highlightEnabled = true //不启用十字线 set1.highlightColor = UIColor.clear let gradientColors = [ChartColorTemplates.colorFromString("#00ff0000").cgColor, ChartColorTemplates.colorFromString("#ffff0000").cgColor] let gradient = CGGradient(colorsSpace: nil, colors: gradientColors as CFArray, locations: nil)! set1.fillAlpha = 1 set1.fill = Fill(linearGradient: gradient, angle: 90) set1.drawFilledEnabled = false let data = LineChartData(dataSet: set1) chartView!.data = data chartView?.animate(xAxisDuration: 2) } } extension IHQualityChartsCell: ChartViewDelegate{ func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) { chartView.setNeedsDisplay() print("选中了一个数据 - ") var chartDataSet = LineChartDataSet() chartDataSet = (chartView.data?.dataSets[0] as? LineChartDataSet)! chartDataSet.drawCirclesEnabled = true let values = chartDataSet.entries print("选中了一个数据 - \(entry) ") chartDataSet.circleColors.removeAll() let index = values.index(where: {$0.x == highlight.x}) //获取索引 for i in 0..