123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266 |
- //
- // IHBarChartView.swift
- // Inhealth
- //
- // Created by weclouds on 2019/12/16.
- // Copyright © 2019 weclouds. All rights reserved.
- //
- import UIKit
- import JXSegmentedView
- import Charts
- protocol IHBarChartViewDelegate : NSObjectProtocol{
- //切换数据类型
-
- func exchangeBarChartDataType(_ index:Int)
- }
- class IHBarChartView: UIView {
- weak var delegate : IHBarChartViewDelegate?
- var reportData: ReportData?{
- didSet{
- if let data = self.reportData {
- setBarChartData(data)
- }
-
- }
- }
-
- var purifierReportData: PurifierReportData?{
- didSet{
- if let data = self.purifierReportData {
- setBarChartData_purifier(data)
- }
-
- }
- }
- lazy var segmentedDataSource: JXSegmentedTitleDataSource = {
- return JXSegmentedTitleDataSource()
- }()
- let segmentedView = JXSegmentedView()
-
- lazy var chartView: BarChartView = {
- let chartView = BarChartView()
- return chartView
- }()
-
- var mTitle : String?{
- didSet{
- titleLabel.text = self.mTitle
- }
- }
-
- private lazy var titleLabel: UILabel = {
- let titleLabel = UILabel()
- titleLabel.text = "Electricity consumption"
- titleLabel.font = UIFont(name: Alibaba_PuHuiTi_Medium, size: 13)
- titleLabel.textColor = UIColor(hexString: "#333333")
- titleLabel.textAlignment = .center
- return titleLabel
- }()
-
- override init(frame: CGRect) {
- super.init(frame: frame)
- // backgroundColor = .brown
- addSubview(titleLabel)
- configSegmentView() //设置选择器
- configChartView() //设置柱状图
- }
-
- required init?(coder: NSCoder) {
- super.init(coder: coder)
- addSubview(titleLabel)
- configSegmentView() //设置选择器
- configChartView() //设置柱状图
- // fatalError("init(coder:) has not been implemented")
- }
-
- //空气净化器数据 历史记录取 workTimeList
- func setBarChartData_purifier(_ report:PurifierReportData?){
- if report?.workTimeList == nil || report?.workTimeList?.count == 0 {
- return
- }
- var dataEntries = [BarChartDataEntry]()
-
- for (index, val) in (report?.workTimeList?.enumerated())! {
- let value = Double(val)
- let entry = BarChartDataEntry(x: Double(index), y: value!)
- dataEntries.append(entry)
- }
- //这10条数据作为柱状图的所有数据
- let chartDataSet = BarChartDataSet(entries: dataEntries, label: "")
- chartDataSet.barShadowColor = UIColor(hexString: "#EBF2ED")!
- chartDataSet.drawValuesEnabled = false
- // #05CFAB
- chartDataSet.colors = [UIColor(hexString: "#573F95")!]
- //#05CFAB
- chartDataSet.highlightColor = UIColor(hexString: "#573F95")!//高梁颜色
- //目前只有一组柱状图
- let chartData = BarChartData(dataSets: [chartDataSet])
- //设置柱子宽度
- let xVals = report?.timeList!
- chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xVals!)
- chartData.barWidth = 0.2
- //设置柱状图数据
- chartView.data = chartData
- chartView.animate(yAxisDuration: 2)
- // #05CFAB
- 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!))
- marker.chartView = chartView
- marker.minimumSize = CGSize(width: 80, height: 20)
- marker.isBarchart = true
- marker.chartView = chartView
- chartView.marker = marker
- }
-
- func setBarChartData(_ report:ReportData?){
- if report?.dataList == nil || report?.dataList?.count == 0 {
- return
- }
- var dataEntries = [BarChartDataEntry]()
-
- for (index, val) in (report?.dataList?.enumerated())! {
- let value = Double(val)
- let entry = BarChartDataEntry(x: Double(index), y: value!)
- dataEntries.append(entry)
- }
- //这10条数据作为柱状图的所有数据
- let chartDataSet = BarChartDataSet(entries: dataEntries, label: "")
- chartDataSet.barShadowColor = UIColor(hexString: "#EBF2ED")!
- chartDataSet.drawValuesEnabled = false
- // #05CFAB
- chartDataSet.colors = [UIColor(hexString: "#573F95")!]
- // #05CFAB
- chartDataSet.highlightColor = UIColor(hexString: "#573F95")!//高梁颜色
- //目前只有一组柱状图
- let chartData = BarChartData(dataSets: [chartDataSet])
- //设置柱子宽度
- let xVals = report?.timeList!
- chartView.xAxis.valueFormatter = IndexAxisValueFormatter(values: xVals!)
- chartData.barWidth = 0.2
- //设置柱状图数据
- chartView.data = chartData
- chartView.animate(yAxisDuration: 2)
- // #05CFAB
- 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!))
- marker.chartView = chartView
- marker.minimumSize = CGSize(width: 80, height: 20)
- marker.isBarchart = true
- marker.chartView = chartView
- chartView.marker = marker
- }
-
- func configChartView() {
- addSubview(chartView)
- //设置间隙
- chartView.setExtraOffsets(left: 0 , top: 0, right: 0, bottom: 0)
- //代理方法
- 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
- // 获取X轴
- let leftAxis = chartView.leftAxis
- leftAxis.enabled = false //绘制右边轴
- leftAxis.labelTextColor = UIColor.clear
- leftAxis.axisLineColor = .clear
- leftAxis.axisMinimum = 0 //最小值从0开始
-
- chartView.rightAxis.enabled = false
- chartView.legend.enabled = false //不需要显示图例
-
- chartView.drawBordersEnabled = false
-
- // 删除x轴线和y轴:
- chartView.xAxis.drawAxisLineEnabled = false
- chartView.leftAxis.drawAxisLineEnabled = false
- //bottom Axis
- let xAxis = chartView.xAxis
- xAxis.labelCount = 4
- chartView.xAxis.enabled = true
- xAxis.labelPosition = .bottom //位置
- xAxis.labelFont = UIFont(name: Alibaba_PuHuiTi_Regular, size: 11)!
- xAxis.labelTextColor = UIColor(hexString: "#949DB0")
- chartView.animate(yAxisDuration: 2)
-
- }
-
- func configSegmentView() {
- //segmentedViewDataSource一定要通过属性强持有!!!!!!!!!
- segmentedDataSource.isTitleColorGradientEnabled = true
- segmentedDataSource.titles = ["当天","本周","本月","本年"]
- segmentedDataSource.titleSelectedColor = UIColor(hexString: "#573F95")!
- segmentedDataSource.titleNormalColor = UIColor(hexString: "#657085")!
- segmentedDataSource.titleNormalFont = UIFont(name: Alibaba_PuHuiTi_Regular, size: 13)!
- segmentedDataSource.titleSelectedFont = UIFont(name: Alibaba_PuHuiTi_Bold, size: 13)!
- segmentedDataSource.reloadData(selectedIndex: 0)
- segmentedView.dataSource = segmentedDataSource
-
- //segmentedDataSource = dataSource
- //配置指示器
- let indicator = JXSegmentedIndicatorTriangleView()
- // #05CFAB
- indicator.indicatorColor = UIColor(hexString: "#573F95")!
- indicator.indicatorWidth = 4
- indicator.indicatorHeight = 2.5
- indicator.indicatorPosition = .top
- segmentedView.indicators = [indicator]
- segmentedView.delegate = self
- addSubview(segmentedView)
-
- }
-
- override func layoutSubviews() {
- super.layoutSubviews()
- titleLabel.snp.makeConstraints { (make) in
- make.centerX.equalToSuperview()
- make.top.equalToSuperview().offset(15)
- }
- segmentedView.frame = CGRect(x: 0, y: self.bounds.size.height - 30, width: self.bounds.size.width , height: 20)
-
- chartView.snp.makeConstraints { (make) in
- make.top.equalTo(titleLabel.snp.bottom).offset(15.5)
- make.left.equalToSuperview().offset(28)
- make.right.equalTo(-28)
- make.bottom.equalTo(segmentedView.snp.top).offset(-13.5)
- }
-
- }
-
- }
- extension IHBarChartView :JXSegmentedViewDelegate{
- func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
- let marker = chartView.marker as! XYMarkerView
- marker.isSeleted = false //切换的时候 去掉选择
-
- if let delegate = self.delegate{
- delegate.exchangeBarChartDataType(index)
- }
- // chartView.animate(yAxisDuration: 2)
- }
-
- }
- extension IHBarChartView:ChartViewDelegate{
- func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
- let marker = chartView.marker as! XYMarkerView
- marker.isSeleted = true
- }
- func chartValueNothingSelected(_ chartView: ChartViewBase) {
- let marker = chartView.marker as! XYMarkerView
- marker.isSeleted = false
- }
- }
|