Graphs.swift 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273
  1. //
  2. // Graphs.swift
  3. // LoopFollow
  4. //
  5. // Created by Jon Fawcett on 6/16/20.
  6. // Copyright © 2020 Jon Fawcett. All rights reserved.
  7. //
  8. import Foundation
  9. import Charts
  10. import UIKit
  11. let ScaleXMax:Float = 150.0
  12. extension MainViewController {
  13. func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
  14. if chartView == BGChartFull {
  15. BGChart.moveViewToX(entry.x)
  16. }
  17. if entry.data as? String == "hide"{
  18. BGChart.highlightValue(nil, callDelegate: false)
  19. }
  20. }
  21. func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat) {
  22. print("Chart Scaled: \(BGChart.scaleX), \(BGChart.scaleY)")
  23. // dont store huge values
  24. var scale: Float = Float(BGChart.scaleX)
  25. if(scale > ScaleXMax ) {
  26. scale = ScaleXMax
  27. }
  28. UserDefaultsRepository.chartScaleX.value = Float(scale)
  29. }
  30. func createGraph(){
  31. self.BGChart.clear()
  32. // Create the BG Graph Data
  33. let entries = bgData
  34. var bgChartEntry = [ChartDataEntry]()
  35. var colors = [NSUIColor]()
  36. var maxBG: Float = UserDefaultsRepository.minBGScale.value
  37. // Setup BG line details
  38. let lineBG = LineChartDataSet(entries:bgChartEntry, label: "")
  39. lineBG.circleRadius = CGFloat(globalVariables.dotBG)
  40. lineBG.circleColors = [NSUIColor.systemGreen]
  41. lineBG.drawCircleHoleEnabled = false
  42. lineBG.axisDependency = YAxis.AxisDependency.right
  43. lineBG.highlightEnabled = true
  44. lineBG.drawValuesEnabled = false
  45. if UserDefaultsRepository.showLines.value {
  46. lineBG.lineWidth = 2
  47. } else {
  48. lineBG.lineWidth = 0
  49. }
  50. if UserDefaultsRepository.showDots.value {
  51. lineBG.drawCirclesEnabled = true
  52. } else {
  53. lineBG.drawCirclesEnabled = false
  54. }
  55. lineBG.setDrawHighlightIndicators(false)
  56. lineBG.valueFont.withSize(50)
  57. // Setup Prediction line details
  58. var predictionChartEntry = [ChartDataEntry]()
  59. let linePrediction = LineChartDataSet(entries:predictionChartEntry, label: "")
  60. linePrediction.circleRadius = CGFloat(globalVariables.dotBG)
  61. linePrediction.circleColors = [NSUIColor.systemPurple]
  62. linePrediction.colors = [NSUIColor.systemPurple]
  63. linePrediction.drawCircleHoleEnabled = false
  64. linePrediction.axisDependency = YAxis.AxisDependency.right
  65. linePrediction.highlightEnabled = true
  66. linePrediction.drawValuesEnabled = false
  67. if UserDefaultsRepository.showLines.value {
  68. linePrediction.lineWidth = 2
  69. } else {
  70. linePrediction.lineWidth = 0
  71. }
  72. if UserDefaultsRepository.showDots.value {
  73. linePrediction.drawCirclesEnabled = true
  74. } else {
  75. linePrediction.drawCirclesEnabled = false
  76. }
  77. linePrediction.setDrawHighlightIndicators(false)
  78. linePrediction.valueFont.withSize(50)
  79. // create Basal graph data
  80. var chartEntry = [ChartDataEntry]()
  81. var maxBasal = UserDefaultsRepository.minBasalScale.value
  82. let lineBasal = LineChartDataSet(entries:chartEntry, label: "")
  83. lineBasal.setDrawHighlightIndicators(false)
  84. lineBasal.setColor(NSUIColor.systemBlue, alpha: 0.5)
  85. lineBasal.lineWidth = 0
  86. lineBasal.drawFilledEnabled = true
  87. lineBasal.fillColor = NSUIColor.systemBlue
  88. lineBasal.fillAlpha = 0.5
  89. lineBasal.drawCirclesEnabled = false
  90. lineBasal.axisDependency = YAxis.AxisDependency.left
  91. lineBasal.highlightEnabled = true
  92. lineBasal.drawValuesEnabled = false
  93. lineBasal.fillFormatter = basalFillFormatter()
  94. // Boluses
  95. var chartEntryBolus = [ChartDataEntry]()
  96. let lineBolus = LineChartDataSet(entries:chartEntryBolus, label: "")
  97. lineBolus.circleRadius = CGFloat(globalVariables.dotBolus)
  98. lineBolus.circleColors = [NSUIColor.systemBlue.withAlphaComponent(0.75)]
  99. lineBolus.drawCircleHoleEnabled = false
  100. lineBolus.setDrawHighlightIndicators(false)
  101. lineBolus.setColor(NSUIColor.systemBlue, alpha: 1.0)
  102. lineBolus.lineWidth = 0
  103. lineBolus.axisDependency = YAxis.AxisDependency.right
  104. lineBolus.valueFormatter = ChartYDataValueFormatter()
  105. lineBolus.valueTextColor = NSUIColor.label
  106. lineBolus.fillColor = NSUIColor.systemBlue
  107. lineBolus.fillAlpha = 0.6
  108. lineBolus.drawCirclesEnabled = true
  109. lineBolus.drawFilledEnabled = false
  110. if UserDefaultsRepository.showValues.value {
  111. lineBolus.drawValuesEnabled = true
  112. lineBolus.highlightEnabled = false
  113. } else {
  114. lineBolus.drawValuesEnabled = false
  115. lineBolus.highlightEnabled = true
  116. }
  117. // Carbs
  118. var chartEntryCarbs = [ChartDataEntry]()
  119. let lineCarbs = LineChartDataSet(entries:chartEntryCarbs, label: "")
  120. lineCarbs.circleRadius = CGFloat(globalVariables.dotCarb)
  121. lineCarbs.circleColors = [NSUIColor.systemOrange.withAlphaComponent(0.75)]
  122. lineCarbs.drawCircleHoleEnabled = false
  123. lineCarbs.setDrawHighlightIndicators(false)
  124. lineCarbs.setColor(NSUIColor.systemBlue, alpha: 1.0)
  125. lineCarbs.lineWidth = 0
  126. lineCarbs.axisDependency = YAxis.AxisDependency.right
  127. lineCarbs.valueFormatter = ChartYDataValueFormatter()
  128. lineCarbs.valueTextColor = NSUIColor.label
  129. lineCarbs.fillColor = NSUIColor.systemOrange
  130. lineCarbs.fillAlpha = 0.6
  131. lineCarbs.drawCirclesEnabled = true
  132. lineCarbs.drawFilledEnabled = false
  133. if UserDefaultsRepository.showValues.value {
  134. lineCarbs.drawValuesEnabled = true
  135. lineCarbs.highlightEnabled = false
  136. } else {
  137. lineCarbs.drawValuesEnabled = false
  138. lineCarbs.highlightEnabled = true
  139. }
  140. // create Scheduled Basal graph data
  141. var chartBasalScheduledEntry = [ChartDataEntry]()
  142. let lineBasalScheduled = LineChartDataSet(entries:chartBasalScheduledEntry, label: "")
  143. lineBasalScheduled.setDrawHighlightIndicators(false)
  144. lineBasalScheduled.setColor(NSUIColor.systemBlue, alpha: 0.8)
  145. lineBasalScheduled.lineWidth = 2
  146. lineBasalScheduled.drawFilledEnabled = false
  147. lineBasalScheduled.drawCirclesEnabled = false
  148. lineBasalScheduled.axisDependency = YAxis.AxisDependency.left
  149. lineBasalScheduled.highlightEnabled = false
  150. lineBasalScheduled.drawValuesEnabled = false
  151. lineBasalScheduled.lineDashLengths = [10.0, 5.0]
  152. // create Override graph data
  153. var chartOverrideEntry = [ChartDataEntry]()
  154. let lineOverride = LineChartDataSet(entries:chartOverrideEntry, label: "")
  155. lineOverride.setDrawHighlightIndicators(false)
  156. lineOverride.lineWidth = 0
  157. lineOverride.drawFilledEnabled = true
  158. lineOverride.fillFormatter = OverrideFillFormatter()
  159. lineOverride.fillColor = NSUIColor.systemGreen
  160. lineOverride.fillAlpha = 0.6
  161. lineOverride.drawCirclesEnabled = false
  162. lineOverride.axisDependency = YAxis.AxisDependency.right
  163. lineOverride.highlightEnabled = true
  164. lineOverride.drawValuesEnabled = false
  165. // lineOverride.circleColors = [NSUIColor.systemGreen.withAlphaComponent(0.75)]
  166. // lineOverride.valueFormatter = ChartYDataValueFormatter()
  167. // lineOverride.drawCircleHoleEnabled = false
  168. // BG Check
  169. var chartEntryBGCheck = [ChartDataEntry]()
  170. let lineBGCheck = LineChartDataSet(entries:chartEntryBGCheck, label: "")
  171. lineBGCheck.circleRadius = CGFloat(globalVariables.dotOther)
  172. lineBGCheck.circleColors = [NSUIColor.systemRed.withAlphaComponent(0.75)]
  173. lineBGCheck.drawCircleHoleEnabled = false
  174. lineBGCheck.setDrawHighlightIndicators(false)
  175. lineBGCheck.setColor(NSUIColor.systemRed, alpha: 1.0)
  176. lineBGCheck.drawCirclesEnabled = true
  177. lineBGCheck.lineWidth = 0
  178. lineBGCheck.highlightEnabled = true
  179. lineBGCheck.axisDependency = YAxis.AxisDependency.right
  180. lineBGCheck.valueFormatter = ChartYDataValueFormatter()
  181. lineBGCheck.drawValuesEnabled = false
  182. // Suspend Pump
  183. var chartEntrySuspend = [ChartDataEntry]()
  184. let lineSuspend = LineChartDataSet(entries:chartEntrySuspend, label: "")
  185. lineSuspend.circleRadius = CGFloat(globalVariables.dotOther)
  186. lineSuspend.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
  187. lineSuspend.drawCircleHoleEnabled = false
  188. lineSuspend.setDrawHighlightIndicators(false)
  189. lineSuspend.setColor(NSUIColor.systemGray2, alpha: 1.0)
  190. lineSuspend.drawCirclesEnabled = true
  191. lineSuspend.lineWidth = 0
  192. lineSuspend.highlightEnabled = true
  193. lineSuspend.axisDependency = YAxis.AxisDependency.right
  194. lineSuspend.valueFormatter = ChartYDataValueFormatter()
  195. lineSuspend.drawValuesEnabled = false
  196. // Resume Pump
  197. var chartEntryResume = [ChartDataEntry]()
  198. let lineResume = LineChartDataSet(entries:chartEntryResume, label: "")
  199. lineResume.circleRadius = CGFloat(globalVariables.dotOther)
  200. lineResume.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
  201. lineResume.drawCircleHoleEnabled = false
  202. lineResume.setDrawHighlightIndicators(false)
  203. lineResume.setColor(NSUIColor.systemGray4, alpha: 1.0)
  204. lineResume.drawCirclesEnabled = true
  205. lineResume.lineWidth = 0
  206. lineResume.highlightEnabled = true
  207. lineResume.axisDependency = YAxis.AxisDependency.right
  208. lineResume.valueFormatter = ChartYDataValueFormatter()
  209. lineResume.drawValuesEnabled = false
  210. // Sensor Start
  211. var chartEntrySensor = [ChartDataEntry]()
  212. let lineSensor = LineChartDataSet(entries:chartEntrySensor, label: "")
  213. lineSensor.circleRadius = CGFloat(globalVariables.dotOther)
  214. lineSensor.circleColors = [NSUIColor.systemIndigo.withAlphaComponent(0.75)]
  215. lineSensor.drawCircleHoleEnabled = false
  216. lineSensor.setDrawHighlightIndicators(false)
  217. lineSensor.setColor(NSUIColor.systemGray3, alpha: 1.0)
  218. lineSensor.drawCirclesEnabled = true
  219. lineSensor.lineWidth = 0
  220. lineSensor.highlightEnabled = true
  221. lineSensor.axisDependency = YAxis.AxisDependency.right
  222. lineSensor.valueFormatter = ChartYDataValueFormatter()
  223. lineSensor.drawValuesEnabled = false
  224. // Notes
  225. var chartEntryNote = [ChartDataEntry]()
  226. let lineNote = LineChartDataSet(entries:chartEntryNote, label: "")
  227. lineNote.circleRadius = CGFloat(globalVariables.dotOther)
  228. lineNote.circleColors = [NSUIColor.systemGray.withAlphaComponent(0.75)]
  229. lineNote.drawCircleHoleEnabled = false
  230. lineNote.setDrawHighlightIndicators(false)
  231. lineNote.setColor(NSUIColor.systemGray3, alpha: 1.0)
  232. lineNote.drawCirclesEnabled = true
  233. lineNote.lineWidth = 0
  234. lineNote.highlightEnabled = true
  235. lineNote.axisDependency = YAxis.AxisDependency.right
  236. lineNote.valueFormatter = ChartYDataValueFormatter()
  237. lineNote.drawValuesEnabled = false
  238. // Setup the chart data of all lines
  239. let data = LineChartData()
  240. data.append(lineBG) // Dataset 0
  241. data.append(linePrediction) // Dataset 1
  242. data.append(lineBasal) // Dataset 2
  243. data.append(lineBolus) // Dataset 3
  244. data.append(lineCarbs) // Dataset 4
  245. data.append(lineBasalScheduled) // Dataset 5
  246. data.append(lineOverride) // Dataset 6
  247. data.append(lineBGCheck) // Dataset 7
  248. data.append(lineSuspend) // Dataset 8
  249. data.append(lineResume) // Dataset 9
  250. data.append(lineSensor) // Dataset 10
  251. data.append(lineNote) // Dataset 11
  252. data.setValueFont(UIFont.systemFont(ofSize: 12))
  253. // Add marker popups for bolus and carbs
  254. let marker = PillMarker(color: .secondarySystemBackground, font: UIFont.boldSystemFont(ofSize: 14), textColor: .label)
  255. BGChart.marker = marker
  256. // Clear limit lines so they don't add multiples when changing the settings
  257. BGChart.rightAxis.removeAllLimitLines()
  258. //Add lower red line based on low alert value
  259. let ll = ChartLimitLine()
  260. ll.limit = Double(UserDefaultsRepository.lowLine.value)
  261. ll.lineColor = NSUIColor.systemRed.withAlphaComponent(0.5)
  262. BGChart.rightAxis.addLimitLine(ll)
  263. //Add upper yellow line based on low alert value
  264. let ul = ChartLimitLine()
  265. ul.limit = Double(UserDefaultsRepository.highLine.value)
  266. ul.lineColor = NSUIColor.systemYellow.withAlphaComponent(0.5)
  267. BGChart.rightAxis.addLimitLine(ul)
  268. // Add vertical lines as configured
  269. createVerticalLines()
  270. startGraphNowTimer()
  271. // Setup the main graph overall details
  272. BGChart.xAxis.valueFormatter = ChartXValueFormatter()
  273. BGChart.xAxis.granularity = 1800
  274. BGChart.xAxis.labelTextColor = NSUIColor.label
  275. BGChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
  276. BGChart.xAxis.drawGridLinesEnabled = false
  277. BGChart.leftAxis.enabled = true
  278. BGChart.leftAxis.labelPosition = YAxis.LabelPosition.insideChart
  279. BGChart.leftAxis.axisMaximum = maxBasal
  280. BGChart.leftAxis.axisMinimum = 0
  281. BGChart.leftAxis.drawGridLinesEnabled = false
  282. BGChart.leftAxis.granularityEnabled = true
  283. BGChart.leftAxis.granularity = 0.5
  284. BGChart.rightAxis.labelTextColor = NSUIColor.label
  285. BGChart.rightAxis.labelPosition = YAxis.LabelPosition.insideChart
  286. BGChart.rightAxis.axisMinimum = 0.0
  287. BGChart.rightAxis.axisMaximum = Double(maxBG)
  288. BGChart.rightAxis.gridLineDashLengths = [5.0, 5.0]
  289. BGChart.rightAxis.drawGridLinesEnabled = false
  290. BGChart.rightAxis.valueFormatter = ChartYMMOLValueFormatter()
  291. BGChart.rightAxis.granularityEnabled = true
  292. BGChart.rightAxis.granularity = 50
  293. BGChart.maxHighlightDistance = 15.0
  294. BGChart.legend.enabled = false
  295. BGChart.scaleYEnabled = false
  296. BGChart.drawGridBackgroundEnabled = true
  297. BGChart.gridBackgroundColor = NSUIColor.secondarySystemBackground
  298. BGChart.highlightValue(nil, callDelegate: false)
  299. BGChart.data = data
  300. BGChart.setExtraOffsets(left: 5, top: 10, right: 5, bottom: 10)
  301. }
  302. func createVerticalLines() {
  303. BGChart.xAxis.removeAllLimitLines()
  304. BGChartFull.xAxis.removeAllLimitLines()
  305. createNowAndDIALines()
  306. createMidnightLines()
  307. }
  308. func createNowAndDIALines() {
  309. let ul = ChartLimitLine()
  310. ul.limit = Double(dateTimeUtils.getNowTimeIntervalUTC())
  311. ul.lineColor = NSUIColor.systemGray.withAlphaComponent(0.5)
  312. ul.lineWidth = 1
  313. BGChart.xAxis.addLimitLine(ul)
  314. if UserDefaultsRepository.show30MinLine.value {
  315. let ul2 = ChartLimitLine()
  316. ul2.limit = Double(dateTimeUtils.getNowTimeIntervalUTC().advanced(by: -30 * 60))
  317. ul2.lineColor = NSUIColor.systemBlue.withAlphaComponent(0.5)
  318. ul2.lineWidth = 1
  319. BGChart.xAxis.addLimitLine(ul2)
  320. }
  321. if UserDefaultsRepository.showDIALines.value {
  322. for i in 1..<7 {
  323. let ul = ChartLimitLine()
  324. ul.limit = Double(dateTimeUtils.getNowTimeIntervalUTC() - Double(i * 60 * 60))
  325. ul.lineColor = NSUIColor.systemGray.withAlphaComponent(0.3)
  326. let dash = 10.0 - Double(i)
  327. let space = 5.0 + Double(i)
  328. ul.lineDashLengths = [CGFloat(dash), CGFloat(space)]
  329. ul.lineWidth = 1
  330. BGChart.xAxis.addLimitLine(ul)
  331. }
  332. }
  333. if UserDefaultsRepository.show90MinLine.value {
  334. let ul3 = ChartLimitLine()
  335. ul3.limit = Double(dateTimeUtils.getNowTimeIntervalUTC().advanced(by: -90 * 60))
  336. ul3.lineColor = NSUIColor.systemOrange.withAlphaComponent(0.5)
  337. ul3.lineWidth = 1
  338. BGChart.xAxis.addLimitLine(ul3)
  339. }
  340. }
  341. func createMidnightLines() {
  342. // Draw a line at midnight: useful when showing multiple days of data
  343. if UserDefaultsRepository.showMidnightLines.value {
  344. var midnightTimeInterval = dateTimeUtils.getTimeIntervalMidnightToday()
  345. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  346. let graphStart = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
  347. while midnightTimeInterval > graphStart {
  348. // Large chart
  349. let ul = ChartLimitLine()
  350. ul.limit = Double(midnightTimeInterval)
  351. ul.lineColor = NSUIColor.systemTeal.withAlphaComponent(0.5)
  352. ul.lineDashLengths = [CGFloat(2), CGFloat(5)]
  353. ul.lineWidth = 1
  354. BGChart.xAxis.addLimitLine(ul)
  355. // Small chart
  356. let sl = ChartLimitLine()
  357. sl.limit = Double(midnightTimeInterval)
  358. sl.lineColor = NSUIColor.systemTeal
  359. sl.lineDashLengths = [CGFloat(2), CGFloat(2)]
  360. sl.lineWidth = 1
  361. BGChartFull.xAxis.addLimitLine(sl)
  362. midnightTimeInterval = midnightTimeInterval.advanced(by: -24*60*60)
  363. }
  364. }
  365. }
  366. func updateBGGraphSettings() {
  367. let dataIndex = 0
  368. let dataIndexPrediction = 1
  369. let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  370. let linePrediction = BGChart.lineData!.dataSets[dataIndexPrediction] as! LineChartDataSet
  371. if UserDefaultsRepository.showLines.value {
  372. lineBG.lineWidth = 2
  373. linePrediction.lineWidth = 2
  374. } else {
  375. lineBG.lineWidth = 0
  376. linePrediction.lineWidth = 0
  377. }
  378. if UserDefaultsRepository.showDots.value {
  379. lineBG.drawCirclesEnabled = true
  380. linePrediction.drawCirclesEnabled = true
  381. } else {
  382. lineBG.drawCirclesEnabled = false
  383. linePrediction.drawCirclesEnabled = false
  384. }
  385. BGChart.rightAxis.axisMinimum = 0
  386. // Clear limit lines so they don't add multiples when changing the settings
  387. BGChart.rightAxis.removeAllLimitLines()
  388. //Add lower red line based on low alert value
  389. let ll = ChartLimitLine()
  390. ll.limit = Double(UserDefaultsRepository.lowLine.value)
  391. ll.lineColor = NSUIColor.systemRed.withAlphaComponent(0.5)
  392. BGChart.rightAxis.addLimitLine(ll)
  393. //Add upper yellow line based on low alert value
  394. let ul = ChartLimitLine()
  395. ul.limit = Double(UserDefaultsRepository.highLine.value)
  396. ul.lineColor = NSUIColor.systemYellow.withAlphaComponent(0.5)
  397. BGChart.rightAxis.addLimitLine(ul)
  398. // Re-create vertical markers in case their settings changed
  399. createVerticalLines()
  400. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  401. BGChart.data?.notifyDataChanged()
  402. BGChart.notifyDataSetChanged()
  403. }
  404. func updateBGGraph() {
  405. if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "##### Start BG Graph #####") }
  406. let dataIndex = 0
  407. let entries = bgData
  408. if entries.count < 1 { return }
  409. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  410. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  411. mainChart.clear()
  412. smallChart.clear()
  413. var maxBGOffset: Float = 50
  414. var colors = [NSUIColor]()
  415. for i in 0..<entries.count{
  416. if Float(entries[i].sgv) > topBG - maxBGOffset {
  417. topBG = Float(entries[i].sgv) + maxBGOffset
  418. }
  419. let value = ChartDataEntry(x: Double(entries[i].date), y: Double(entries[i].sgv), data: formatPillText(line1: bgUnits.toDisplayUnits(String(entries[i].sgv)), time: entries[i].date))
  420. if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "BG: " + value.description) }
  421. mainChart.addEntry(value)
  422. smallChart.addEntry(value)
  423. if Double(entries[i].sgv) >= Double(UserDefaultsRepository.highLine.value) {
  424. colors.append(NSUIColor.systemYellow)
  425. } else if Double(entries[i].sgv) <= Double(UserDefaultsRepository.lowLine.value) {
  426. colors.append(NSUIColor.systemRed)
  427. } else {
  428. colors.append(NSUIColor.systemGreen)
  429. }
  430. }
  431. if (predictionData.count < 1) {
  432. let value = ChartDataEntry(x: Double(entries[entries.count-1].date+1200), y: 0)
  433. mainChart.addEntry(value)
  434. colors.append(NSUIColor.systemGray.withAlphaComponent(0.0))
  435. }
  436. if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "Total Graph BGs: " + mainChart.entries.count.description) }
  437. // Set Colors
  438. let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  439. let lineBGSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  440. lineBG.colors.removeAll()
  441. lineBG.circleColors.removeAll()
  442. lineBGSmall.colors.removeAll()
  443. lineBGSmall.circleColors.removeAll()
  444. if colors.count > 0 {
  445. for i in 0..<colors.count{
  446. mainChart.addColor(colors[i])
  447. mainChart.circleColors.append(colors[i])
  448. smallChart.addColor(colors[i])
  449. smallChart.circleColors.append(colors[i])
  450. }
  451. }
  452. if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "Total Colors: " + mainChart.colors.count.description) }
  453. BGChart.rightAxis.axisMaximum = Double(topBG)
  454. BGChart.setVisibleXRangeMinimum(600)
  455. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  456. BGChart.data?.notifyDataChanged()
  457. BGChart.notifyDataSetChanged()
  458. BGChartFull.rightAxis.axisMaximum = Double(topBG)
  459. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  460. BGChartFull.data?.notifyDataChanged()
  461. BGChartFull.notifyDataSetChanged()
  462. if firstGraphLoad {
  463. var scaleX = CGFloat(UserDefaultsRepository.chartScaleX.value)
  464. print("Scale: \(scaleX)")
  465. if( scaleX > CGFloat(ScaleXMax) ) {
  466. scaleX = CGFloat(ScaleXMax)
  467. UserDefaultsRepository.chartScaleX.value = ScaleXMax
  468. }
  469. BGChart.zoom(scaleX: scaleX, scaleY: 1, x: 1, y: 1)
  470. firstGraphLoad = false
  471. }
  472. // Move to current reading everytime new readings load
  473. BGChart.moveViewToAnimated(xValue: dateTimeUtils.getNowTimeIntervalUTC() - (BGChart.visibleXRange * 0.7), yValue: 0.0, axis: .right, duration: 1, easingOption: .easeInBack)
  474. }
  475. func updatePredictionGraph(color: UIColor? = nil) {
  476. let dataIndex = 1
  477. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  478. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  479. mainChart.clear()
  480. smallChart.clear()
  481. var colors = [NSUIColor]()
  482. let maxBGOffset: Float = 20
  483. for i in 0..<predictionData.count {
  484. var predictionVal = Double(predictionData[i].sgv)
  485. if Float(predictionVal) > topBG - maxBGOffset {
  486. topBG = Float(predictionVal) + maxBGOffset
  487. }
  488. if i == 0 {
  489. if UserDefaultsRepository.showDots.value {
  490. colors.append((color ?? NSUIColor.systemPurple).withAlphaComponent(0.0))
  491. } else {
  492. colors.append((color ?? NSUIColor.systemPurple).withAlphaComponent(1.0))
  493. }
  494. } else if predictionVal > 400 {
  495. colors.append(color ?? NSUIColor.systemYellow)
  496. } else if predictionVal < 0 {
  497. colors.append(color ?? NSUIColor.systemRed)
  498. } else {
  499. colors.append(color ?? NSUIColor.systemPurple)
  500. }
  501. let value = ChartDataEntry(x: predictionData[i].date, y: predictionVal, data: formatPillText(line1: bgUnits.toDisplayUnits(String(predictionData[i].sgv)), time: predictionData[i].date))
  502. mainChart.addEntry(value)
  503. smallChart.addEntry(value)
  504. }
  505. smallChart.circleColors.removeAll()
  506. smallChart.colors.removeAll()
  507. mainChart.colors.removeAll()
  508. mainChart.circleColors.removeAll()
  509. if colors.count > 0 {
  510. for i in 0..<colors.count {
  511. mainChart.addColor(colors[i])
  512. mainChart.circleColors.append(colors[i])
  513. smallChart.addColor(colors[i])
  514. smallChart.circleColors.append(colors[i])
  515. }
  516. }
  517. BGChart.rightAxis.axisMaximum = Double(topBG)
  518. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  519. BGChart.data?.notifyDataChanged()
  520. BGChart.notifyDataSetChanged()
  521. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  522. BGChartFull.data?.notifyDataChanged()
  523. BGChartFull.notifyDataSetChanged()
  524. }
  525. func updateBasalGraph() {
  526. var dataIndex = 2
  527. BGChart.lineData?.dataSets[dataIndex].clear()
  528. BGChartFull.lineData?.dataSets[dataIndex].clear()
  529. var maxBasal = UserDefaultsRepository.minBasalScale.value
  530. var maxBasalSmall: Double = 0.0
  531. for i in 0..<basalData.count{
  532. let value = ChartDataEntry(x: Double(basalData[i].date), y: Double(basalData[i].basalRate), data: formatPillText(line1: String(basalData[i].basalRate), time: basalData[i].date))
  533. BGChart.data?.dataSets[dataIndex].addEntry(value)
  534. if UserDefaultsRepository.smallGraphTreatments.value {
  535. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  536. }
  537. if basalData[i].basalRate > maxBasal {
  538. maxBasal = basalData[i].basalRate
  539. }
  540. if basalData[i].basalRate > maxBasalSmall {
  541. maxBasalSmall = basalData[i].basalRate
  542. }
  543. }
  544. BGChart.leftAxis.axisMaximum = maxBasal
  545. BGChartFull.leftAxis.axisMaximum = maxBasalSmall
  546. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  547. BGChart.data?.notifyDataChanged()
  548. BGChart.notifyDataSetChanged()
  549. if UserDefaultsRepository.smallGraphTreatments.value {
  550. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  551. BGChartFull.data?.notifyDataChanged()
  552. BGChartFull.notifyDataSetChanged()
  553. }
  554. }
  555. func updateBasalScheduledGraph() {
  556. var dataIndex = 5
  557. BGChart.lineData?.dataSets[dataIndex].clear()
  558. BGChartFull.lineData?.dataSets[dataIndex].clear()
  559. for i in 0..<basalScheduleData.count{
  560. let value = ChartDataEntry(x: Double(basalScheduleData[i].date), y: Double(basalScheduleData[i].basalRate))
  561. BGChart.data?.dataSets[dataIndex].addEntry(value)
  562. if UserDefaultsRepository.smallGraphTreatments.value {
  563. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  564. }
  565. }
  566. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  567. BGChart.data?.notifyDataChanged()
  568. BGChart.notifyDataSetChanged()
  569. if UserDefaultsRepository.smallGraphTreatments.value {
  570. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  571. BGChartFull.data?.notifyDataChanged()
  572. BGChartFull.notifyDataSetChanged()
  573. }
  574. }
  575. func updateBolusGraph() {
  576. var dataIndex = 3
  577. var yTop: Double = 370
  578. var yBottom: Double = 345
  579. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  580. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  581. mainChart.clear()
  582. smallChart.clear()
  583. var colors = [NSUIColor]()
  584. for i in 0..<bolusData.count{
  585. let formatter = NumberFormatter()
  586. formatter.minimumFractionDigits = 0
  587. formatter.maximumFractionDigits = 2
  588. formatter.minimumIntegerDigits = 0
  589. // Check overlapping carbs to shift left if needed
  590. let bolusShift = findNextBolusTime(timeWithin: 240, needle: bolusData[i].date, haystack: bolusData, startingIndex: i)
  591. var dateTimeStamp = bolusData[i].date
  592. // Alpha colors for DIA
  593. let nowTime = dateTimeUtils.getNowTimeIntervalUTC()
  594. let diffTimeHours = (nowTime - dateTimeStamp) / 60 / 60
  595. if diffTimeHours <= 1 {
  596. colors.append(NSUIColor.systemBlue.withAlphaComponent(1.0))
  597. } else if diffTimeHours > 6 {
  598. colors.append(NSUIColor.systemBlue.withAlphaComponent(0.25))
  599. } else {
  600. let thisAlpha = 1.0 - (0.15 * diffTimeHours)
  601. colors.append(NSUIColor.systemBlue.withAlphaComponent(CGFloat(thisAlpha)))
  602. }
  603. if bolusShift {
  604. // Move it half the distance between BG readings
  605. dateTimeStamp = dateTimeStamp - 150
  606. }
  607. // skip if outside of visible area
  608. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  609. if dateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  610. let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(bolusData[i].sgv), data: formatter.string(from: NSNumber(value: bolusData[i].value)))
  611. mainChart.addEntry(dot)
  612. if UserDefaultsRepository.smallGraphTreatments.value {
  613. smallChart.addEntry(dot)
  614. }
  615. }
  616. // Set Colors
  617. let lineBolus = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  618. let lineBolusSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  619. lineBolus.colors.removeAll()
  620. lineBolus.circleColors.removeAll()
  621. lineBolusSmall.colors.removeAll()
  622. lineBolusSmall.circleColors.removeAll()
  623. if colors.count > 0 {
  624. for i in 0..<colors.count{
  625. mainChart.addColor(colors[i])
  626. mainChart.circleColors.append(colors[i])
  627. smallChart.addColor(colors[i])
  628. smallChart.circleColors.append(colors[i])
  629. }
  630. }
  631. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  632. BGChart.data?.notifyDataChanged()
  633. BGChart.notifyDataSetChanged()
  634. if UserDefaultsRepository.smallGraphTreatments.value {
  635. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  636. BGChartFull.data?.notifyDataChanged()
  637. BGChartFull.notifyDataSetChanged()
  638. }
  639. }
  640. func updateCarbGraph() {
  641. var dataIndex = 4
  642. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  643. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  644. mainChart.clear()
  645. smallChart.clear()
  646. var colors = [NSUIColor]()
  647. for i in 0..<carbData.count{
  648. let formatter = NumberFormatter()
  649. formatter.minimumFractionDigits = 0
  650. formatter.maximumFractionDigits = 2
  651. formatter.minimumIntegerDigits = 1
  652. var valueString: String = formatter.string(from: NSNumber(value: carbData[i].value))!
  653. var hours = 3
  654. if carbData[i].absorptionTime > 0 && UserDefaultsRepository.showAbsorption.value {
  655. hours = carbData[i].absorptionTime / 60
  656. valueString += " " + String(hours) + "h"
  657. }
  658. // Check overlapping carbs to shift left if needed
  659. let carbShift = findNextCarbTime(timeWithin: 250, needle: carbData[i].date, haystack: carbData, startingIndex: i)
  660. var dateTimeStamp = carbData[i].date
  661. // Alpha colors for DIA
  662. let nowTime = dateTimeUtils.getNowTimeIntervalUTC()
  663. let diffTimeHours = (nowTime - dateTimeStamp) / 60 / 60
  664. if diffTimeHours <= 0.5 {
  665. colors.append(NSUIColor.systemOrange.withAlphaComponent(1.0))
  666. } else if diffTimeHours > Double(hours) {
  667. colors.append(NSUIColor.systemOrange.withAlphaComponent(0.25))
  668. } else {
  669. let thisAlpha = 1.0 - ((0.75 / Double(hours)) * diffTimeHours)
  670. colors.append(NSUIColor.systemOrange.withAlphaComponent(CGFloat(thisAlpha)))
  671. }
  672. // skip if outside of visible area
  673. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  674. if dateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  675. if carbShift {
  676. dateTimeStamp = dateTimeStamp - 250
  677. }
  678. let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(carbData[i].sgv), data: valueString)
  679. BGChart.data?.dataSets[dataIndex].addEntry(dot)
  680. if UserDefaultsRepository.smallGraphTreatments.value {
  681. BGChartFull.data?.dataSets[dataIndex].addEntry(dot)
  682. }
  683. }
  684. // Set Colors
  685. let lineCarbs = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  686. let lineCarbsSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  687. lineCarbs.colors.removeAll()
  688. lineCarbs.circleColors.removeAll()
  689. lineCarbsSmall.colors.removeAll()
  690. lineCarbsSmall.circleColors.removeAll()
  691. if colors.count > 0 {
  692. for i in 0..<colors.count{
  693. mainChart.addColor(colors[i])
  694. mainChart.circleColors.append(colors[i])
  695. smallChart.addColor(colors[i])
  696. smallChart.circleColors.append(colors[i])
  697. }
  698. }
  699. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  700. BGChart.data?.notifyDataChanged()
  701. BGChart.notifyDataSetChanged()
  702. if UserDefaultsRepository.smallGraphTreatments.value {
  703. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  704. BGChartFull.data?.notifyDataChanged()
  705. BGChartFull.notifyDataSetChanged()
  706. }
  707. }
  708. func updateBGCheckGraph() {
  709. var dataIndex = 7
  710. BGChart.lineData?.dataSets[dataIndex].clear()
  711. BGChartFull.lineData?.dataSets[dataIndex].clear()
  712. for i in 0..<bgCheckData.count{
  713. let formatter = NumberFormatter()
  714. formatter.minimumFractionDigits = 0
  715. formatter.maximumFractionDigits = 2
  716. formatter.minimumIntegerDigits = 1
  717. // skip if outside of visible area
  718. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  719. if bgCheckData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  720. let value = ChartDataEntry(x: Double(bgCheckData[i].date), y: Double(bgCheckData[i].sgv), data: formatPillText(line1: bgUnits.toDisplayUnits(String(bgCheckData[i].sgv)), time: bgCheckData[i].date))
  721. BGChart.data?.dataSets[dataIndex].addEntry(value)
  722. if UserDefaultsRepository.smallGraphTreatments.value {
  723. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  724. }
  725. }
  726. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  727. BGChart.data?.notifyDataChanged()
  728. BGChart.notifyDataSetChanged()
  729. if UserDefaultsRepository.smallGraphTreatments.value {
  730. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  731. BGChartFull.data?.notifyDataChanged()
  732. BGChartFull.notifyDataSetChanged()
  733. }
  734. }
  735. func updateSuspendGraph() {
  736. var dataIndex = 8
  737. BGChart.lineData?.dataSets[dataIndex].clear()
  738. BGChartFull.lineData?.dataSets[dataIndex].clear()
  739. let thisData = suspendGraphData
  740. for i in 0..<thisData.count{
  741. // skip if outside of visible area
  742. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  743. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  744. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Suspend Pump", time: thisData[i].date))
  745. BGChart.data?.dataSets[dataIndex].addEntry(value)
  746. if UserDefaultsRepository.smallGraphTreatments.value {
  747. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  748. }
  749. }
  750. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  751. BGChart.data?.notifyDataChanged()
  752. BGChart.notifyDataSetChanged()
  753. if UserDefaultsRepository.smallGraphTreatments.value {
  754. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  755. BGChartFull.data?.notifyDataChanged()
  756. BGChartFull.notifyDataSetChanged()
  757. }
  758. }
  759. func updateResumeGraph() {
  760. var dataIndex = 9
  761. BGChart.lineData?.dataSets[dataIndex].clear()
  762. BGChartFull.lineData?.dataSets[dataIndex].clear()
  763. let thisData = resumeGraphData
  764. for i in 0..<thisData.count{
  765. // skip if outside of visible area
  766. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  767. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  768. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Resume Pump", time: thisData[i].date))
  769. BGChart.data?.dataSets[dataIndex].addEntry(value)
  770. if UserDefaultsRepository.smallGraphTreatments.value {
  771. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  772. }
  773. }
  774. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  775. BGChart.data?.notifyDataChanged()
  776. BGChart.notifyDataSetChanged()
  777. if UserDefaultsRepository.smallGraphTreatments.value {
  778. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  779. BGChartFull.data?.notifyDataChanged()
  780. BGChartFull.notifyDataSetChanged()
  781. }
  782. }
  783. func updateSensorStart() {
  784. var dataIndex = 10
  785. BGChart.lineData?.dataSets[dataIndex].clear()
  786. BGChartFull.lineData?.dataSets[dataIndex].clear()
  787. let thisData = sensorStartGraphData
  788. for i in 0..<thisData.count{
  789. // skip if outside of visible area
  790. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  791. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  792. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Start Sensor", time: thisData[i].date))
  793. BGChart.data?.dataSets[dataIndex].addEntry(value)
  794. if UserDefaultsRepository.smallGraphTreatments.value {
  795. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  796. }
  797. }
  798. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  799. BGChart.data?.notifyDataChanged()
  800. BGChart.notifyDataSetChanged()
  801. if UserDefaultsRepository.smallGraphTreatments.value {
  802. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  803. BGChartFull.data?.notifyDataChanged()
  804. BGChartFull.notifyDataSetChanged()
  805. }
  806. }
  807. func updateNotes() {
  808. var dataIndex = 11
  809. BGChart.lineData?.dataSets[dataIndex].clear()
  810. BGChartFull.lineData?.dataSets[dataIndex].clear()
  811. let thisData = noteGraphData
  812. for i in 0..<thisData.count{
  813. // skip if outside of visible area
  814. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  815. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  816. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: thisData[i].note, time: thisData[i].date))
  817. BGChart.data?.dataSets[dataIndex].addEntry(value)
  818. if UserDefaultsRepository.smallGraphTreatments.value {
  819. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  820. }
  821. }
  822. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  823. BGChart.data?.notifyDataChanged()
  824. BGChart.notifyDataSetChanged()
  825. if UserDefaultsRepository.smallGraphTreatments.value {
  826. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  827. BGChartFull.data?.notifyDataChanged()
  828. BGChartFull.notifyDataSetChanged()
  829. }
  830. }
  831. func createSmallBGGraph(){
  832. let entries = bgData
  833. var bgChartEntry = [ChartDataEntry]()
  834. var colors = [NSUIColor]()
  835. var maxBG: Float = UserDefaultsRepository.minBGScale.value
  836. let lineBG = LineChartDataSet(entries:bgChartEntry, label: "")
  837. lineBG.drawCirclesEnabled = false
  838. //line2.setDrawHighlightIndicators(false)
  839. lineBG.highlightEnabled = true
  840. lineBG.drawHorizontalHighlightIndicatorEnabled = false
  841. lineBG.drawVerticalHighlightIndicatorEnabled = false
  842. lineBG.highlightColor = NSUIColor.label
  843. lineBG.drawValuesEnabled = false
  844. lineBG.lineWidth = 1.5
  845. lineBG.axisDependency = YAxis.AxisDependency.right
  846. // Setup Prediction line details
  847. var predictionChartEntry = [ChartDataEntry]()
  848. let linePrediction = LineChartDataSet(entries:predictionChartEntry, label: "")
  849. linePrediction.drawCirclesEnabled = false
  850. //line2.setDrawHighlightIndicators(false)
  851. linePrediction.setColor(NSUIColor.systemPurple)
  852. linePrediction.highlightEnabled = true
  853. linePrediction.drawHorizontalHighlightIndicatorEnabled = false
  854. linePrediction.drawVerticalHighlightIndicatorEnabled = false
  855. linePrediction.highlightColor = NSUIColor.label
  856. linePrediction.drawValuesEnabled = false
  857. linePrediction.lineWidth = 1.5
  858. linePrediction.axisDependency = YAxis.AxisDependency.right
  859. // create Basal graph data
  860. var chartEntry = [ChartDataEntry]()
  861. var maxBasal = UserDefaultsRepository.minBasalScale.value
  862. let lineBasal = LineChartDataSet(entries:chartEntry, label: "")
  863. lineBasal.setDrawHighlightIndicators(false)
  864. lineBasal.setColor(NSUIColor.systemBlue, alpha: 0.5)
  865. lineBasal.lineWidth = 0
  866. lineBasal.drawFilledEnabled = true
  867. lineBasal.fillColor = NSUIColor.systemBlue
  868. lineBasal.fillAlpha = 0.35
  869. lineBasal.drawCirclesEnabled = false
  870. lineBasal.axisDependency = YAxis.AxisDependency.left
  871. lineBasal.highlightEnabled = false
  872. lineBasal.drawValuesEnabled = false
  873. lineBasal.fillFormatter = basalFillFormatter()
  874. // Boluses
  875. var chartEntryBolus = [ChartDataEntry]()
  876. let lineBolus = LineChartDataSet(entries:chartEntryBolus, label: "")
  877. lineBolus.circleRadius = 2
  878. lineBolus.circleColors = [NSUIColor.systemBlue.withAlphaComponent(0.75)]
  879. lineBolus.drawCircleHoleEnabled = false
  880. lineBolus.setDrawHighlightIndicators(false)
  881. lineBolus.setColor(NSUIColor.systemBlue, alpha: 1.0)
  882. lineBolus.lineWidth = 0
  883. lineBolus.axisDependency = YAxis.AxisDependency.right
  884. lineBolus.valueFormatter = ChartYDataValueFormatter()
  885. lineBolus.valueTextColor = NSUIColor.label
  886. lineBolus.fillColor = NSUIColor.systemBlue
  887. lineBolus.fillAlpha = 0.6
  888. lineBolus.drawCirclesEnabled = true
  889. lineBolus.drawFilledEnabled = false
  890. lineBolus.drawValuesEnabled = false
  891. lineBolus.highlightEnabled = false
  892. // Carbs
  893. var chartEntryCarbs = [ChartDataEntry]()
  894. let lineCarbs = LineChartDataSet(entries:chartEntryCarbs, label: "")
  895. lineCarbs.circleRadius = 2
  896. lineCarbs.circleColors = [NSUIColor.systemOrange.withAlphaComponent(0.75)]
  897. lineCarbs.drawCircleHoleEnabled = false
  898. lineCarbs.setDrawHighlightIndicators(false)
  899. lineCarbs.setColor(NSUIColor.systemBlue, alpha: 1.0)
  900. lineCarbs.lineWidth = 0
  901. lineCarbs.axisDependency = YAxis.AxisDependency.right
  902. lineCarbs.valueFormatter = ChartYDataValueFormatter()
  903. lineCarbs.valueTextColor = NSUIColor.label
  904. lineCarbs.fillColor = NSUIColor.systemOrange
  905. lineCarbs.fillAlpha = 0.6
  906. lineCarbs.drawCirclesEnabled = true
  907. lineCarbs.drawFilledEnabled = false
  908. lineCarbs.drawValuesEnabled = false
  909. lineCarbs.highlightEnabled = false
  910. // create Scheduled Basal graph data
  911. var chartBasalScheduledEntry = [ChartDataEntry]()
  912. let lineBasalScheduled = LineChartDataSet(entries:chartBasalScheduledEntry, label: "")
  913. lineBasalScheduled.setDrawHighlightIndicators(false)
  914. lineBasalScheduled.setColor(NSUIColor.systemBlue, alpha: 0.8)
  915. lineBasalScheduled.lineWidth = 0.5
  916. lineBasalScheduled.drawFilledEnabled = false
  917. lineBasalScheduled.drawCirclesEnabled = false
  918. lineBasalScheduled.axisDependency = YAxis.AxisDependency.left
  919. lineBasalScheduled.highlightEnabled = false
  920. lineBasalScheduled.drawValuesEnabled = false
  921. lineBasalScheduled.lineDashLengths = [2, 1]
  922. // create Override graph data
  923. var chartOverrideEntry = [ChartDataEntry]()
  924. let lineOverride = LineChartDataSet(entries:chartOverrideEntry, label: "")
  925. lineOverride.setDrawHighlightIndicators(false)
  926. lineOverride.lineWidth = 0
  927. lineOverride.drawFilledEnabled = true
  928. lineOverride.fillFormatter = OverrideFillFormatter()
  929. lineOverride.fillColor = NSUIColor.systemGreen
  930. lineOverride.fillAlpha = 0.6
  931. lineOverride.drawCirclesEnabled = false
  932. lineOverride.axisDependency = YAxis.AxisDependency.right
  933. lineOverride.highlightEnabled = true
  934. lineOverride.drawValuesEnabled = false
  935. // BG Check
  936. var chartEntryBGCheck = [ChartDataEntry]()
  937. let lineBGCheck = LineChartDataSet(entries:chartEntryBGCheck, label: "")
  938. lineBGCheck.circleRadius = 2
  939. lineBGCheck.circleColors = [NSUIColor.systemRed.withAlphaComponent(0.75)]
  940. lineBGCheck.drawCircleHoleEnabled = false
  941. lineBGCheck.setDrawHighlightIndicators(false)
  942. lineBGCheck.setColor(NSUIColor.systemRed, alpha: 1.0)
  943. lineBGCheck.drawCirclesEnabled = true
  944. lineBGCheck.lineWidth = 0
  945. lineBGCheck.highlightEnabled = false
  946. lineBGCheck.axisDependency = YAxis.AxisDependency.right
  947. lineBGCheck.valueFormatter = ChartYDataValueFormatter()
  948. lineBGCheck.drawValuesEnabled = false
  949. // Suspend Pump
  950. var chartEntrySuspend = [ChartDataEntry]()
  951. let lineSuspend = LineChartDataSet(entries:chartEntrySuspend, label: "")
  952. lineSuspend.circleRadius = 2
  953. lineSuspend.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
  954. lineSuspend.drawCircleHoleEnabled = false
  955. lineSuspend.setDrawHighlightIndicators(false)
  956. lineSuspend.setColor(NSUIColor.systemGray2, alpha: 1.0)
  957. lineSuspend.drawCirclesEnabled = true
  958. lineSuspend.lineWidth = 0
  959. lineSuspend.highlightEnabled = false
  960. lineSuspend.axisDependency = YAxis.AxisDependency.right
  961. lineSuspend.valueFormatter = ChartYDataValueFormatter()
  962. lineSuspend.drawValuesEnabled = false
  963. // Resume Pump
  964. var chartEntryResume = [ChartDataEntry]()
  965. let lineResume = LineChartDataSet(entries:chartEntryResume, label: "")
  966. lineResume.circleRadius = 2
  967. lineResume.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
  968. lineResume.drawCircleHoleEnabled = false
  969. lineResume.setDrawHighlightIndicators(false)
  970. lineResume.setColor(NSUIColor.systemGray4, alpha: 1.0)
  971. lineResume.drawCirclesEnabled = true
  972. lineResume.lineWidth = 0
  973. lineResume.highlightEnabled = false
  974. lineResume.axisDependency = YAxis.AxisDependency.right
  975. lineResume.valueFormatter = ChartYDataValueFormatter()
  976. lineResume.drawValuesEnabled = false
  977. // Sensor Start
  978. var chartEntrySensor = [ChartDataEntry]()
  979. let lineSensor = LineChartDataSet(entries:chartEntrySensor, label: "")
  980. lineSensor.circleRadius = 2
  981. lineSensor.circleColors = [NSUIColor.systemIndigo.withAlphaComponent(0.75)]
  982. lineSensor.drawCircleHoleEnabled = false
  983. lineSensor.setDrawHighlightIndicators(false)
  984. lineSensor.setColor(NSUIColor.systemGray3, alpha: 1.0)
  985. lineSensor.drawCirclesEnabled = true
  986. lineSensor.lineWidth = 0
  987. lineSensor.highlightEnabled = false
  988. lineSensor.axisDependency = YAxis.AxisDependency.right
  989. lineSensor.valueFormatter = ChartYDataValueFormatter()
  990. lineSensor.drawValuesEnabled = false
  991. // Notes
  992. var chartEntryNote = [ChartDataEntry]()
  993. let lineNote = LineChartDataSet(entries:chartEntryNote, label: "")
  994. lineNote.circleRadius = 2
  995. lineNote.circleColors = [NSUIColor.systemGray.withAlphaComponent(0.75)]
  996. lineNote.drawCircleHoleEnabled = false
  997. lineNote.setDrawHighlightIndicators(false)
  998. lineNote.setColor(NSUIColor.systemGray3, alpha: 1.0)
  999. lineNote.drawCirclesEnabled = true
  1000. lineNote.lineWidth = 0
  1001. lineNote.highlightEnabled = false
  1002. lineNote.axisDependency = YAxis.AxisDependency.right
  1003. lineNote.valueFormatter = ChartYDataValueFormatter()
  1004. lineNote.drawValuesEnabled = false
  1005. // Setup the chart data of all lines
  1006. let data = LineChartData()
  1007. data.append(lineBG) // Dataset 0
  1008. data.append(linePrediction) // Dataset 1
  1009. data.append(lineBasal) // Dataset 2
  1010. data.append(lineBolus) // Dataset 3
  1011. data.append(lineCarbs) // Dataset 4
  1012. data.append(lineBasalScheduled) // Dataset 5
  1013. data.append(lineOverride) // Dataset 6
  1014. data.append(lineBGCheck) // Dataset 7
  1015. data.append(lineSuspend) // Dataset 8
  1016. data.append(lineResume) // Dataset 9
  1017. data.append(lineSensor) // Dataset 10
  1018. data.append(lineNote) // Dataset 11
  1019. BGChartFull.highlightPerDragEnabled = true
  1020. BGChartFull.leftAxis.enabled = false
  1021. BGChartFull.leftAxis.axisMaximum = maxBasal
  1022. BGChartFull.leftAxis.axisMinimum = 0
  1023. BGChartFull.rightAxis.enabled = false
  1024. BGChartFull.rightAxis.axisMinimum = 0.0
  1025. BGChartFull.rightAxis.axisMaximum = Double(maxBG)
  1026. BGChartFull.xAxis.drawLabelsEnabled = false
  1027. BGChartFull.xAxis.drawGridLinesEnabled = false
  1028. BGChartFull.xAxis.drawAxisLineEnabled = false
  1029. BGChartFull.legend.enabled = false
  1030. BGChartFull.scaleYEnabled = false
  1031. BGChartFull.scaleXEnabled = false
  1032. BGChartFull.drawGridBackgroundEnabled = false
  1033. BGChartFull.data = data
  1034. }
  1035. func updateOverrideGraph() {
  1036. var dataIndex = 6
  1037. var yTop: Double = Double(topBG - 5)
  1038. var yBottom: Double = Double(topBG - 25)
  1039. var chart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  1040. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  1041. chart.clear()
  1042. smallChart.clear()
  1043. let thisData = overrideGraphData
  1044. var colors = [NSUIColor]()
  1045. for i in 0..<thisData.count{
  1046. let thisItem = thisData[i]
  1047. let multiplier = thisItem.insulNeedsScaleFactor as! Double * 100.0
  1048. var labelText = thisItem.reason + "\r\n"
  1049. labelText += String(Int(thisItem.insulNeedsScaleFactor * 100)) + "% "
  1050. if thisItem.correctionRange.count == 2 {
  1051. labelText += String(thisItem.correctionRange[0]) + "-" + String(thisItem.correctionRange[1])
  1052. }
  1053. if thisItem.enteredBy.count > 0 {
  1054. labelText += "\r\nEntered By: " + thisItem.enteredBy
  1055. }
  1056. // Start Dot
  1057. // Shift dots 30 seconds to create an empty 0 space between consecutive temps
  1058. let preStartDot = ChartDataEntry(x: Double(thisItem.date), y: yBottom, data: labelText)
  1059. BGChart.data?.dataSets[dataIndex].addEntry(preStartDot)
  1060. if UserDefaultsRepository.smallGraphTreatments.value {
  1061. BGChartFull.data?.dataSets[dataIndex].addEntry(preStartDot)
  1062. }
  1063. let startDot = ChartDataEntry(x: Double(thisItem.date + 1), y: yTop, data: labelText)
  1064. BGChart.data?.dataSets[dataIndex].addEntry(startDot)
  1065. if UserDefaultsRepository.smallGraphTreatments.value {
  1066. BGChartFull.data?.dataSets[dataIndex].addEntry(startDot)
  1067. }
  1068. // End Dot
  1069. let endDot = ChartDataEntry(x: Double(thisItem.endDate - 2), y: yTop, data: labelText)
  1070. BGChart.data?.dataSets[dataIndex].addEntry(endDot)
  1071. if UserDefaultsRepository.smallGraphTreatments.value {
  1072. BGChartFull.data?.dataSets[dataIndex].addEntry(endDot)
  1073. }
  1074. // Post end dot
  1075. let postEndDot = ChartDataEntry(x: Double(thisItem.endDate - 1), y: yBottom, data: labelText)
  1076. BGChart.data?.dataSets[dataIndex].addEntry(postEndDot)
  1077. if UserDefaultsRepository.smallGraphTreatments.value {
  1078. BGChartFull.data?.dataSets[dataIndex].addEntry(postEndDot)
  1079. }
  1080. }
  1081. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  1082. BGChart.data?.notifyDataChanged()
  1083. BGChart.notifyDataSetChanged()
  1084. if UserDefaultsRepository.smallGraphTreatments.value {
  1085. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  1086. BGChartFull.data?.notifyDataChanged()
  1087. BGChartFull.notifyDataSetChanged()
  1088. }
  1089. }
  1090. func formatPillText(line1: String, time: TimeInterval) -> String {
  1091. let dateFormatter = DateFormatter()
  1092. //let timezoneOffset = TimeZone.current.secondsFromGMT()
  1093. //let epochTimezoneOffset = value + Double(timezoneOffset)
  1094. if dateTimeUtils.is24Hour() {
  1095. dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm")
  1096. } else {
  1097. dateFormatter.setLocalizedDateFormatFromTemplate("hh:mm")
  1098. }
  1099. //let date = Date(timeIntervalSince1970: epochTimezoneOffset)
  1100. let date = Date(timeIntervalSince1970: time)
  1101. let formattedDate = dateFormatter.string(from: date)
  1102. return line1 + "\r\n" + formattedDate
  1103. }
  1104. }