BloodPressureCell.swift 3.32 KB
//
//  BloodPressureCell.swift
//  Twear
//
//  Created by yangbin on 2021/11/16.
//

import UIKit
import Charts

class BloodPressureCell: UICollectionViewCell {

    @IBOutlet weak var bpValueLabel: UILabel!
    @IBOutlet weak var lineChartView: LineChartView!
    @IBOutlet weak var noDataImageView: UIImageView!
    @IBOutlet weak var dateLabel: UILabel!
    
    var bpHistory: [BloodPressureModel] = [] {
        didSet {
            let count = bpHistory.count
            if count == 0 {
                bpValueLabel.text = "--/--"
                dateLabel.text = LocString("暂无数据")
                noDataImageView.isHidden = false
                lineChartView.isHidden = true
            } else {
                bpValueLabel.text = "\(bpHistory[count-1].sbp)/\(bpHistory[count-1].dbp)"
                dateLabel.text = bpHistory[count-1].date!.toString(.custom("yyyy/MM/dd"))
                updateChartView(bpHistory)
                noDataImageView.isHidden = true
                lineChartView.isHidden = false
            }
        }
    }
    
    private func updateChartView(_ bpHistory: [BloodPressureModel]) {
        setupChartView()
        var dbpDataEntries = [ChartDataEntry]()
        for (i, bp) in bpHistory.enumerated() {
            dbpDataEntries.append(ChartDataEntry(x: Double(i), y: Double(bp.dbp)))
        }
        if dbpDataEntries.count == 1 {
            dbpDataEntries.append(ChartDataEntry(x: dbpDataEntries[0].x+0.5, y: dbpDataEntries[0].y))
        }
        let dbpDataSet = LineChartDataSet(entries: dbpDataEntries, label: nil)
        dbpDataSet.drawCirclesEnabled = false
        dbpDataSet.drawValuesEnabled = false
        dbpDataSet.mode = .horizontalBezier
        dbpDataSet.setDrawHighlightIndicators(false)
        dbpDataSet.setColor(UIColor.rgbColorFromHex(0x00DEFF))
        dbpDataSet.lineWidth = 1
        
        var sbpDataEntries = [ChartDataEntry]()
        for (i, bp) in bpHistory.enumerated() {
            sbpDataEntries.append(ChartDataEntry(x: Double(i), y: Double(bp.sbp)))
        }
        if sbpDataEntries.count == 1 {
            sbpDataEntries.append(ChartDataEntry(x: sbpDataEntries[0].x+0.5, y: sbpDataEntries[0].y))
        }
        let sbpDataSet = LineChartDataSet(entries: sbpDataEntries, label: nil)
        sbpDataSet.drawCirclesEnabled = false
        sbpDataSet.drawValuesEnabled = false
        sbpDataSet.mode = .horizontalBezier
        sbpDataSet.setDrawHighlightIndicators(false)
        sbpDataSet.setColor(UIColor.rgbColorFromHex(0xFF7800))
        sbpDataSet.lineWidth = 1
        lineChartView.data = LineChartData(dataSets: [dbpDataSet, sbpDataSet])
    }
    
    func setupChartView() {
        lineChartView.chartDescription?.enabled = false
        lineChartView.legend.enabled = false
        lineChartView.setScaleEnabled(false)
        lineChartView.rightAxis.enabled = false
        
        let leftAxis = lineChartView.leftAxis
        leftAxis.axisMinimum = 50 //设置左侧Y轴最小值
        leftAxis.axisMaximum = 140
        leftAxis.granularity = 10
        leftAxis.enabled = false
        
        let xAxis = lineChartView.xAxis
        xAxis.axisMinimum = 0
        xAxis.axisMaximum = 30
        xAxis.granularity = 1 //间隔
        xAxis.enabled = false
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

}