123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- //
- // 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..<values.count {
- // chartDataSet.circleColors.append(.clear)
- // }
- //
- // log.debug("chart.xAxis \(chartView.xAxis )")
- //
- //
- // //选中颜色
- // chartDataSet.circleColors[index! ] = UIColor(hexString: "#05CFAB")!
- //
- //
- //
- // //重新渲染表格
- // chartView.data?.notifyDataChanged()
- // chartView.notifyDataSetChanged()
-
- var chartDataSet = LineChartDataSet()
- chartDataSet = (chartView.data?.dataSets[0] as? LineChartDataSet)!
- let values = chartDataSet.entries
-
- let index = values.index(where: {$0.x == highlight.x}) //获取索引
- if let delegate = self.delegate {
- delegate.lineChartDidSelected(index ?? 0)
- }
-
- let marker = chartView.marker as! XYMarkerView
- marker.isSeleted = true
-
-
- }
-
-
- // Called when nothing has been selected or an "un-select" has been made.
- func chartValueNothingSelected(_ chartView: ChartViewBase){
- log.debug("取消选中数据")
- let marker = chartView.marker as! XYMarkerView
- marker.isSeleted = false
- if let delegate = self.delegate {
- delegate.lineChartDeselected()
- }
- //还原选中数据
- // var chartDataSet = LineChartDataSet()
- // chartDataSet = (chartView.data?.dataSets[0] as? LineChartDataSet)!
- // let values = chartDataSet.entries
- // chartDataSet.circleColors.removeAll()
- // for i in 0..<values.count {
- // chartDataSet.circleColors.append(.clear)
- // }
- // let bmxAxis = chartView.xAxis
- //
- //
- //
- // // bmxAxis.labelTextColor = UIColor.black
- // //重新渲染表格
- // chartView.data?.notifyDataChanged()
- // chartView.notifyDataSetChanged()
- }
-
- /// Called when a user stops panning between values on the chart
- func chartViewDidEndPanning(_ chartView: ChartViewBase){
- log.debug("横向滑动")
- }
-
-
- // Callbacks when the chart is scaled / zoomed via pinch zoom gesture.
- func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat){
- log.debug("缩放 -- scaleX - \(scaleX) scaleY - \(scaleY)")
- }
-
- // Callbacks when the chart is moved / translated via drag gesture.
- func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat){
- log.debug("移动 -- dX - \(dX) dY - \(dY)")
- }
-
- }
- extension IHLineChartView :DYScrollRulerDelegate{
- func dyScrollRulerViewValueChange(rulerView: DYScrollRulerView, value: Float) {
- // log.debug(value)
- }
-
-
- }
|