StatsDisplayView.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // LoopFollow
  2. // StatsDisplayView.swift
  3. import Charts
  4. import SwiftUI
  5. struct StatsDisplayView: View {
  6. @ObservedObject var model: StatsDisplayModel
  7. var onTap: (() -> Void)?
  8. var body: some View {
  9. HStack {
  10. StatsPieChartView(
  11. pieLow: model.pieLow,
  12. pieRange: model.pieRange,
  13. pieHigh: model.pieHigh
  14. )
  15. .frame(width: 80, height: 80)
  16. .padding(.leading, 8)
  17. VStack(spacing: 10) {
  18. HStack {
  19. statColumn(title: String(localized: "Low:"), value: model.lowPercent)
  20. statColumn(title: String(localized: "In Range:"), value: model.inRangePercent)
  21. statColumn(title: String(localized: "High:"), value: model.highPercent)
  22. }
  23. HStack {
  24. statColumn(title: String(localized: "Avg BG:"), value: model.avgBG)
  25. statColumn(title: model.estA1CTitle, value: model.estA1C)
  26. statColumn(title: model.stdDevTitle, value: model.stdDev)
  27. }
  28. }
  29. .frame(maxWidth: .infinity)
  30. }
  31. .frame(height: 100)
  32. .background(Color(.secondarySystemBackground))
  33. .contentShape(Rectangle())
  34. .onTapGesture { onTap?() }
  35. }
  36. private func statColumn(title: String, value: String) -> some View {
  37. VStack {
  38. Text(title)
  39. .font(.system(size: 15))
  40. Text(value)
  41. .font(.system(size: 15))
  42. }
  43. .frame(maxWidth: .infinity)
  44. }
  45. }
  46. struct StatsPieChartView: View {
  47. var pieLow: Double
  48. var pieRange: Double
  49. var pieHigh: Double
  50. private struct Slice: Identifiable {
  51. let id: String
  52. let value: Double
  53. let color: Color
  54. }
  55. private var slices: [Slice] {
  56. [
  57. Slice(id: "low", value: max(pieLow, 0.1), color: .red),
  58. Slice(id: "range", value: max(pieRange, 0.1), color: .green),
  59. Slice(id: "high", value: max(pieHigh, 0.1), color: .yellow),
  60. ]
  61. }
  62. var body: some View {
  63. Chart(slices) { slice in
  64. SectorMark(angle: .value("share", slice.value))
  65. .foregroundStyle(slice.color)
  66. }
  67. .chartLegend(.hidden)
  68. .allowsHitTesting(false)
  69. }
  70. }