IHLineChartView.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. //
  2. // IHLineChartView.swift
  3. // Inhealth
  4. //
  5. // Created by weclouds on 2019/12/16.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. import Charts
  10. protocol IHLineChartViewDelegate : NSObjectProtocol{
  11. func lineChartDidSelected(_ index:Int) //选择
  12. func lineChartDeselected() //取消选择
  13. }
  14. class IHLineChartView: UIView {
  15. weak var delegate :IHLineChartViewDelegate?
  16. var data:[String]? {
  17. didSet{
  18. if let data = self.data {
  19. var yData = [Double]()
  20. for str in data {
  21. let a = String(format: "%.1f", Double(str)!)
  22. let y = Double(a)
  23. yData.append(y!)
  24. }
  25. rawDatas = [YRawData(yData: yData)]
  26. self.setDataCount()
  27. }
  28. }
  29. }
  30. var xVals:[String]? = [String]()
  31. var rawDatas :[YRawData]? = [YRawData]()
  32. var avarageValue:String?{
  33. didSet{
  34. if let avarage = self.avarageValue {
  35. limitline.label = avarage
  36. limitline.limit = Double(avarage)!
  37. }
  38. }
  39. }
  40. var unit:String?{
  41. didSet{
  42. if let unit = self.unit {
  43. unitLabel.text = unit
  44. }
  45. }
  46. }
  47. lazy var lazyTimeRullerView:DYScrollRulerView = { [unowned self] in
  48. let unitStr = ""
  49. let rulersHeight = DYScrollRulerView.rulerViewHeight
  50. print(rulersHeight)
  51. 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)
  52. timerView.setDefaultValueAndAnimated(defaultValue: 11, animated: true)
  53. timerView.backgroundColor = .clear
  54. timerView.bgColor = UIColor.orange
  55. // #05CFAB
  56. timerView.triangleColor = UIColor(hexString: "#573F95")
  57. timerView.delegate = self
  58. timerView.scrollByHand = true
  59. return timerView
  60. }()
  61. lazy var unitLabel: UILabel = {
  62. let unitLabel = UILabel(frame: CGRect(x: 10, y: 0, width: 100, height: 15))
  63. unitLabel.text = "kWh"
  64. unitLabel.textColor = UIColor(hexString: "#A3AFBB")
  65. unitLabel.font = UIFont(name: Alibaba_PuHuiTi_Regular, size: 11)
  66. return unitLabel
  67. }()
  68. //限制线
  69. lazy var limitline: ChartLimitLine = {
  70. let limitline = ChartLimitLine(limit: 80, label: "80")
  71. limitline.lineWidth = 1.5
  72. // limitline.lineColor = UIColor(hexString: "#05CFAB")
  73. limitline.lineColor = UIColor.init(red: 197/255.0, green: 194/255.0, blue: 213/255.0, alpha: 1.0)
  74. limitline.lineDashLengths = [4,2]
  75. limitline.labelPosition = .topRight
  76. // limitline.valueTextColor = UIColor(hexString: "#05CFAB")
  77. limitline.valueTextColor = UIColor(hexString: "#573F95")
  78. limitline.valueFont = UIFont(name: Alibaba_PuHuiTi_Regular, size: 11)!
  79. return limitline
  80. }()
  81. var chartView: LineChartView?
  82. override init(frame: CGRect) {
  83. super.init(frame: frame)
  84. configChartView()
  85. addSubview(lazyTimeRullerView)
  86. }
  87. required init?(coder aDecoder: NSCoder) {
  88. fatalError("init(coder:) has not been implemented")
  89. }
  90. func configChartView() {
  91. self.chartView = LineChartView(frame: CGRect(x: 0, y: 0, width: KSCREENWIDTH , height: 240))
  92. chartView?.backgroundColor = .white
  93. addSubview(chartView!)
  94. chartView!.addSubview(unitLabel)
  95. //设置间隙
  96. chartView?.setExtraOffsets(left: 10, top: 10, right: 10, bottom: 20)
  97. //代理方法
  98. chartView?.delegate = self
  99. chartView?.chartDescription?.enabled = false
  100. chartView?.scaleYEnabled = false
  101. chartView?.doubleTapToZoomEnabled = false
  102. chartView?.pinchZoomEnabled = false
  103. //s只滑动不滚动(系统gneiss默认先缩放后滑动)以及初始化时x轴就缩放1.5倍,就可以滑动了
  104. let scaleX = chartView?.viewPortHandler
  105. scaleX?.setMinimumScaleX(1.5)
  106. chartView?.xAxis.drawGridLinesEnabled = false
  107. let leftAxis = chartView?.leftAxis
  108. leftAxis?.enabled = true //绘制右边轴
  109. leftAxis!.valueFormatter = LargeValueFormatter()
  110. leftAxis!.labelTextColor = UIColor(hexString: "#A3AFBB")
  111. leftAxis!.axisMinimum = 0 //最小值从0开始
  112. leftAxis!.spaceTop = 0.4
  113. leftAxis?.axisLineColor = .clear
  114. leftAxis!.drawGridLinesEnabled = true
  115. leftAxis?.gridColor = UIColor(hexString: "#C6CDD5")!
  116. leftAxis?.gridLineDashLengths = [4, 2]
  117. leftAxis!.gridLineWidth = 0.5 //x轴对应网格线的大小
  118. leftAxis?.drawLimitLinesBehindDataEnabled = true // //添加限制线
  119. leftAxis!.labelCount = 4
  120. leftAxis!.addLimitLine(limitline)//添加到U轴上
  121. chartView?.rightAxis.enabled = false
  122. chartView?.legend.enabled = false
  123. //bottom Axis
  124. let bmxAxis = chartView?.xAxis
  125. bmxAxis?.enabled = true
  126. bmxAxis?.labelTextColor = UIColor.black
  127. bmxAxis?.labelPosition = .bottom
  128. bmxAxis?.forceLabelsEnabled = true
  129. bmxAxis?.avoidFirstLastClippingEnabled = false
  130. bmxAxis?.drawAxisLineEnabled = true
  131. // bmxAxis?.gridColor = UIColor(hexString: "#E3ECF5")!
  132. bmxAxis?.gridColor = .red
  133. bmxAxis?.gridLineDashLengths = [2, 1]
  134. bmxAxis?.drawLimitLinesBehindDataEnabled = false
  135. bmxAxis?.labelCount = 4
  136. chartView?.animate(xAxisDuration: 2)
  137. // self.updateChartData()
  138. }
  139. func updateChartData() {
  140. var ydata = [Double]()
  141. for i in 0..<31 {
  142. let y = arc4random() % 100
  143. xVals?.append("\(i)")
  144. ydata.append(Double(y))
  145. }
  146. rawDatas = [YRawData(yData: ydata)]
  147. self.setDataCount()
  148. }
  149. fileprivate func setDataCount() {
  150. if xVals != nil {
  151. IHLineChartModel.share.gRealChartSetData(chartView!, xVals: xVals!, rawDatas: rawDatas!)
  152. chartView?.animate(xAxisDuration: 2)
  153. }
  154. }
  155. }
  156. extension IHLineChartView: ChartViewDelegate{
  157. func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
  158. // chartView.setNeedsDisplay()
  159. // print("选中了一个数据 ")
  160. //
  161. // var chartDataSet = LineChartDataSet()
  162. // chartDataSet = (chartView.data?.dataSets[0] as? LineChartDataSet)!
  163. // let values = chartDataSet.entries
  164. // chartDataSet.circleColors.removeAll()
  165. // let index = values.index(where: {$0.x == highlight.x}) //获取索引
  166. // for i in 0..<values.count {
  167. // chartDataSet.circleColors.append(.clear)
  168. // }
  169. //
  170. // log.debug("chart.xAxis \(chartView.xAxis )")
  171. //
  172. //
  173. // //选中颜色
  174. // chartDataSet.circleColors[index! ] = UIColor(hexString: "#05CFAB")!
  175. //
  176. //
  177. //
  178. // //重新渲染表格
  179. // chartView.data?.notifyDataChanged()
  180. // chartView.notifyDataSetChanged()
  181. var chartDataSet = LineChartDataSet()
  182. chartDataSet = (chartView.data?.dataSets[0] as? LineChartDataSet)!
  183. let values = chartDataSet.entries
  184. let index = values.index(where: {$0.x == highlight.x}) //获取索引
  185. if let delegate = self.delegate {
  186. delegate.lineChartDidSelected(index ?? 0)
  187. }
  188. let marker = chartView.marker as! XYMarkerView
  189. marker.isSeleted = true
  190. }
  191. // Called when nothing has been selected or an "un-select" has been made.
  192. func chartValueNothingSelected(_ chartView: ChartViewBase){
  193. log.debug("取消选中数据")
  194. let marker = chartView.marker as! XYMarkerView
  195. marker.isSeleted = false
  196. if let delegate = self.delegate {
  197. delegate.lineChartDeselected()
  198. }
  199. //还原选中数据
  200. // var chartDataSet = LineChartDataSet()
  201. // chartDataSet = (chartView.data?.dataSets[0] as? LineChartDataSet)!
  202. // let values = chartDataSet.entries
  203. // chartDataSet.circleColors.removeAll()
  204. // for i in 0..<values.count {
  205. // chartDataSet.circleColors.append(.clear)
  206. // }
  207. // let bmxAxis = chartView.xAxis
  208. //
  209. //
  210. //
  211. // // bmxAxis.labelTextColor = UIColor.black
  212. // //重新渲染表格
  213. // chartView.data?.notifyDataChanged()
  214. // chartView.notifyDataSetChanged()
  215. }
  216. /// Called when a user stops panning between values on the chart
  217. func chartViewDidEndPanning(_ chartView: ChartViewBase){
  218. log.debug("横向滑动")
  219. }
  220. // Callbacks when the chart is scaled / zoomed via pinch zoom gesture.
  221. func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat){
  222. log.debug("缩放 -- scaleX - \(scaleX) scaleY - \(scaleY)")
  223. }
  224. // Callbacks when the chart is moved / translated via drag gesture.
  225. func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat){
  226. log.debug("移动 -- dX - \(dX) dY - \(dY)")
  227. }
  228. }
  229. extension IHLineChartView :DYScrollRulerDelegate{
  230. func dyScrollRulerViewValueChange(rulerView: DYScrollRulerView, value: Float) {
  231. // log.debug(value)
  232. }
  233. }