IHBarChartView.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. //
  2. // IHBarChartView.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 JXSegmentedView
  10. import Charts
  11. protocol IHBarChartViewDelegate : NSObjectProtocol{
  12. //切换数据类型
  13. func exchangeBarChartDataType(_ index:Int)
  14. }
  15. class IHBarChartView: UIView {
  16. weak var delegate : IHBarChartViewDelegate?
  17. var reportData: ReportData?{
  18. didSet{
  19. if let data = self.reportData {
  20. setBarChartData(data)
  21. }
  22. }
  23. }
  24. var purifierReportData: PurifierReportData?{
  25. didSet{
  26. if let data = self.purifierReportData {
  27. setBarChartData_purifier(data)
  28. }
  29. }
  30. }
  31. lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
  32. return JXSegmentedTitleDataSource()
  33. }()
  34. let segmentedView = JXSegmentedView()
  35. lazy var chartView: BarChartView = {
  36. let chartView = BarChartView()
  37. return chartView
  38. }()
  39. var mTitle : String?{
  40. didSet{
  41. titleLabel.text = self.mTitle
  42. }
  43. }
  44. private lazy var titleLabel: UILabel = {
  45. let titleLabel = UILabel()
  46. titleLabel.text = "Electricity consumption"
  47. titleLabel.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 13)
  48. titleLabel.textColor = UIColor(hexString: "#333333")
  49. titleLabel.textAlignment = .center
  50. return titleLabel
  51. }()
  52. override init(frame: CGRect) {
  53. super.init(frame: frame)
  54. // backgroundColor = .brown
  55. addSubview(titleLabel)
  56. configSegmentView() //设置选择器
  57. configChartView() //设置柱状图
  58. }
  59. required init?(coder: NSCoder) {
  60. super.init(coder: coder)
  61. addSubview(titleLabel)
  62. configSegmentView() //设置选择器
  63. configChartView() //设置柱状图
  64. // fatalError("init(coder:) has not been implemented")
  65. }
  66. //空气净化器数据 历史记录取 workTimeList
  67. func setBarChartData_purifier(_ report:PurifierReportData?){
  68. if report?.workTimeList == nil || report?.workTimeList?.count == 0 {
  69. return
  70. }
  71. var dataEntries = [BarChartDataEntry]()
  72. for (index, val) in (report?.workTimeList?.enumerated())! {
  73. let value = Double(val)
  74. let entry = BarChartDataEntry(x: Double(index), y: value!)
  75. dataEntries.append(entry)
  76. }
  77. //这10条数据作为柱状图的所有数据
  78. let chartDataSet = BarChartDataSet(entries: dataEntries, label: "")
  79. chartDataSet.barShadowColor = UIColor(hexString: "#EBF2ED")!
  80. chartDataSet.drawValuesEnabled = false
  81. // #05CFAB
  82. chartDataSet.colors = [UIColor(hexString: "#573F95")!]
  83. //#05CFAB
  84. chartDataSet.highlightColor = UIColor(hexString: "#573F95")!//高梁颜色
  85. //目前只有一组柱状图
  86. let chartData = BarChartData(dataSets: [chartDataSet])
  87. //设置柱子宽度
  88. let xVals = report?.timeList!
  89. chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xVals!)
  90. chartData.barWidth = 0.2
  91. //设置柱状图数据
  92. chartView.data = chartData
  93. chartView.animate(yAxisDuration: 2)
  94. // #05CFAB
  95. let marker = XYMarkerView(color: UIColor(hexString: "#573F95")!, font: .systemFont(ofSize: 12), textColor: .white, insets: UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5), xAxisValueFormatter: IndexAxisValueFormatter(values: xVals!))
  96. marker.chartView = chartView
  97. marker.minimumSize = CGSize(width: 80, height: 20)
  98. marker.isBarchart = true
  99. marker.chartView = chartView
  100. chartView.marker = marker
  101. }
  102. func setBarChartData(_ report:ReportData?){
  103. if report?.dataList == nil || report?.dataList?.count == 0 {
  104. return
  105. }
  106. var dataEntries = [BarChartDataEntry]()
  107. for (index, val) in (report?.dataList?.enumerated())! {
  108. let value = Double(val)
  109. let entry = BarChartDataEntry(x: Double(index), y: value!)
  110. dataEntries.append(entry)
  111. }
  112. //这10条数据作为柱状图的所有数据
  113. let chartDataSet = BarChartDataSet(entries: dataEntries, label: "")
  114. chartDataSet.barShadowColor = UIColor(hexString: "#EBF2ED")!
  115. chartDataSet.drawValuesEnabled = false
  116. // #05CFAB
  117. chartDataSet.colors = [UIColor(hexString: "#573F95")!]
  118. // #05CFAB
  119. chartDataSet.highlightColor = UIColor(hexString: "#573F95")!//高梁颜色
  120. //目前只有一组柱状图
  121. let chartData = BarChartData(dataSets: [chartDataSet])
  122. //设置柱子宽度
  123. let xVals = report?.timeList!
  124. chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xVals!)
  125. chartData.barWidth = 0.2
  126. //设置柱状图数据
  127. chartView.data = chartData
  128. chartView.animate(yAxisDuration: 2)
  129. // #05CFAB
  130. let marker = XYMarkerView(color: UIColor(hexString: "#573F95")!, font: .systemFont(ofSize: 12), textColor: .white, insets: UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5), xAxisValueFormatter: IndexAxisValueFormatter(values: xVals!))
  131. marker.chartView = chartView
  132. marker.minimumSize = CGSize(width: 80, height: 20)
  133. marker.isBarchart = true
  134. marker.chartView = chartView
  135. chartView.marker = marker
  136. }
  137. func configChartView() {
  138. addSubview(chartView)
  139. //设置间隙
  140. chartView.setExtraOffsets(left: 0 , top: 0, right: 0, bottom: 0)
  141. //代理方法
  142. chartView.delegate = self
  143. chartView.chartDescription?.enabled = false
  144. chartView.scaleYEnabled = false
  145. chartView.doubleTapToZoomEnabled = false
  146. chartView.pinchZoomEnabled = false
  147. //s只滑动不滚动(系统gneiss默认先缩放后滑动)以及初始化时x轴就缩放1.5倍,就可以滑动了
  148. let scaleX = chartView.viewPortHandler
  149. scaleX?.setMinimumScaleX(1.5)
  150. chartView.xAxis.drawGridLinesEnabled = false
  151. // 获取X轴
  152. let leftAxis = chartView.leftAxis
  153. leftAxis.enabled = false //绘制右边轴
  154. leftAxis.labelTextColor = UIColor.clear
  155. leftAxis.axisLineColor = .clear
  156. leftAxis.axisMinimum = 0 //最小值从0开始
  157. chartView.rightAxis.enabled = false
  158. chartView.legend.enabled = false //不需要显示图例
  159. chartView.drawBordersEnabled = false
  160. // 删除x轴线和y轴:
  161. chartView.xAxis.drawAxisLineEnabled = false
  162. chartView.leftAxis.drawAxisLineEnabled = false
  163. //bottom Axis
  164. let xAxis = chartView.xAxis
  165. xAxis.labelCount = 4
  166. chartView.xAxis.enabled = true
  167. xAxis.labelPosition = .bottom //位置
  168. xAxis.labelFont = UIFont(name: Alibaba_PuHuiTi_Regular, size: 11)!
  169. xAxis.labelTextColor = UIColor(hexString: "#949DB0")
  170. chartView.animate(yAxisDuration: 2)
  171. }
  172. func configSegmentView() {
  173. //segmentedViewDataSource一定要通过属性强持有!!!!!!!!!
  174. segmentedDataSource.isTitleColorGradientEnabled = true
  175. segmentedDataSource.titles = ["当天","本周","本月","本年"]
  176. segmentedDataSource.titleSelectedColor = UIColor(hexString: "#573F95")!
  177. segmentedDataSource.titleNormalColor = UIColor(hexString: "#657085")!
  178. segmentedDataSource.titleNormalFont = UIFont(name: Alibaba_PuHuiTi_Regular, size: 13)!
  179. segmentedDataSource.titleSelectedFont = UIFont(name: Alibaba_PuHuiTi_Bold, size: 13)!
  180. segmentedDataSource.reloadData(selectedIndex: 0)
  181. segmentedView.dataSource = segmentedDataSource
  182. //segmentedDataSource = dataSource
  183. //配置指示器
  184. let indicator = JXSegmentedIndicatorTriangleView()
  185. // #05CFAB
  186. indicator.indicatorColor = UIColor(hexString: "#573F95")!
  187. indicator.indicatorWidth = 4
  188. indicator.indicatorHeight = 2.5
  189. indicator.indicatorPosition = .top
  190. segmentedView.indicators = [indicator]
  191. segmentedView.delegate = self
  192. addSubview(segmentedView)
  193. }
  194. override func layoutSubviews() {
  195. super.layoutSubviews()
  196. titleLabel.snp.makeConstraints { (make) in
  197. make.centerX.equalToSuperview()
  198. make.top.equalToSuperview().offset(15)
  199. }
  200. segmentedView.frame = CGRect(x: 0, y: self.bounds.size.height - 30, width: self.bounds.size.width , height: 20)
  201. chartView.snp.makeConstraints { (make) in
  202. make.top.equalTo(titleLabel.snp.bottom).offset(15.5)
  203. make.left.equalToSuperview().offset(28)
  204. make.right.equalTo(-28)
  205. make.bottom.equalTo(segmentedView.snp.top).offset(-13.5)
  206. }
  207. }
  208. }
  209. extension IHBarChartView :JXSegmentedViewDelegate{
  210. func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
  211. let marker = chartView.marker as! XYMarkerView
  212. marker.isSeleted = false //切换的时候 去掉选择
  213. if let delegate = self.delegate{
  214. delegate.exchangeBarChartDataType(index)
  215. }
  216. // chartView.animate(yAxisDuration: 2)
  217. }
  218. }
  219. extension IHBarChartView:ChartViewDelegate{
  220. func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
  221. let marker = chartView.marker as! XYMarkerView
  222. marker.isSeleted = true
  223. }
  224. func chartValueNothingSelected(_ chartView: ChartViewBase) {
  225. let marker = chartView.marker as! XYMarkerView
  226. marker.isSeleted = false
  227. }
  228. }