BloodPressureCell.swift
3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//
// 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
}
}