Graphs.swift 54 KB

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