Graphs.swift 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277
  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() {
  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. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: print prediction") }
  482. var colors = [NSUIColor]()
  483. let maxBGOffset: Float = 20
  484. for i in 0..<predictionData.count {
  485. var predictionVal = Double(predictionData[i].sgv)
  486. if Float(predictionVal) > topBG - maxBGOffset {
  487. topBG = Float(predictionVal) + maxBGOffset
  488. }
  489. if i == 0 {
  490. if UserDefaultsRepository.showDots.value {
  491. colors.append(NSUIColor.systemPurple.withAlphaComponent(0.0))
  492. } else {
  493. colors.append(NSUIColor.systemPurple.withAlphaComponent(1.0))
  494. }
  495. } else if predictionVal > 400 {
  496. predictionVal = 400
  497. colors.append(NSUIColor.systemYellow)
  498. } else if predictionVal < 0 {
  499. predictionVal = 0
  500. colors.append(NSUIColor.systemRed)
  501. } else {
  502. colors.append(NSUIColor.systemPurple)
  503. }
  504. let value = ChartDataEntry(x: predictionData[i].date, y: predictionVal, data: formatPillText(line1: bgUnits.toDisplayUnits(String(predictionData[i].sgv)), time: predictionData[i].date))
  505. mainChart.addEntry(value)
  506. smallChart.addEntry(value)
  507. }
  508. smallChart.circleColors.removeAll()
  509. smallChart.colors.removeAll()
  510. mainChart.colors.removeAll()
  511. mainChart.circleColors.removeAll()
  512. if colors.count > 0 {
  513. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: prediction colors") }
  514. for i in 0..<colors.count{
  515. mainChart.addColor(colors[i])
  516. mainChart.circleColors.append(colors[i])
  517. smallChart.addColor(colors[i])
  518. smallChart.circleColors.append(colors[i])
  519. }
  520. }
  521. BGChart.rightAxis.axisMaximum = Double(topBG)
  522. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  523. BGChart.data?.notifyDataChanged()
  524. BGChart.notifyDataSetChanged()
  525. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  526. BGChartFull.data?.notifyDataChanged()
  527. BGChartFull.notifyDataSetChanged()
  528. }
  529. func updateBasalGraph() {
  530. var dataIndex = 2
  531. BGChart.lineData?.dataSets[dataIndex].clear()
  532. BGChartFull.lineData?.dataSets[dataIndex].clear()
  533. var maxBasal = UserDefaultsRepository.minBasalScale.value
  534. var maxBasalSmall: Double = 0.0
  535. for i in 0..<basalData.count{
  536. let value = ChartDataEntry(x: Double(basalData[i].date), y: Double(basalData[i].basalRate), data: formatPillText(line1: String(basalData[i].basalRate), time: basalData[i].date))
  537. BGChart.data?.dataSets[dataIndex].addEntry(value)
  538. if UserDefaultsRepository.smallGraphTreatments.value {
  539. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  540. }
  541. if basalData[i].basalRate > maxBasal {
  542. maxBasal = basalData[i].basalRate
  543. }
  544. if basalData[i].basalRate > maxBasalSmall {
  545. maxBasalSmall = basalData[i].basalRate
  546. }
  547. }
  548. BGChart.leftAxis.axisMaximum = maxBasal
  549. BGChartFull.leftAxis.axisMaximum = maxBasalSmall
  550. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  551. BGChart.data?.notifyDataChanged()
  552. BGChart.notifyDataSetChanged()
  553. if UserDefaultsRepository.smallGraphTreatments.value {
  554. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  555. BGChartFull.data?.notifyDataChanged()
  556. BGChartFull.notifyDataSetChanged()
  557. }
  558. }
  559. func updateBasalScheduledGraph() {
  560. var dataIndex = 5
  561. BGChart.lineData?.dataSets[dataIndex].clear()
  562. BGChartFull.lineData?.dataSets[dataIndex].clear()
  563. for i in 0..<basalScheduleData.count{
  564. let value = ChartDataEntry(x: Double(basalScheduleData[i].date), y: Double(basalScheduleData[i].basalRate))
  565. BGChart.data?.dataSets[dataIndex].addEntry(value)
  566. if UserDefaultsRepository.smallGraphTreatments.value {
  567. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  568. }
  569. }
  570. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  571. BGChart.data?.notifyDataChanged()
  572. BGChart.notifyDataSetChanged()
  573. if UserDefaultsRepository.smallGraphTreatments.value {
  574. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  575. BGChartFull.data?.notifyDataChanged()
  576. BGChartFull.notifyDataSetChanged()
  577. }
  578. }
  579. func updateBolusGraph() {
  580. var dataIndex = 3
  581. var yTop: Double = 370
  582. var yBottom: Double = 345
  583. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  584. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  585. mainChart.clear()
  586. smallChart.clear()
  587. var colors = [NSUIColor]()
  588. for i in 0..<bolusData.count{
  589. let formatter = NumberFormatter()
  590. formatter.minimumFractionDigits = 0
  591. formatter.maximumFractionDigits = 2
  592. formatter.minimumIntegerDigits = 0
  593. // Check overlapping carbs to shift left if needed
  594. let bolusShift = findNextBolusTime(timeWithin: 240, needle: bolusData[i].date, haystack: bolusData, startingIndex: i)
  595. var dateTimeStamp = bolusData[i].date
  596. // Alpha colors for DIA
  597. let nowTime = dateTimeUtils.getNowTimeIntervalUTC()
  598. let diffTimeHours = (nowTime - dateTimeStamp) / 60 / 60
  599. if diffTimeHours <= 1 {
  600. colors.append(NSUIColor.systemBlue.withAlphaComponent(1.0))
  601. } else if diffTimeHours > 6 {
  602. colors.append(NSUIColor.systemBlue.withAlphaComponent(0.25))
  603. } else {
  604. let thisAlpha = 1.0 - (0.15 * diffTimeHours)
  605. colors.append(NSUIColor.systemBlue.withAlphaComponent(CGFloat(thisAlpha)))
  606. }
  607. if bolusShift {
  608. // Move it half the distance between BG readings
  609. dateTimeStamp = dateTimeStamp - 150
  610. }
  611. // skip if outside of visible area
  612. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  613. if dateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  614. let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(bolusData[i].sgv), data: formatter.string(from: NSNumber(value: bolusData[i].value)))
  615. mainChart.addEntry(dot)
  616. if UserDefaultsRepository.smallGraphTreatments.value {
  617. smallChart.addEntry(dot)
  618. }
  619. }
  620. // Set Colors
  621. let lineBolus = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  622. let lineBolusSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  623. lineBolus.colors.removeAll()
  624. lineBolus.circleColors.removeAll()
  625. lineBolusSmall.colors.removeAll()
  626. lineBolusSmall.circleColors.removeAll()
  627. if colors.count > 0 {
  628. for i in 0..<colors.count{
  629. mainChart.addColor(colors[i])
  630. mainChart.circleColors.append(colors[i])
  631. smallChart.addColor(colors[i])
  632. smallChart.circleColors.append(colors[i])
  633. }
  634. }
  635. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  636. BGChart.data?.notifyDataChanged()
  637. BGChart.notifyDataSetChanged()
  638. if UserDefaultsRepository.smallGraphTreatments.value {
  639. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  640. BGChartFull.data?.notifyDataChanged()
  641. BGChartFull.notifyDataSetChanged()
  642. }
  643. }
  644. func updateCarbGraph() {
  645. var dataIndex = 4
  646. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  647. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  648. mainChart.clear()
  649. smallChart.clear()
  650. var colors = [NSUIColor]()
  651. for i in 0..<carbData.count{
  652. let formatter = NumberFormatter()
  653. formatter.minimumFractionDigits = 0
  654. formatter.maximumFractionDigits = 2
  655. formatter.minimumIntegerDigits = 1
  656. var valueString: String = formatter.string(from: NSNumber(value: carbData[i].value))!
  657. var hours = 3
  658. if carbData[i].absorptionTime > 0 && UserDefaultsRepository.showAbsorption.value {
  659. hours = carbData[i].absorptionTime / 60
  660. valueString += " " + String(hours) + "h"
  661. }
  662. // Check overlapping carbs to shift left if needed
  663. let carbShift = findNextCarbTime(timeWithin: 250, needle: carbData[i].date, haystack: carbData, startingIndex: i)
  664. var dateTimeStamp = carbData[i].date
  665. // Alpha colors for DIA
  666. let nowTime = dateTimeUtils.getNowTimeIntervalUTC()
  667. let diffTimeHours = (nowTime - dateTimeStamp) / 60 / 60
  668. if diffTimeHours <= 0.5 {
  669. colors.append(NSUIColor.systemOrange.withAlphaComponent(1.0))
  670. } else if diffTimeHours > Double(hours) {
  671. colors.append(NSUIColor.systemOrange.withAlphaComponent(0.25))
  672. } else {
  673. let thisAlpha = 1.0 - ((0.75 / Double(hours)) * diffTimeHours)
  674. colors.append(NSUIColor.systemOrange.withAlphaComponent(CGFloat(thisAlpha)))
  675. }
  676. // skip if outside of visible area
  677. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  678. if dateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  679. if carbShift {
  680. dateTimeStamp = dateTimeStamp - 250
  681. }
  682. let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(carbData[i].sgv), data: valueString)
  683. BGChart.data?.dataSets[dataIndex].addEntry(dot)
  684. if UserDefaultsRepository.smallGraphTreatments.value {
  685. BGChartFull.data?.dataSets[dataIndex].addEntry(dot)
  686. }
  687. }
  688. // Set Colors
  689. let lineCarbs = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  690. let lineCarbsSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  691. lineCarbs.colors.removeAll()
  692. lineCarbs.circleColors.removeAll()
  693. lineCarbsSmall.colors.removeAll()
  694. lineCarbsSmall.circleColors.removeAll()
  695. if colors.count > 0 {
  696. for i in 0..<colors.count{
  697. mainChart.addColor(colors[i])
  698. mainChart.circleColors.append(colors[i])
  699. smallChart.addColor(colors[i])
  700. smallChart.circleColors.append(colors[i])
  701. }
  702. }
  703. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  704. BGChart.data?.notifyDataChanged()
  705. BGChart.notifyDataSetChanged()
  706. if UserDefaultsRepository.smallGraphTreatments.value {
  707. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  708. BGChartFull.data?.notifyDataChanged()
  709. BGChartFull.notifyDataSetChanged()
  710. }
  711. }
  712. func updateBGCheckGraph() {
  713. var dataIndex = 7
  714. BGChart.lineData?.dataSets[dataIndex].clear()
  715. BGChartFull.lineData?.dataSets[dataIndex].clear()
  716. for i in 0..<bgCheckData.count{
  717. let formatter = NumberFormatter()
  718. formatter.minimumFractionDigits = 0
  719. formatter.maximumFractionDigits = 2
  720. formatter.minimumIntegerDigits = 1
  721. // skip if outside of visible area
  722. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  723. if bgCheckData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  724. 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))
  725. BGChart.data?.dataSets[dataIndex].addEntry(value)
  726. if UserDefaultsRepository.smallGraphTreatments.value {
  727. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  728. }
  729. }
  730. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  731. BGChart.data?.notifyDataChanged()
  732. BGChart.notifyDataSetChanged()
  733. if UserDefaultsRepository.smallGraphTreatments.value {
  734. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  735. BGChartFull.data?.notifyDataChanged()
  736. BGChartFull.notifyDataSetChanged()
  737. }
  738. }
  739. func updateSuspendGraph() {
  740. var dataIndex = 8
  741. BGChart.lineData?.dataSets[dataIndex].clear()
  742. BGChartFull.lineData?.dataSets[dataIndex].clear()
  743. let thisData = suspendGraphData
  744. for i in 0..<thisData.count{
  745. // skip if outside of visible area
  746. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  747. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  748. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Suspend Pump", time: thisData[i].date))
  749. BGChart.data?.dataSets[dataIndex].addEntry(value)
  750. if UserDefaultsRepository.smallGraphTreatments.value {
  751. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  752. }
  753. }
  754. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  755. BGChart.data?.notifyDataChanged()
  756. BGChart.notifyDataSetChanged()
  757. if UserDefaultsRepository.smallGraphTreatments.value {
  758. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  759. BGChartFull.data?.notifyDataChanged()
  760. BGChartFull.notifyDataSetChanged()
  761. }
  762. }
  763. func updateResumeGraph() {
  764. var dataIndex = 9
  765. BGChart.lineData?.dataSets[dataIndex].clear()
  766. BGChartFull.lineData?.dataSets[dataIndex].clear()
  767. let thisData = resumeGraphData
  768. for i in 0..<thisData.count{
  769. // skip if outside of visible area
  770. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  771. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  772. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Resume Pump", time: thisData[i].date))
  773. BGChart.data?.dataSets[dataIndex].addEntry(value)
  774. if UserDefaultsRepository.smallGraphTreatments.value {
  775. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  776. }
  777. }
  778. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  779. BGChart.data?.notifyDataChanged()
  780. BGChart.notifyDataSetChanged()
  781. if UserDefaultsRepository.smallGraphTreatments.value {
  782. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  783. BGChartFull.data?.notifyDataChanged()
  784. BGChartFull.notifyDataSetChanged()
  785. }
  786. }
  787. func updateSensorStart() {
  788. var dataIndex = 10
  789. BGChart.lineData?.dataSets[dataIndex].clear()
  790. BGChartFull.lineData?.dataSets[dataIndex].clear()
  791. let thisData = sensorStartGraphData
  792. for i in 0..<thisData.count{
  793. // skip if outside of visible area
  794. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  795. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  796. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Start Sensor", time: thisData[i].date))
  797. BGChart.data?.dataSets[dataIndex].addEntry(value)
  798. if UserDefaultsRepository.smallGraphTreatments.value {
  799. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  800. }
  801. }
  802. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  803. BGChart.data?.notifyDataChanged()
  804. BGChart.notifyDataSetChanged()
  805. if UserDefaultsRepository.smallGraphTreatments.value {
  806. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  807. BGChartFull.data?.notifyDataChanged()
  808. BGChartFull.notifyDataSetChanged()
  809. }
  810. }
  811. func updateNotes() {
  812. var dataIndex = 11
  813. BGChart.lineData?.dataSets[dataIndex].clear()
  814. BGChartFull.lineData?.dataSets[dataIndex].clear()
  815. let thisData = noteGraphData
  816. for i in 0..<thisData.count{
  817. // skip if outside of visible area
  818. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  819. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  820. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: thisData[i].note, time: thisData[i].date))
  821. BGChart.data?.dataSets[dataIndex].addEntry(value)
  822. if UserDefaultsRepository.smallGraphTreatments.value {
  823. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  824. }
  825. }
  826. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  827. BGChart.data?.notifyDataChanged()
  828. BGChart.notifyDataSetChanged()
  829. if UserDefaultsRepository.smallGraphTreatments.value {
  830. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  831. BGChartFull.data?.notifyDataChanged()
  832. BGChartFull.notifyDataSetChanged()
  833. }
  834. }
  835. func createSmallBGGraph(){
  836. let entries = bgData
  837. var bgChartEntry = [ChartDataEntry]()
  838. var colors = [NSUIColor]()
  839. var maxBG: Float = UserDefaultsRepository.minBGScale.value
  840. let lineBG = LineChartDataSet(entries:bgChartEntry, label: "")
  841. lineBG.drawCirclesEnabled = false
  842. //line2.setDrawHighlightIndicators(false)
  843. lineBG.highlightEnabled = true
  844. lineBG.drawHorizontalHighlightIndicatorEnabled = false
  845. lineBG.drawVerticalHighlightIndicatorEnabled = false
  846. lineBG.highlightColor = NSUIColor.label
  847. lineBG.drawValuesEnabled = false
  848. lineBG.lineWidth = 1.5
  849. lineBG.axisDependency = YAxis.AxisDependency.right
  850. // Setup Prediction line details
  851. var predictionChartEntry = [ChartDataEntry]()
  852. let linePrediction = LineChartDataSet(entries:predictionChartEntry, label: "")
  853. linePrediction.drawCirclesEnabled = false
  854. //line2.setDrawHighlightIndicators(false)
  855. linePrediction.setColor(NSUIColor.systemPurple)
  856. linePrediction.highlightEnabled = true
  857. linePrediction.drawHorizontalHighlightIndicatorEnabled = false
  858. linePrediction.drawVerticalHighlightIndicatorEnabled = false
  859. linePrediction.highlightColor = NSUIColor.label
  860. linePrediction.drawValuesEnabled = false
  861. linePrediction.lineWidth = 1.5
  862. linePrediction.axisDependency = YAxis.AxisDependency.right
  863. // create Basal graph data
  864. var chartEntry = [ChartDataEntry]()
  865. var maxBasal = UserDefaultsRepository.minBasalScale.value
  866. let lineBasal = LineChartDataSet(entries:chartEntry, label: "")
  867. lineBasal.setDrawHighlightIndicators(false)
  868. lineBasal.setColor(NSUIColor.systemBlue, alpha: 0.5)
  869. lineBasal.lineWidth = 0
  870. lineBasal.drawFilledEnabled = true
  871. lineBasal.fillColor = NSUIColor.systemBlue
  872. lineBasal.fillAlpha = 0.35
  873. lineBasal.drawCirclesEnabled = false
  874. lineBasal.axisDependency = YAxis.AxisDependency.left
  875. lineBasal.highlightEnabled = false
  876. lineBasal.drawValuesEnabled = false
  877. lineBasal.fillFormatter = basalFillFormatter()
  878. // Boluses
  879. var chartEntryBolus = [ChartDataEntry]()
  880. let lineBolus = LineChartDataSet(entries:chartEntryBolus, label: "")
  881. lineBolus.circleRadius = 2
  882. lineBolus.circleColors = [NSUIColor.systemBlue.withAlphaComponent(0.75)]
  883. lineBolus.drawCircleHoleEnabled = false
  884. lineBolus.setDrawHighlightIndicators(false)
  885. lineBolus.setColor(NSUIColor.systemBlue, alpha: 1.0)
  886. lineBolus.lineWidth = 0
  887. lineBolus.axisDependency = YAxis.AxisDependency.right
  888. lineBolus.valueFormatter = ChartYDataValueFormatter()
  889. lineBolus.valueTextColor = NSUIColor.label
  890. lineBolus.fillColor = NSUIColor.systemBlue
  891. lineBolus.fillAlpha = 0.6
  892. lineBolus.drawCirclesEnabled = true
  893. lineBolus.drawFilledEnabled = false
  894. lineBolus.drawValuesEnabled = false
  895. lineBolus.highlightEnabled = false
  896. // Carbs
  897. var chartEntryCarbs = [ChartDataEntry]()
  898. let lineCarbs = LineChartDataSet(entries:chartEntryCarbs, label: "")
  899. lineCarbs.circleRadius = 2
  900. lineCarbs.circleColors = [NSUIColor.systemOrange.withAlphaComponent(0.75)]
  901. lineCarbs.drawCircleHoleEnabled = false
  902. lineCarbs.setDrawHighlightIndicators(false)
  903. lineCarbs.setColor(NSUIColor.systemBlue, alpha: 1.0)
  904. lineCarbs.lineWidth = 0
  905. lineCarbs.axisDependency = YAxis.AxisDependency.right
  906. lineCarbs.valueFormatter = ChartYDataValueFormatter()
  907. lineCarbs.valueTextColor = NSUIColor.label
  908. lineCarbs.fillColor = NSUIColor.systemOrange
  909. lineCarbs.fillAlpha = 0.6
  910. lineCarbs.drawCirclesEnabled = true
  911. lineCarbs.drawFilledEnabled = false
  912. lineCarbs.drawValuesEnabled = false
  913. lineCarbs.highlightEnabled = false
  914. // create Scheduled Basal graph data
  915. var chartBasalScheduledEntry = [ChartDataEntry]()
  916. let lineBasalScheduled = LineChartDataSet(entries:chartBasalScheduledEntry, label: "")
  917. lineBasalScheduled.setDrawHighlightIndicators(false)
  918. lineBasalScheduled.setColor(NSUIColor.systemBlue, alpha: 0.8)
  919. lineBasalScheduled.lineWidth = 0.5
  920. lineBasalScheduled.drawFilledEnabled = false
  921. lineBasalScheduled.drawCirclesEnabled = false
  922. lineBasalScheduled.axisDependency = YAxis.AxisDependency.left
  923. lineBasalScheduled.highlightEnabled = false
  924. lineBasalScheduled.drawValuesEnabled = false
  925. lineBasalScheduled.lineDashLengths = [2, 1]
  926. // create Override graph data
  927. var chartOverrideEntry = [ChartDataEntry]()
  928. let lineOverride = LineChartDataSet(entries:chartOverrideEntry, label: "")
  929. lineOverride.setDrawHighlightIndicators(false)
  930. lineOverride.lineWidth = 0
  931. lineOverride.drawFilledEnabled = true
  932. lineOverride.fillFormatter = OverrideFillFormatter()
  933. lineOverride.fillColor = NSUIColor.systemGreen
  934. lineOverride.fillAlpha = 0.6
  935. lineOverride.drawCirclesEnabled = false
  936. lineOverride.axisDependency = YAxis.AxisDependency.right
  937. lineOverride.highlightEnabled = true
  938. lineOverride.drawValuesEnabled = false
  939. // BG Check
  940. var chartEntryBGCheck = [ChartDataEntry]()
  941. let lineBGCheck = LineChartDataSet(entries:chartEntryBGCheck, label: "")
  942. lineBGCheck.circleRadius = 2
  943. lineBGCheck.circleColors = [NSUIColor.systemRed.withAlphaComponent(0.75)]
  944. lineBGCheck.drawCircleHoleEnabled = false
  945. lineBGCheck.setDrawHighlightIndicators(false)
  946. lineBGCheck.setColor(NSUIColor.systemRed, alpha: 1.0)
  947. lineBGCheck.drawCirclesEnabled = true
  948. lineBGCheck.lineWidth = 0
  949. lineBGCheck.highlightEnabled = false
  950. lineBGCheck.axisDependency = YAxis.AxisDependency.right
  951. lineBGCheck.valueFormatter = ChartYDataValueFormatter()
  952. lineBGCheck.drawValuesEnabled = false
  953. // Suspend Pump
  954. var chartEntrySuspend = [ChartDataEntry]()
  955. let lineSuspend = LineChartDataSet(entries:chartEntrySuspend, label: "")
  956. lineSuspend.circleRadius = 2
  957. lineSuspend.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
  958. lineSuspend.drawCircleHoleEnabled = false
  959. lineSuspend.setDrawHighlightIndicators(false)
  960. lineSuspend.setColor(NSUIColor.systemGray2, alpha: 1.0)
  961. lineSuspend.drawCirclesEnabled = true
  962. lineSuspend.lineWidth = 0
  963. lineSuspend.highlightEnabled = false
  964. lineSuspend.axisDependency = YAxis.AxisDependency.right
  965. lineSuspend.valueFormatter = ChartYDataValueFormatter()
  966. lineSuspend.drawValuesEnabled = false
  967. // Resume Pump
  968. var chartEntryResume = [ChartDataEntry]()
  969. let lineResume = LineChartDataSet(entries:chartEntryResume, label: "")
  970. lineResume.circleRadius = 2
  971. lineResume.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
  972. lineResume.drawCircleHoleEnabled = false
  973. lineResume.setDrawHighlightIndicators(false)
  974. lineResume.setColor(NSUIColor.systemGray4, alpha: 1.0)
  975. lineResume.drawCirclesEnabled = true
  976. lineResume.lineWidth = 0
  977. lineResume.highlightEnabled = false
  978. lineResume.axisDependency = YAxis.AxisDependency.right
  979. lineResume.valueFormatter = ChartYDataValueFormatter()
  980. lineResume.drawValuesEnabled = false
  981. // Sensor Start
  982. var chartEntrySensor = [ChartDataEntry]()
  983. let lineSensor = LineChartDataSet(entries:chartEntrySensor, label: "")
  984. lineSensor.circleRadius = 2
  985. lineSensor.circleColors = [NSUIColor.systemIndigo.withAlphaComponent(0.75)]
  986. lineSensor.drawCircleHoleEnabled = false
  987. lineSensor.setDrawHighlightIndicators(false)
  988. lineSensor.setColor(NSUIColor.systemGray3, alpha: 1.0)
  989. lineSensor.drawCirclesEnabled = true
  990. lineSensor.lineWidth = 0
  991. lineSensor.highlightEnabled = false
  992. lineSensor.axisDependency = YAxis.AxisDependency.right
  993. lineSensor.valueFormatter = ChartYDataValueFormatter()
  994. lineSensor.drawValuesEnabled = false
  995. // Notes
  996. var chartEntryNote = [ChartDataEntry]()
  997. let lineNote = LineChartDataSet(entries:chartEntryNote, label: "")
  998. lineNote.circleRadius = 2
  999. lineNote.circleColors = [NSUIColor.systemGray.withAlphaComponent(0.75)]
  1000. lineNote.drawCircleHoleEnabled = false
  1001. lineNote.setDrawHighlightIndicators(false)
  1002. lineNote.setColor(NSUIColor.systemGray3, alpha: 1.0)
  1003. lineNote.drawCirclesEnabled = true
  1004. lineNote.lineWidth = 0
  1005. lineNote.highlightEnabled = false
  1006. lineNote.axisDependency = YAxis.AxisDependency.right
  1007. lineNote.valueFormatter = ChartYDataValueFormatter()
  1008. lineNote.drawValuesEnabled = false
  1009. // Setup the chart data of all lines
  1010. let data = LineChartData()
  1011. data.append(lineBG) // Dataset 0
  1012. data.append(linePrediction) // Dataset 1
  1013. data.append(lineBasal) // Dataset 2
  1014. data.append(lineBolus) // Dataset 3
  1015. data.append(lineCarbs) // Dataset 4
  1016. data.append(lineBasalScheduled) // Dataset 5
  1017. data.append(lineOverride) // Dataset 6
  1018. data.append(lineBGCheck) // Dataset 7
  1019. data.append(lineSuspend) // Dataset 8
  1020. data.append(lineResume) // Dataset 9
  1021. data.append(lineSensor) // Dataset 10
  1022. data.append(lineNote) // Dataset 11
  1023. BGChartFull.highlightPerDragEnabled = true
  1024. BGChartFull.leftAxis.enabled = false
  1025. BGChartFull.leftAxis.axisMaximum = maxBasal
  1026. BGChartFull.leftAxis.axisMinimum = 0
  1027. BGChartFull.rightAxis.enabled = false
  1028. BGChartFull.rightAxis.axisMinimum = 0.0
  1029. BGChartFull.rightAxis.axisMaximum = Double(maxBG)
  1030. BGChartFull.xAxis.drawLabelsEnabled = false
  1031. BGChartFull.xAxis.drawGridLinesEnabled = false
  1032. BGChartFull.xAxis.drawAxisLineEnabled = false
  1033. BGChartFull.legend.enabled = false
  1034. BGChartFull.scaleYEnabled = false
  1035. BGChartFull.scaleXEnabled = false
  1036. BGChartFull.drawGridBackgroundEnabled = false
  1037. BGChartFull.data = data
  1038. }
  1039. func updateOverrideGraph() {
  1040. var dataIndex = 6
  1041. var yTop: Double = Double(topBG - 5)
  1042. var yBottom: Double = Double(topBG - 25)
  1043. var chart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  1044. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  1045. chart.clear()
  1046. smallChart.clear()
  1047. let thisData = overrideGraphData
  1048. var colors = [NSUIColor]()
  1049. for i in 0..<thisData.count{
  1050. let thisItem = thisData[i]
  1051. let multiplier = thisItem.insulNeedsScaleFactor as! Double * 100.0
  1052. var labelText = thisItem.reason + "\r\n"
  1053. labelText += String(Int(thisItem.insulNeedsScaleFactor * 100)) + "% "
  1054. if thisItem.correctionRange.count == 2 {
  1055. labelText += String(thisItem.correctionRange[0]) + "-" + String(thisItem.correctionRange[1])
  1056. }
  1057. if thisItem.enteredBy.count > 0 {
  1058. labelText += "\r\nEntered By: " + thisItem.enteredBy
  1059. }
  1060. // Start Dot
  1061. // Shift dots 30 seconds to create an empty 0 space between consecutive temps
  1062. let preStartDot = ChartDataEntry(x: Double(thisItem.date), y: yBottom, data: labelText)
  1063. BGChart.data?.dataSets[dataIndex].addEntry(preStartDot)
  1064. if UserDefaultsRepository.smallGraphTreatments.value {
  1065. BGChartFull.data?.dataSets[dataIndex].addEntry(preStartDot)
  1066. }
  1067. let startDot = ChartDataEntry(x: Double(thisItem.date + 1), y: yTop, data: labelText)
  1068. BGChart.data?.dataSets[dataIndex].addEntry(startDot)
  1069. if UserDefaultsRepository.smallGraphTreatments.value {
  1070. BGChartFull.data?.dataSets[dataIndex].addEntry(startDot)
  1071. }
  1072. // End Dot
  1073. let endDot = ChartDataEntry(x: Double(thisItem.endDate - 2), y: yTop, data: labelText)
  1074. BGChart.data?.dataSets[dataIndex].addEntry(endDot)
  1075. if UserDefaultsRepository.smallGraphTreatments.value {
  1076. BGChartFull.data?.dataSets[dataIndex].addEntry(endDot)
  1077. }
  1078. // Post end dot
  1079. let postEndDot = ChartDataEntry(x: Double(thisItem.endDate - 1), y: yBottom, data: labelText)
  1080. BGChart.data?.dataSets[dataIndex].addEntry(postEndDot)
  1081. if UserDefaultsRepository.smallGraphTreatments.value {
  1082. BGChartFull.data?.dataSets[dataIndex].addEntry(postEndDot)
  1083. }
  1084. }
  1085. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  1086. BGChart.data?.notifyDataChanged()
  1087. BGChart.notifyDataSetChanged()
  1088. if UserDefaultsRepository.smallGraphTreatments.value {
  1089. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  1090. BGChartFull.data?.notifyDataChanged()
  1091. BGChartFull.notifyDataSetChanged()
  1092. }
  1093. }
  1094. func formatPillText(line1: String, time: TimeInterval) -> String {
  1095. let dateFormatter = DateFormatter()
  1096. //let timezoneOffset = TimeZone.current.secondsFromGMT()
  1097. //let epochTimezoneOffset = value + Double(timezoneOffset)
  1098. if dateTimeUtils.is24Hour() {
  1099. dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm")
  1100. } else {
  1101. dateFormatter.setLocalizedDateFormatFromTemplate("hh:mm")
  1102. }
  1103. //let date = Date(timeIntervalSince1970: epochTimezoneOffset)
  1104. let date = Date(timeIntervalSince1970: time)
  1105. let formattedDate = dateFormatter.string(from: date)
  1106. return line1 + "\r\n" + formattedDate
  1107. }
  1108. }