| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- //
- // FilteredLoopsView.swift
- // FreeAPS
- //
- // Created by Jon Mårtensson on 2023-05-29.
- //
- import Charts
- import CoreData
- import SwiftDate
- import SwiftUI
- struct ChartsView: View {
- @FetchRequest var fetchRequest: FetchedResults<Readings>
- @Binding var highLimit: Decimal
- @Binding var lowLimit: Decimal
- @Binding var units: GlucoseUnits
- @Binding var overrideUnit: Bool
- @Binding var standing: Bool
- @State var headline: Color = .secondary
- private let conversionFactor = 0.0555
- var body: some View {
- glucoseChart
- Rectangle().fill(.cyan.opacity(0.2)).frame(maxHeight: 3)
- if standing {
- VStack {
- tirChart
- Rectangle().fill(.cyan.opacity(0.2)).frame(maxHeight: 3)
- groupedGlucoseStatsLaying
- }
- } else {
- HStack(spacing: 20) {
- standingTIRchart
- groupedGlucose
- }
- }
- }
- init(
- filter: NSDate,
- _ highLimit: Binding<Decimal>,
- _ lowLimit: Binding<Decimal>,
- _ units: Binding<GlucoseUnits>,
- _ overrideUnit: Binding<Bool>,
- _ standing: Binding<Bool>
- ) { _fetchRequest = FetchRequest<Readings>(
- sortDescriptors: [NSSortDescriptor(key: "date", ascending: false)],
- predicate: NSPredicate(format: "glucose > 0 AND date > %@", filter)
- )
- _highLimit = highLimit
- _lowLimit = lowLimit
- _units = units
- _overrideUnit = overrideUnit
- _standing = standing
- }
- var glucoseChart: some View {
- // Be aware of the low/lowLimit difference. lowLimit/highLimit is always in mg/dl, whereas low/high is configurable in settings
- let low = lowLimit * (units == .mmolL ? Decimal(conversionFactor) : 1)
- let high = highLimit * (units == .mmolL ? Decimal(conversionFactor) : 1)
- let readings = fetchRequest
- let count = readings.count
- // The symbol size when fewer readings are larger
- let sizeOfDataPoints: CGFloat = count < 20 ? 50 : count < 50 ? 35 : count > 2000 ? 5 : 15
- return Chart {
- ForEach(readings.filter({ $0.glucose > Int(highLimit) }), id: \.date) { item in
- PointMark(
- x: .value("Date", item.date ?? Date()),
- y: .value("High", Double(item.glucose) * (units == .mmolL ? self.conversionFactor : 1))
- )
- .foregroundStyle(.orange)
- .symbolSize(sizeOfDataPoints)
- }
- ForEach(
- readings
- .filter({
- $0.glucose >= Int(lowLimit) && $0
- .glucose <= Int(highLimit) }),
- id: \.date
- ) { item in
- PointMark(
- x: .value("Date", item.date ?? Date()),
- y: .value("In Range", Double(item.glucose) * (units == .mmolL ? conversionFactor : 1))
- )
- .foregroundStyle(.green)
- .symbolSize(sizeOfDataPoints)
- }
- ForEach(readings.filter({ $0.glucose < Int(lowLimit) }), id: \.date) { item in
- PointMark(
- x: .value("Date", item.date ?? Date()),
- y: .value("Low", Double(item.glucose) * (units == .mmolL ? conversionFactor : 1))
- )
- .foregroundStyle(.red)
- .symbolSize(sizeOfDataPoints)
- }
- }
- .chartYAxis {
- AxisMarks(
- values: [
- 0,
- low,
- high,
- units == .mmolL ? 15 : 270
- ]
- )
- }
- }
- var tirChart: some View {
- let fetched = tir()
- let low = lowLimit * (units == .mmolL ? Decimal(conversionFactor) : 1)
- let high = highLimit * (units == .mmolL ? Decimal(conversionFactor) : 1)
- let data: [ShapeModel] = [
- .init(
- type: NSLocalizedString(
- "Low",
- comment: ""
- ) + " (≤\(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))",
- percent: fetched[0].decimal
- ),
- .init(type: NSLocalizedString("In Range", comment: ""), percent: fetched[1].decimal),
- .init(
- type: NSLocalizedString(
- "High",
- comment: ""
- ) + " (≥\(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))",
- percent: fetched[2].decimal
- )
- ]
- return Chart(data) { shape in
- BarMark(
- x: .value("TIR", shape.percent)
- )
- .foregroundStyle(by: .value("Group", shape.type))
- .annotation(position: .top, alignment: .center) {
- Text(
- "\(shape.percent, format: .number.precision(.fractionLength(0))) %"
- ).font(.footnote).foregroundColor(.secondary)
- }
- }
- .chartXAxis(.hidden)
- .chartForegroundStyleScale([
- NSLocalizedString(
- "Low",
- comment: ""
- ) + " (≤\(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))": .red,
- NSLocalizedString("In Range", comment: ""): .green,
- NSLocalizedString(
- "High",
- comment: ""
- ) + " (≥\(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))": .orange
- ]).frame(maxHeight: 25)
- }
- var standingTIRchart: some View {
- let fetched = tir()
- let low = lowLimit * (units == .mmolL ? Decimal(conversionFactor) : 1)
- let high = highLimit * (units == .mmolL ? Decimal(conversionFactor) : 1)
- let data: [ShapeModel] = [
- .init(
- type: NSLocalizedString(
- "Low",
- comment: ""
- ) + " (≤ \(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))",
- percent: fetched[0].decimal
- ),
- .init(type: NSLocalizedString("In Range", comment: ""), percent: fetched[1].decimal),
- .init(
- type: NSLocalizedString(
- "High",
- comment: ""
- ) + " (≥ \(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))",
- percent: fetched[2].decimal
- )
- ]
- return Chart(data) { shape in
- BarMark(
- x: .value("Shape", shape.type),
- y: .value("Percentage", shape.percent)
- )
- .foregroundStyle(by: .value("Group", shape.type))
- .annotation(position: .automatic, alignment: .center) {
- Text(shape.percent == 0 ? "" : "\(shape.percent, format: .number.precision(.fractionLength(0)))")
- }
- }
- .chartXAxis(.hidden)
- .chartForegroundStyleScale([
- NSLocalizedString(
- "Low",
- comment: ""
- ) + " (≤ \(low.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))": .red,
- NSLocalizedString("In Range", comment: ""): .green,
- NSLocalizedString(
- "High",
- comment: ""
- ) + " (≥ \(high.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1)))))": .orange
- ])
- }
- var groupedGlucose: some View {
- VStack(alignment: .leading, spacing: 20) {
- let glucose = fetchRequest
- let mapGlucose = glucose.compactMap({ each in each.glucose })
- let mapGlucoseAcuteLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
- let mapGlucoseHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
- HStack {
- let value = Double(mapGlucoseHigh.count * 100 / mapGlucose.count)
- if value != 0 {
- Text(units == .mmolL ? "> 11" : "> 200").foregroundColor(.secondary)
- Text(value.formatted()).foregroundColor(.orange)
- Text("%").foregroundColor(.secondary)
- }
- }.font(.caption)
- HStack {
- let value = Double(mapGlucoseAcuteLow.count * 100 / mapGlucose.count)
- if value != 0 {
- Text(units == .mmolL ? "< 3.3" : "< 59").foregroundColor(.secondary)
- Text(value.formatted()).foregroundColor(.red)
- Text("%").foregroundColor(.secondary)
- }
- }.font(.caption)
- }
- }
- var groupedGlucoseStatsLaying: some View {
- HStack {
- let glucose = fetchRequest
- let mapGlucose = glucose.compactMap({ each in each.glucose })
- let mapGlucoseLow = mapGlucose.filter({ $0 < Int16(3.3 / 0.0555) })
- let mapGlucoseAcuteLow = mapGlucose.filter({ $0 < Int16(2.6 / 0.0555) })
- let mapGlucoseHigh = mapGlucose.filter({ $0 > Int(8.5 / 0.0555) })
- let mapGlucoseAcuteHigh = mapGlucose.filter({ $0 > Int16(11 / 0.0555) })
- HStack {
- let value = Double(mapGlucoseAcuteLow.count * 100 / mapGlucose.count)
- if value != 0 {
- Text(units == .mmolL ? "< 2.6" : "< 47").font(.caption2).foregroundColor(.secondary)
- Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .red)
- Text("%").font(.caption)
- }
- }.padding(.horizontal, 10)
- HStack {
- let value = Double(mapGlucoseLow.count * 100 / mapGlucose.count)
- if value != 0 {
- Text(units == .mmolL ? "< 3.3" : "< 59").font(.caption2).foregroundColor(.secondary)
- Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .orange)
- Text("%").font(.caption)
- }
- }
- Spacer()
- HStack {
- let value = Double(mapGlucoseHigh.count * 100 / mapGlucose.count)
- if value != 0 {
- Text(units == .mmolL ? "> 8.5" : "> 144").font(.caption).foregroundColor(.secondary)
- Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .orange)
- Text("%").font(.caption)
- }
- }.padding(.horizontal, 10)
- HStack {
- let value = Double(mapGlucoseAcuteHigh.count * 100 / mapGlucose.count)
- if value != 0 {
- Text(units == .mmolL ? "> 11.0" : "> 216").font(.caption).foregroundColor(.secondary)
- Text(value.formatted()).font(.caption).foregroundColor(value == 0 ? .green : .red)
- Text("%").font(.caption)
- }
- }
- }
- }
- private func tir() -> [(decimal: Decimal, string: String)] {
- let hypoLimit = Int(lowLimit)
- let hyperLimit = Int(highLimit)
- let glucose = fetchRequest
- let justGlucoseArray = glucose.compactMap({ each in Int(each.glucose as Int16) })
- let totalReadings = justGlucoseArray.count
- let hyperArray = glucose.filter({ $0.glucose >= hyperLimit })
- let hyperReadings = hyperArray.compactMap({ each in each.glucose as Int16 }).count
- let hyperPercentage = Double(hyperReadings) / Double(totalReadings) * 100
- let hypoArray = glucose.filter({ $0.glucose <= hypoLimit })
- let hypoReadings = hypoArray.compactMap({ each in each.glucose as Int16 }).count
- let hypoPercentage = Double(hypoReadings) / Double(totalReadings) * 100
- let tir = 100 - (hypoPercentage + hyperPercentage)
- var array: [(decimal: Decimal, string: String)] = []
- array.append((decimal: Decimal(hypoPercentage), string: "Low"))
- array.append((decimal: Decimal(tir), string: "NormaL"))
- array.append((decimal: Decimal(hyperPercentage), string: "High"))
- return array
- }
- }
|