|
@@ -5,7 +5,6 @@ import SwiftUI
|
|
|
|
|
|
|
|
struct GlucoseSectorChart: View {
|
|
struct GlucoseSectorChart: View {
|
|
|
let highLimit: Decimal
|
|
let highLimit: Decimal
|
|
|
- let lowLimit: Decimal
|
|
|
|
|
let units: GlucoseUnits
|
|
let units: GlucoseUnits
|
|
|
let glucose: [GlucoseStored]
|
|
let glucose: [GlucoseStored]
|
|
|
let timeInRangeType: TimeInRangeType
|
|
let timeInRangeType: TimeInRangeType
|
|
@@ -27,15 +26,15 @@ struct GlucoseSectorChart: View {
|
|
|
HStack(alignment: .center, spacing: 20) {
|
|
HStack(alignment: .center, spacing: 20) {
|
|
|
// Calculate total number of glucose readings
|
|
// Calculate total number of glucose readings
|
|
|
let total = Decimal(glucose.count)
|
|
let total = Decimal(glucose.count)
|
|
|
- // Count readings between high limit and 250 mg/dL (high)
|
|
|
|
|
|
|
+ // Count readings greater than high limit (180 mg/dL)
|
|
|
let high = glucose.filter { $0.glucose > Int(highLimit) }.count
|
|
let high = glucose.filter { $0.glucose > Int(highLimit) }.count
|
|
|
// Count readings between low limit (TITR: 70 mg/dL, TING 63 mg/dL) and 140 mg/dL (tight control)
|
|
// Count readings between low limit (TITR: 70 mg/dL, TING 63 mg/dL) and 140 mg/dL (tight control)
|
|
|
let tight = glucose
|
|
let tight = glucose
|
|
|
.filter { $0.glucose >= timeInRangeType.bottomThreshold && $0.glucose <= timeInRangeType.topThreshold }.count
|
|
.filter { $0.glucose >= timeInRangeType.bottomThreshold && $0.glucose <= timeInRangeType.topThreshold }.count
|
|
|
// Count readings between 140 and high limit (normal range)
|
|
// Count readings between 140 and high limit (normal range)
|
|
|
- let normal = glucose.filter { $0.glucose >= Int(lowLimit) && $0.glucose <= Int(highLimit) }.count
|
|
|
|
|
- // Count readings between 54 and low limit (low)
|
|
|
|
|
- let low = glucose.filter { $0.glucose < Int(lowLimit) }.count
|
|
|
|
|
|
|
+ let normal = glucose.filter { $0.glucose >= timeInRangeType.bottomThreshold && $0.glucose <= Int(highLimit) }.count
|
|
|
|
|
+ // Count readings less than low limit (low)
|
|
|
|
|
+ let low = glucose.filter { $0.glucose < timeInRangeType.bottomThreshold }.count
|
|
|
|
|
|
|
|
let justGlucoseArray = glucose.compactMap({ each in Int(each.glucose as Int16) })
|
|
let justGlucoseArray = glucose.compactMap({ each in Int(each.glucose as Int16) })
|
|
|
let sumReadings = justGlucoseArray.reduce(0, +)
|
|
let sumReadings = justGlucoseArray.reduce(0, +)
|
|
@@ -50,7 +49,8 @@ struct GlucoseSectorChart: View {
|
|
|
|
|
|
|
|
VStack(alignment: .leading, spacing: 10) {
|
|
VStack(alignment: .leading, spacing: 10) {
|
|
|
VStack(alignment: .leading, spacing: 5) {
|
|
VStack(alignment: .leading, spacing: 5) {
|
|
|
- Text("\(formatValue(lowLimit))-\(formatValue(highLimit))").font(.subheadline).foregroundStyle(Color.secondary)
|
|
|
|
|
|
|
+ Text("\(formatValue(Decimal(timeInRangeType.bottomThreshold)))-\(formatValue(highLimit))").font(.subheadline)
|
|
|
|
|
+ .foregroundStyle(Color.secondary)
|
|
|
Text(inRangePercentage.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1))) + "%")
|
|
Text(inRangePercentage.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1))) + "%")
|
|
|
.foregroundStyle(Color.loopGreen)
|
|
.foregroundStyle(Color.loopGreen)
|
|
|
}
|
|
}
|
|
@@ -68,13 +68,18 @@ struct GlucoseSectorChart: View {
|
|
|
|
|
|
|
|
VStack(alignment: .leading, spacing: 10) {
|
|
VStack(alignment: .leading, spacing: 10) {
|
|
|
VStack(alignment: .leading, spacing: 5) {
|
|
VStack(alignment: .leading, spacing: 5) {
|
|
|
- Text("> \(formatValue(highLimit))").font(.subheadline).foregroundStyle(Color.secondary)
|
|
|
|
|
|
|
+ Text("> \(formatValue(highLimit))").font(.subheadline)
|
|
|
|
|
+ .foregroundStyle(Color.secondary)
|
|
|
Text(highPercentage.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1))) + "%")
|
|
Text(highPercentage.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1))) + "%")
|
|
|
.foregroundStyle(Color.orange)
|
|
.foregroundStyle(Color.orange)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
VStack(alignment: .leading, spacing: 5) {
|
|
VStack(alignment: .leading, spacing: 5) {
|
|
|
- Text("< \(formatValue(lowLimit))").font(.subheadline).foregroundStyle(Color.secondary)
|
|
|
|
|
|
|
+ Text(
|
|
|
|
|
+ "< \(formatValue(Decimal(timeInRangeType.bottomThreshold)))"
|
|
|
|
|
+ )
|
|
|
|
|
+ .font(.subheadline)
|
|
|
|
|
+ .foregroundStyle(Color.secondary)
|
|
|
Text(lowPercentage.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1))) + "%")
|
|
Text(lowPercentage.formatted(.number.grouping(.never).rounded().precision(.fractionLength(1))) + "%")
|
|
|
.foregroundStyle(Color.loopRed)
|
|
.foregroundStyle(Color.loopRed)
|
|
|
}
|
|
}
|
|
@@ -156,7 +161,7 @@ struct GlucoseSectorChart: View {
|
|
|
// Count readings above high limit
|
|
// Count readings above high limit
|
|
|
let highCount = glucose.filter { $0.glucose > Int(highLimit) }.count
|
|
let highCount = glucose.filter { $0.glucose > Int(highLimit) }.count
|
|
|
// Count readings below low limit
|
|
// Count readings below low limit
|
|
|
- let lowCount = glucose.filter { $0.glucose < Int(lowLimit) }.count
|
|
|
|
|
|
|
+ let lowCount = glucose.filter { $0.glucose < timeInRangeType.bottomThreshold }.count
|
|
|
// Calculate in-range readings by subtracting high and low counts from total
|
|
// Calculate in-range readings by subtracting high and low counts from total
|
|
|
let inRangeCount = total - highCount - lowCount
|
|
let inRangeCount = total - highCount - lowCount
|
|
|
|
|
|
|
@@ -227,7 +232,7 @@ struct GlucoseSectorChart: View {
|
|
|
case .inRange:
|
|
case .inRange:
|
|
|
let tight = glucose
|
|
let tight = glucose
|
|
|
.filter { $0.glucose >= Int(timeInRangeType.bottomThreshold) && $0.glucose <= timeInRangeType.topThreshold }.count
|
|
.filter { $0.glucose >= Int(timeInRangeType.bottomThreshold) && $0.glucose <= timeInRangeType.topThreshold }.count
|
|
|
- let glucoseValues = glucose.filter { $0.glucose >= Int(lowLimit) && $0.glucose <= Int(highLimit) }
|
|
|
|
|
|
|
+ let glucoseValues = glucose.filter { $0.glucose >= timeInRangeType.bottomThreshold && $0.glucose <= Int(highLimit) }
|
|
|
let glucoseValuesAsInt = glucoseValues.map { Int($0.glucose) }
|
|
let glucoseValuesAsInt = glucoseValues.map { Int($0.glucose) }
|
|
|
let (average, median, standardDeviation) = calculateDetailedStatistics(for: glucoseValuesAsInt)
|
|
let (average, median, standardDeviation) = calculateDetailedStatistics(for: glucoseValuesAsInt)
|
|
|
|
|
|
|
@@ -236,7 +241,9 @@ struct GlucoseSectorChart: View {
|
|
|
color: .green,
|
|
color: .green,
|
|
|
items: [
|
|
items: [
|
|
|
(
|
|
(
|
|
|
- String(localized: "Normal (\(formatValue(lowLimit))-\(formatValue(highLimit)))"),
|
|
|
|
|
|
|
+ String(
|
|
|
|
|
+ localized: "Normal (\(formatValue(Decimal(timeInRangeType.bottomThreshold)))-\(formatValue(highLimit)))"
|
|
|
|
|
+ ),
|
|
|
formatPercentage(Decimal(glucoseValues.count) / total * 100)
|
|
formatPercentage(Decimal(glucoseValues.count) / total * 100)
|
|
|
),
|
|
),
|
|
|
(
|
|
(
|
|
@@ -253,9 +260,9 @@ struct GlucoseSectorChart: View {
|
|
|
|
|
|
|
|
case .low:
|
|
case .low:
|
|
|
let veryLow = glucose.filter { $0.glucose <= 54 }.count
|
|
let veryLow = glucose.filter { $0.glucose <= 54 }.count
|
|
|
- let low = glucose.filter { $0.glucose > 54 && $0.glucose < Int(lowLimit) }.count
|
|
|
|
|
|
|
+ let low = glucose.filter { $0.glucose > 54 && $0.glucose < timeInRangeType.bottomThreshold }.count
|
|
|
|
|
|
|
|
- let lowGlucoseValues = glucose.filter { $0.glucose < Int(lowLimit) }
|
|
|
|
|
|
|
+ let lowGlucoseValues = glucose.filter { $0.glucose < timeInRangeType.bottomThreshold }
|
|
|
let lowGlucoseValuesAsInt = lowGlucoseValues.map { Int($0.glucose) }
|
|
let lowGlucoseValuesAsInt = lowGlucoseValues.map { Int($0.glucose) }
|
|
|
let (average, median, standardDeviation) = calculateDetailedStatistics(for: lowGlucoseValuesAsInt)
|
|
let (average, median, standardDeviation) = calculateDetailedStatistics(for: lowGlucoseValuesAsInt)
|
|
|
|
|
|
|
@@ -264,7 +271,7 @@ struct GlucoseSectorChart: View {
|
|
|
color: .red,
|
|
color: .red,
|
|
|
items: [
|
|
items: [
|
|
|
(
|
|
(
|
|
|
- String(localized: "Low (\(formatValue(54))-\(formatValue(lowLimit)))"),
|
|
|
|
|
|
|
+ String(localized: "Low (\(formatValue(54))-\(formatValue(Decimal(timeInRangeType.bottomThreshold))))"),
|
|
|
formatPercentage(Decimal(low) / total * 100)
|
|
formatPercentage(Decimal(low) / total * 100)
|
|
|
),
|
|
),
|
|
|
(String(localized: "Very Low (<\(formatValue(54)))"), formatPercentage(Decimal(veryLow) / total * 100)),
|
|
(String(localized: "Very Low (<\(formatValue(54)))"), formatPercentage(Decimal(veryLow) / total * 100)),
|