Graphs.swift 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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: 10, top: 10, right: 10, 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.showDIALines.value {
  315. for i in 1..<7 {
  316. let ul = ChartLimitLine()
  317. ul.limit = Double(dateTimeUtils.getNowTimeIntervalUTC() - Double(i * 60 * 60))
  318. ul.lineColor = NSUIColor.systemGray.withAlphaComponent(0.3)
  319. let dash = 10.0 - Double(i)
  320. let space = 5.0 + Double(i)
  321. ul.lineDashLengths = [CGFloat(dash), CGFloat(space)]
  322. ul.lineWidth = 1
  323. BGChart.xAxis.addLimitLine(ul)
  324. }
  325. }
  326. }
  327. func createMidnightLines() {
  328. // Draw a line at midnight: useful when showing multiple days of data
  329. if UserDefaultsRepository.showMidnightLines.value {
  330. var midnightTimeInterval = dateTimeUtils.getTimeIntervalMidnightToday()
  331. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  332. let graphStart = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
  333. while midnightTimeInterval > graphStart {
  334. // Large chart
  335. let ul = ChartLimitLine()
  336. ul.limit = Double(midnightTimeInterval)
  337. ul.lineColor = NSUIColor.systemTeal.withAlphaComponent(0.5)
  338. ul.lineDashLengths = [CGFloat(2), CGFloat(5)]
  339. ul.lineWidth = 1
  340. BGChart.xAxis.addLimitLine(ul)
  341. // Small chart
  342. let sl = ChartLimitLine()
  343. sl.limit = Double(midnightTimeInterval)
  344. sl.lineColor = NSUIColor.systemTeal
  345. sl.lineDashLengths = [CGFloat(2), CGFloat(2)]
  346. sl.lineWidth = 1
  347. BGChartFull.xAxis.addLimitLine(sl)
  348. midnightTimeInterval = midnightTimeInterval.advanced(by: -24*60*60)
  349. }
  350. }
  351. }
  352. func updateBGGraphSettings() {
  353. let dataIndex = 0
  354. let dataIndexPrediction = 1
  355. let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  356. let linePrediction = BGChart.lineData!.dataSets[dataIndexPrediction] as! LineChartDataSet
  357. if UserDefaultsRepository.showLines.value {
  358. lineBG.lineWidth = 2
  359. linePrediction.lineWidth = 2
  360. } else {
  361. lineBG.lineWidth = 0
  362. linePrediction.lineWidth = 0
  363. }
  364. if UserDefaultsRepository.showDots.value {
  365. lineBG.drawCirclesEnabled = true
  366. linePrediction.drawCirclesEnabled = true
  367. } else {
  368. lineBG.drawCirclesEnabled = false
  369. linePrediction.drawCirclesEnabled = false
  370. }
  371. BGChart.rightAxis.axisMinimum = 0
  372. // Clear limit lines so they don't add multiples when changing the settings
  373. BGChart.rightAxis.removeAllLimitLines()
  374. //Add lower red line based on low alert value
  375. let ll = ChartLimitLine()
  376. ll.limit = Double(UserDefaultsRepository.lowLine.value)
  377. ll.lineColor = NSUIColor.systemRed.withAlphaComponent(0.5)
  378. BGChart.rightAxis.addLimitLine(ll)
  379. //Add upper yellow line based on low alert value
  380. let ul = ChartLimitLine()
  381. ul.limit = Double(UserDefaultsRepository.highLine.value)
  382. ul.lineColor = NSUIColor.systemYellow.withAlphaComponent(0.5)
  383. BGChart.rightAxis.addLimitLine(ul)
  384. // Re-create vertical markers in case their settings changed
  385. createVerticalLines()
  386. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  387. BGChart.data?.notifyDataChanged()
  388. BGChart.notifyDataSetChanged()
  389. }
  390. func updateBGGraph() {
  391. if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "##### Start BG Graph #####") }
  392. let dataIndex = 0
  393. let entries = bgData
  394. if entries.count < 1 { return }
  395. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  396. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  397. mainChart.clear()
  398. smallChart.clear()
  399. var maxBGOffset: Float = 50
  400. var colors = [NSUIColor]()
  401. for i in 0..<entries.count{
  402. if Float(entries[i].sgv) > topBG - maxBGOffset {
  403. topBG = Float(entries[i].sgv) + maxBGOffset
  404. }
  405. 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))
  406. if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "BG: " + value.description) }
  407. mainChart.addEntry(value)
  408. smallChart.addEntry(value)
  409. if Double(entries[i].sgv) >= Double(UserDefaultsRepository.highLine.value) {
  410. colors.append(NSUIColor.systemYellow)
  411. } else if Double(entries[i].sgv) <= Double(UserDefaultsRepository.lowLine.value) {
  412. colors.append(NSUIColor.systemRed)
  413. } else {
  414. colors.append(NSUIColor.systemGreen)
  415. }
  416. }
  417. if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "Total Graph BGs: " + mainChart.entries.count.description) }
  418. // Set Colors
  419. let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  420. let lineBGSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  421. lineBG.colors.removeAll()
  422. lineBG.circleColors.removeAll()
  423. lineBGSmall.colors.removeAll()
  424. lineBGSmall.circleColors.removeAll()
  425. if colors.count > 0 {
  426. for i in 0..<colors.count{
  427. mainChart.addColor(colors[i])
  428. mainChart.circleColors.append(colors[i])
  429. smallChart.addColor(colors[i])
  430. smallChart.circleColors.append(colors[i])
  431. }
  432. }
  433. if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "Total Colors: " + mainChart.colors.count.description) }
  434. BGChart.rightAxis.axisMaximum = Double(topBG)
  435. BGChart.setVisibleXRangeMinimum(600)
  436. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  437. BGChart.data?.notifyDataChanged()
  438. BGChart.notifyDataSetChanged()
  439. BGChartFull.rightAxis.axisMaximum = Double(topBG)
  440. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  441. BGChartFull.data?.notifyDataChanged()
  442. BGChartFull.notifyDataSetChanged()
  443. if firstGraphLoad {
  444. var scaleX = CGFloat(UserDefaultsRepository.chartScaleX.value)
  445. print("Scale: \(scaleX)")
  446. if( scaleX > CGFloat(ScaleXMax) ) {
  447. scaleX = CGFloat(ScaleXMax)
  448. UserDefaultsRepository.chartScaleX.value = ScaleXMax
  449. }
  450. BGChart.zoom(scaleX: scaleX, scaleY: 1, x: 1, y: 1)
  451. firstGraphLoad = false
  452. }
  453. // Move to current reading everytime new readings load
  454. BGChart.moveViewToAnimated(xValue: dateTimeUtils.getNowTimeIntervalUTC() - (BGChart.visibleXRange * 0.7), yValue: 0.0, axis: .right, duration: 1, easingOption: .easeInBack)
  455. }
  456. func updatePredictionGraph() {
  457. let dataIndex = 1
  458. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  459. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  460. mainChart.clear()
  461. smallChart.clear()
  462. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: print prediction") }
  463. var colors = [NSUIColor]()
  464. let maxBGOffset: Float = 20
  465. for i in 0..<predictionData.count {
  466. var predictionVal = Double(predictionData[i].sgv)
  467. if Float(predictionVal) > topBG - maxBGOffset {
  468. topBG = Float(predictionVal) + maxBGOffset
  469. }
  470. if i == 0 {
  471. if UserDefaultsRepository.showDots.value {
  472. colors.append(NSUIColor.systemPurple.withAlphaComponent(0.0))
  473. } else {
  474. colors.append(NSUIColor.systemPurple.withAlphaComponent(1.0))
  475. }
  476. } else if predictionVal > 400 {
  477. predictionVal = 400
  478. colors.append(NSUIColor.systemYellow)
  479. } else if predictionVal < 0 {
  480. predictionVal = 0
  481. colors.append(NSUIColor.systemRed)
  482. } else {
  483. colors.append(NSUIColor.systemPurple)
  484. }
  485. let value = ChartDataEntry(x: predictionData[i].date, y: predictionVal, data: formatPillText(line1: bgUnits.toDisplayUnits(String(predictionData[i].sgv)), time: predictionData[i].date))
  486. mainChart.addEntry(value)
  487. smallChart.addEntry(value)
  488. }
  489. smallChart.circleColors.removeAll()
  490. smallChart.colors.removeAll()
  491. mainChart.colors.removeAll()
  492. mainChart.circleColors.removeAll()
  493. if colors.count > 0 {
  494. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: prediction colors") }
  495. for i in 0..<colors.count{
  496. mainChart.addColor(colors[i])
  497. mainChart.circleColors.append(colors[i])
  498. smallChart.addColor(colors[i])
  499. smallChart.circleColors.append(colors[i])
  500. }
  501. }
  502. BGChart.rightAxis.axisMaximum = Double(topBG)
  503. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  504. BGChart.data?.notifyDataChanged()
  505. BGChart.notifyDataSetChanged()
  506. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  507. BGChartFull.data?.notifyDataChanged()
  508. BGChartFull.notifyDataSetChanged()
  509. }
  510. func updateBasalGraph() {
  511. var dataIndex = 2
  512. BGChart.lineData?.dataSets[dataIndex].clear()
  513. BGChartFull.lineData?.dataSets[dataIndex].clear()
  514. var maxBasal = UserDefaultsRepository.minBasalScale.value
  515. var maxBasalSmall: Double = 0.0
  516. for i in 0..<basalData.count{
  517. let value = ChartDataEntry(x: Double(basalData[i].date), y: Double(basalData[i].basalRate), data: formatPillText(line1: String(basalData[i].basalRate), time: basalData[i].date))
  518. BGChart.data?.dataSets[dataIndex].addEntry(value)
  519. if UserDefaultsRepository.smallGraphTreatments.value {
  520. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  521. }
  522. if basalData[i].basalRate > maxBasal {
  523. maxBasal = basalData[i].basalRate
  524. }
  525. if basalData[i].basalRate > maxBasalSmall {
  526. maxBasalSmall = basalData[i].basalRate
  527. }
  528. }
  529. BGChart.leftAxis.axisMaximum = maxBasal
  530. BGChartFull.leftAxis.axisMaximum = maxBasalSmall
  531. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  532. BGChart.data?.notifyDataChanged()
  533. BGChart.notifyDataSetChanged()
  534. if UserDefaultsRepository.smallGraphTreatments.value {
  535. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  536. BGChartFull.data?.notifyDataChanged()
  537. BGChartFull.notifyDataSetChanged()
  538. }
  539. }
  540. func updateBasalScheduledGraph() {
  541. var dataIndex = 5
  542. BGChart.lineData?.dataSets[dataIndex].clear()
  543. BGChartFull.lineData?.dataSets[dataIndex].clear()
  544. for i in 0..<basalScheduleData.count{
  545. let value = ChartDataEntry(x: Double(basalScheduleData[i].date), y: Double(basalScheduleData[i].basalRate))
  546. BGChart.data?.dataSets[dataIndex].addEntry(value)
  547. if UserDefaultsRepository.smallGraphTreatments.value {
  548. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  549. }
  550. }
  551. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  552. BGChart.data?.notifyDataChanged()
  553. BGChart.notifyDataSetChanged()
  554. if UserDefaultsRepository.smallGraphTreatments.value {
  555. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  556. BGChartFull.data?.notifyDataChanged()
  557. BGChartFull.notifyDataSetChanged()
  558. }
  559. }
  560. func updateBolusGraph() {
  561. var dataIndex = 3
  562. var yTop: Double = 370
  563. var yBottom: Double = 345
  564. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  565. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  566. mainChart.clear()
  567. smallChart.clear()
  568. var colors = [NSUIColor]()
  569. for i in 0..<bolusData.count{
  570. let formatter = NumberFormatter()
  571. formatter.minimumFractionDigits = 0
  572. formatter.maximumFractionDigits = 2
  573. formatter.minimumIntegerDigits = 0
  574. // Check overlapping carbs to shift left if needed
  575. let bolusShift = findNextBolusTime(timeWithin: 240, needle: bolusData[i].date, haystack: bolusData, startingIndex: i)
  576. var dateTimeStamp = bolusData[i].date
  577. // Alpha colors for DIA
  578. let nowTime = dateTimeUtils.getNowTimeIntervalUTC()
  579. let diffTimeHours = (nowTime - dateTimeStamp) / 60 / 60
  580. if diffTimeHours <= 1 {
  581. colors.append(NSUIColor.systemBlue.withAlphaComponent(1.0))
  582. } else if diffTimeHours > 6 {
  583. colors.append(NSUIColor.systemBlue.withAlphaComponent(0.25))
  584. } else {
  585. let thisAlpha = 1.0 - (0.15 * diffTimeHours)
  586. colors.append(NSUIColor.systemBlue.withAlphaComponent(CGFloat(thisAlpha)))
  587. }
  588. if bolusShift {
  589. // Move it half the distance between BG readings
  590. dateTimeStamp = dateTimeStamp - 150
  591. }
  592. // skip if outside of visible area
  593. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  594. if dateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  595. let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(bolusData[i].sgv), data: formatter.string(from: NSNumber(value: bolusData[i].value)))
  596. mainChart.addEntry(dot)
  597. if UserDefaultsRepository.smallGraphTreatments.value {
  598. smallChart.addEntry(dot)
  599. }
  600. }
  601. // Set Colors
  602. let lineBolus = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  603. let lineBolusSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  604. lineBolus.colors.removeAll()
  605. lineBolus.circleColors.removeAll()
  606. lineBolusSmall.colors.removeAll()
  607. lineBolusSmall.circleColors.removeAll()
  608. if colors.count > 0 {
  609. for i in 0..<colors.count{
  610. mainChart.addColor(colors[i])
  611. mainChart.circleColors.append(colors[i])
  612. smallChart.addColor(colors[i])
  613. smallChart.circleColors.append(colors[i])
  614. }
  615. }
  616. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  617. BGChart.data?.notifyDataChanged()
  618. BGChart.notifyDataSetChanged()
  619. if UserDefaultsRepository.smallGraphTreatments.value {
  620. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  621. BGChartFull.data?.notifyDataChanged()
  622. BGChartFull.notifyDataSetChanged()
  623. }
  624. }
  625. func updateCarbGraph() {
  626. var dataIndex = 4
  627. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  628. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  629. mainChart.clear()
  630. smallChart.clear()
  631. var colors = [NSUIColor]()
  632. for i in 0..<carbData.count{
  633. let formatter = NumberFormatter()
  634. formatter.minimumFractionDigits = 0
  635. formatter.maximumFractionDigits = 2
  636. formatter.minimumIntegerDigits = 1
  637. var valueString: String = formatter.string(from: NSNumber(value: carbData[i].value))!
  638. var hours = 3
  639. if carbData[i].absorptionTime > 0 && UserDefaultsRepository.showAbsorption.value {
  640. hours = carbData[i].absorptionTime / 60
  641. valueString += " " + String(hours) + "h"
  642. }
  643. // Check overlapping carbs to shift left if needed
  644. let carbShift = findNextCarbTime(timeWithin: 250, needle: carbData[i].date, haystack: carbData, startingIndex: i)
  645. var dateTimeStamp = carbData[i].date
  646. // Alpha colors for DIA
  647. let nowTime = dateTimeUtils.getNowTimeIntervalUTC()
  648. let diffTimeHours = (nowTime - dateTimeStamp) / 60 / 60
  649. if diffTimeHours <= 0.5 {
  650. colors.append(NSUIColor.systemOrange.withAlphaComponent(1.0))
  651. } else if diffTimeHours > Double(hours) {
  652. colors.append(NSUIColor.systemOrange.withAlphaComponent(0.25))
  653. } else {
  654. let thisAlpha = 1.0 - ((0.75 / Double(hours)) * diffTimeHours)
  655. colors.append(NSUIColor.systemOrange.withAlphaComponent(CGFloat(thisAlpha)))
  656. }
  657. // skip if outside of visible area
  658. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  659. if dateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  660. if carbShift {
  661. dateTimeStamp = dateTimeStamp - 250
  662. }
  663. let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(carbData[i].sgv), data: valueString)
  664. BGChart.data?.dataSets[dataIndex].addEntry(dot)
  665. if UserDefaultsRepository.smallGraphTreatments.value {
  666. BGChartFull.data?.dataSets[dataIndex].addEntry(dot)
  667. }
  668. }
  669. // Set Colors
  670. let lineCarbs = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  671. let lineCarbsSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  672. lineCarbs.colors.removeAll()
  673. lineCarbs.circleColors.removeAll()
  674. lineCarbsSmall.colors.removeAll()
  675. lineCarbsSmall.circleColors.removeAll()
  676. if colors.count > 0 {
  677. for i in 0..<colors.count{
  678. mainChart.addColor(colors[i])
  679. mainChart.circleColors.append(colors[i])
  680. smallChart.addColor(colors[i])
  681. smallChart.circleColors.append(colors[i])
  682. }
  683. }
  684. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  685. BGChart.data?.notifyDataChanged()
  686. BGChart.notifyDataSetChanged()
  687. if UserDefaultsRepository.smallGraphTreatments.value {
  688. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  689. BGChartFull.data?.notifyDataChanged()
  690. BGChartFull.notifyDataSetChanged()
  691. }
  692. }
  693. func updateBGCheckGraph() {
  694. var dataIndex = 7
  695. BGChart.lineData?.dataSets[dataIndex].clear()
  696. BGChartFull.lineData?.dataSets[dataIndex].clear()
  697. for i in 0..<bgCheckData.count{
  698. let formatter = NumberFormatter()
  699. formatter.minimumFractionDigits = 0
  700. formatter.maximumFractionDigits = 2
  701. formatter.minimumIntegerDigits = 1
  702. // skip if outside of visible area
  703. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  704. if bgCheckData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  705. 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))
  706. BGChart.data?.dataSets[dataIndex].addEntry(value)
  707. if UserDefaultsRepository.smallGraphTreatments.value {
  708. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  709. }
  710. }
  711. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  712. BGChart.data?.notifyDataChanged()
  713. BGChart.notifyDataSetChanged()
  714. if UserDefaultsRepository.smallGraphTreatments.value {
  715. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  716. BGChartFull.data?.notifyDataChanged()
  717. BGChartFull.notifyDataSetChanged()
  718. }
  719. }
  720. func updateSuspendGraph() {
  721. var dataIndex = 8
  722. BGChart.lineData?.dataSets[dataIndex].clear()
  723. BGChartFull.lineData?.dataSets[dataIndex].clear()
  724. let thisData = suspendGraphData
  725. for i in 0..<thisData.count{
  726. // skip if outside of visible area
  727. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  728. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  729. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Suspend Pump", time: thisData[i].date))
  730. BGChart.data?.dataSets[dataIndex].addEntry(value)
  731. if UserDefaultsRepository.smallGraphTreatments.value {
  732. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  733. }
  734. }
  735. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  736. BGChart.data?.notifyDataChanged()
  737. BGChart.notifyDataSetChanged()
  738. if UserDefaultsRepository.smallGraphTreatments.value {
  739. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  740. BGChartFull.data?.notifyDataChanged()
  741. BGChartFull.notifyDataSetChanged()
  742. }
  743. }
  744. func updateResumeGraph() {
  745. var dataIndex = 9
  746. BGChart.lineData?.dataSets[dataIndex].clear()
  747. BGChartFull.lineData?.dataSets[dataIndex].clear()
  748. let thisData = resumeGraphData
  749. for i in 0..<thisData.count{
  750. // skip if outside of visible area
  751. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  752. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  753. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Resume Pump", time: thisData[i].date))
  754. BGChart.data?.dataSets[dataIndex].addEntry(value)
  755. if UserDefaultsRepository.smallGraphTreatments.value {
  756. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  757. }
  758. }
  759. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  760. BGChart.data?.notifyDataChanged()
  761. BGChart.notifyDataSetChanged()
  762. if UserDefaultsRepository.smallGraphTreatments.value {
  763. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  764. BGChartFull.data?.notifyDataChanged()
  765. BGChartFull.notifyDataSetChanged()
  766. }
  767. }
  768. func updateSensorStart() {
  769. var dataIndex = 10
  770. BGChart.lineData?.dataSets[dataIndex].clear()
  771. BGChartFull.lineData?.dataSets[dataIndex].clear()
  772. let thisData = sensorStartGraphData
  773. for i in 0..<thisData.count{
  774. // skip if outside of visible area
  775. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  776. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  777. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Start Sensor", time: thisData[i].date))
  778. BGChart.data?.dataSets[dataIndex].addEntry(value)
  779. if UserDefaultsRepository.smallGraphTreatments.value {
  780. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  781. }
  782. }
  783. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  784. BGChart.data?.notifyDataChanged()
  785. BGChart.notifyDataSetChanged()
  786. if UserDefaultsRepository.smallGraphTreatments.value {
  787. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  788. BGChartFull.data?.notifyDataChanged()
  789. BGChartFull.notifyDataSetChanged()
  790. }
  791. }
  792. func updateNotes() {
  793. var dataIndex = 11
  794. BGChart.lineData?.dataSets[dataIndex].clear()
  795. BGChartFull.lineData?.dataSets[dataIndex].clear()
  796. let thisData = noteGraphData
  797. for i in 0..<thisData.count{
  798. // skip if outside of visible area
  799. let graphHours = 24 * UserDefaultsRepository.downloadDays.value
  800. if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
  801. let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: thisData[i].note, time: thisData[i].date))
  802. BGChart.data?.dataSets[dataIndex].addEntry(value)
  803. if UserDefaultsRepository.smallGraphTreatments.value {
  804. BGChartFull.data?.dataSets[dataIndex].addEntry(value)
  805. }
  806. }
  807. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  808. BGChart.data?.notifyDataChanged()
  809. BGChart.notifyDataSetChanged()
  810. if UserDefaultsRepository.smallGraphTreatments.value {
  811. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  812. BGChartFull.data?.notifyDataChanged()
  813. BGChartFull.notifyDataSetChanged()
  814. }
  815. }
  816. func createSmallBGGraph(){
  817. let entries = bgData
  818. var bgChartEntry = [ChartDataEntry]()
  819. var colors = [NSUIColor]()
  820. var maxBG: Float = UserDefaultsRepository.minBGScale.value
  821. let lineBG = LineChartDataSet(entries:bgChartEntry, label: "")
  822. lineBG.drawCirclesEnabled = false
  823. //line2.setDrawHighlightIndicators(false)
  824. lineBG.highlightEnabled = true
  825. lineBG.drawHorizontalHighlightIndicatorEnabled = false
  826. lineBG.drawVerticalHighlightIndicatorEnabled = false
  827. lineBG.highlightColor = NSUIColor.label
  828. lineBG.drawValuesEnabled = false
  829. lineBG.lineWidth = 1.5
  830. lineBG.axisDependency = YAxis.AxisDependency.right
  831. // Setup Prediction line details
  832. var predictionChartEntry = [ChartDataEntry]()
  833. let linePrediction = LineChartDataSet(entries:predictionChartEntry, label: "")
  834. linePrediction.drawCirclesEnabled = false
  835. //line2.setDrawHighlightIndicators(false)
  836. linePrediction.setColor(NSUIColor.systemPurple)
  837. linePrediction.highlightEnabled = true
  838. linePrediction.drawHorizontalHighlightIndicatorEnabled = false
  839. linePrediction.drawVerticalHighlightIndicatorEnabled = false
  840. linePrediction.highlightColor = NSUIColor.label
  841. linePrediction.drawValuesEnabled = false
  842. linePrediction.lineWidth = 1.5
  843. linePrediction.axisDependency = YAxis.AxisDependency.right
  844. // create Basal graph data
  845. var chartEntry = [ChartDataEntry]()
  846. var maxBasal = UserDefaultsRepository.minBasalScale.value
  847. let lineBasal = LineChartDataSet(entries:chartEntry, label: "")
  848. lineBasal.setDrawHighlightIndicators(false)
  849. lineBasal.setColor(NSUIColor.systemBlue, alpha: 0.5)
  850. lineBasal.lineWidth = 0
  851. lineBasal.drawFilledEnabled = true
  852. lineBasal.fillColor = NSUIColor.systemBlue
  853. lineBasal.fillAlpha = 0.35
  854. lineBasal.drawCirclesEnabled = false
  855. lineBasal.axisDependency = YAxis.AxisDependency.left
  856. lineBasal.highlightEnabled = false
  857. lineBasal.drawValuesEnabled = false
  858. lineBasal.fillFormatter = basalFillFormatter()
  859. // Boluses
  860. var chartEntryBolus = [ChartDataEntry]()
  861. let lineBolus = LineChartDataSet(entries:chartEntryBolus, label: "")
  862. lineBolus.circleRadius = 2
  863. lineBolus.circleColors = [NSUIColor.systemBlue.withAlphaComponent(0.75)]
  864. lineBolus.drawCircleHoleEnabled = false
  865. lineBolus.setDrawHighlightIndicators(false)
  866. lineBolus.setColor(NSUIColor.systemBlue, alpha: 1.0)
  867. lineBolus.lineWidth = 0
  868. lineBolus.axisDependency = YAxis.AxisDependency.right
  869. lineBolus.valueFormatter = ChartYDataValueFormatter()
  870. lineBolus.valueTextColor = NSUIColor.label
  871. lineBolus.fillColor = NSUIColor.systemBlue
  872. lineBolus.fillAlpha = 0.6
  873. lineBolus.drawCirclesEnabled = true
  874. lineBolus.drawFilledEnabled = false
  875. lineBolus.drawValuesEnabled = false
  876. lineBolus.highlightEnabled = false
  877. // Carbs
  878. var chartEntryCarbs = [ChartDataEntry]()
  879. let lineCarbs = LineChartDataSet(entries:chartEntryCarbs, label: "")
  880. lineCarbs.circleRadius = 2
  881. lineCarbs.circleColors = [NSUIColor.systemOrange.withAlphaComponent(0.75)]
  882. lineCarbs.drawCircleHoleEnabled = false
  883. lineCarbs.setDrawHighlightIndicators(false)
  884. lineCarbs.setColor(NSUIColor.systemBlue, alpha: 1.0)
  885. lineCarbs.lineWidth = 0
  886. lineCarbs.axisDependency = YAxis.AxisDependency.right
  887. lineCarbs.valueFormatter = ChartYDataValueFormatter()
  888. lineCarbs.valueTextColor = NSUIColor.label
  889. lineCarbs.fillColor = NSUIColor.systemOrange
  890. lineCarbs.fillAlpha = 0.6
  891. lineCarbs.drawCirclesEnabled = true
  892. lineCarbs.drawFilledEnabled = false
  893. lineCarbs.drawValuesEnabled = false
  894. lineCarbs.highlightEnabled = false
  895. // create Scheduled Basal graph data
  896. var chartBasalScheduledEntry = [ChartDataEntry]()
  897. let lineBasalScheduled = LineChartDataSet(entries:chartBasalScheduledEntry, label: "")
  898. lineBasalScheduled.setDrawHighlightIndicators(false)
  899. lineBasalScheduled.setColor(NSUIColor.systemBlue, alpha: 0.8)
  900. lineBasalScheduled.lineWidth = 0.5
  901. lineBasalScheduled.drawFilledEnabled = false
  902. lineBasalScheduled.drawCirclesEnabled = false
  903. lineBasalScheduled.axisDependency = YAxis.AxisDependency.left
  904. lineBasalScheduled.highlightEnabled = false
  905. lineBasalScheduled.drawValuesEnabled = false
  906. lineBasalScheduled.lineDashLengths = [2, 1]
  907. // create Override graph data
  908. var chartOverrideEntry = [ChartDataEntry]()
  909. let lineOverride = LineChartDataSet(entries:chartOverrideEntry, label: "")
  910. lineOverride.setDrawHighlightIndicators(false)
  911. lineOverride.lineWidth = 0
  912. lineOverride.drawFilledEnabled = true
  913. lineOverride.fillFormatter = OverrideFillFormatter()
  914. lineOverride.fillColor = NSUIColor.systemGreen
  915. lineOverride.fillAlpha = 0.6
  916. lineOverride.drawCirclesEnabled = false
  917. lineOverride.axisDependency = YAxis.AxisDependency.right
  918. lineOverride.highlightEnabled = true
  919. lineOverride.drawValuesEnabled = false
  920. // BG Check
  921. var chartEntryBGCheck = [ChartDataEntry]()
  922. let lineBGCheck = LineChartDataSet(entries:chartEntryBGCheck, label: "")
  923. lineBGCheck.circleRadius = 2
  924. lineBGCheck.circleColors = [NSUIColor.systemRed.withAlphaComponent(0.75)]
  925. lineBGCheck.drawCircleHoleEnabled = false
  926. lineBGCheck.setDrawHighlightIndicators(false)
  927. lineBGCheck.setColor(NSUIColor.systemRed, alpha: 1.0)
  928. lineBGCheck.drawCirclesEnabled = true
  929. lineBGCheck.lineWidth = 0
  930. lineBGCheck.highlightEnabled = false
  931. lineBGCheck.axisDependency = YAxis.AxisDependency.right
  932. lineBGCheck.valueFormatter = ChartYDataValueFormatter()
  933. lineBGCheck.drawValuesEnabled = false
  934. // Suspend Pump
  935. var chartEntrySuspend = [ChartDataEntry]()
  936. let lineSuspend = LineChartDataSet(entries:chartEntrySuspend, label: "")
  937. lineSuspend.circleRadius = 2
  938. lineSuspend.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
  939. lineSuspend.drawCircleHoleEnabled = false
  940. lineSuspend.setDrawHighlightIndicators(false)
  941. lineSuspend.setColor(NSUIColor.systemGray2, alpha: 1.0)
  942. lineSuspend.drawCirclesEnabled = true
  943. lineSuspend.lineWidth = 0
  944. lineSuspend.highlightEnabled = false
  945. lineSuspend.axisDependency = YAxis.AxisDependency.right
  946. lineSuspend.valueFormatter = ChartYDataValueFormatter()
  947. lineSuspend.drawValuesEnabled = false
  948. // Resume Pump
  949. var chartEntryResume = [ChartDataEntry]()
  950. let lineResume = LineChartDataSet(entries:chartEntryResume, label: "")
  951. lineResume.circleRadius = 2
  952. lineResume.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
  953. lineResume.drawCircleHoleEnabled = false
  954. lineResume.setDrawHighlightIndicators(false)
  955. lineResume.setColor(NSUIColor.systemGray4, alpha: 1.0)
  956. lineResume.drawCirclesEnabled = true
  957. lineResume.lineWidth = 0
  958. lineResume.highlightEnabled = false
  959. lineResume.axisDependency = YAxis.AxisDependency.right
  960. lineResume.valueFormatter = ChartYDataValueFormatter()
  961. lineResume.drawValuesEnabled = false
  962. // Sensor Start
  963. var chartEntrySensor = [ChartDataEntry]()
  964. let lineSensor = LineChartDataSet(entries:chartEntrySensor, label: "")
  965. lineSensor.circleRadius = 2
  966. lineSensor.circleColors = [NSUIColor.systemIndigo.withAlphaComponent(0.75)]
  967. lineSensor.drawCircleHoleEnabled = false
  968. lineSensor.setDrawHighlightIndicators(false)
  969. lineSensor.setColor(NSUIColor.systemGray3, alpha: 1.0)
  970. lineSensor.drawCirclesEnabled = true
  971. lineSensor.lineWidth = 0
  972. lineSensor.highlightEnabled = false
  973. lineSensor.axisDependency = YAxis.AxisDependency.right
  974. lineSensor.valueFormatter = ChartYDataValueFormatter()
  975. lineSensor.drawValuesEnabled = false
  976. // Notes
  977. var chartEntryNote = [ChartDataEntry]()
  978. let lineNote = LineChartDataSet(entries:chartEntryNote, label: "")
  979. lineNote.circleRadius = 2
  980. lineNote.circleColors = [NSUIColor.systemGray.withAlphaComponent(0.75)]
  981. lineNote.drawCircleHoleEnabled = false
  982. lineNote.setDrawHighlightIndicators(false)
  983. lineNote.setColor(NSUIColor.systemGray3, alpha: 1.0)
  984. lineNote.drawCirclesEnabled = true
  985. lineNote.lineWidth = 0
  986. lineNote.highlightEnabled = false
  987. lineNote.axisDependency = YAxis.AxisDependency.right
  988. lineNote.valueFormatter = ChartYDataValueFormatter()
  989. lineNote.drawValuesEnabled = false
  990. // Setup the chart data of all lines
  991. let data = LineChartData()
  992. data.append(lineBG) // Dataset 0
  993. data.append(linePrediction) // Dataset 1
  994. data.append(lineBasal) // Dataset 2
  995. data.append(lineBolus) // Dataset 3
  996. data.append(lineCarbs) // Dataset 4
  997. data.append(lineBasalScheduled) // Dataset 5
  998. data.append(lineOverride) // Dataset 6
  999. data.append(lineBGCheck) // Dataset 7
  1000. data.append(lineSuspend) // Dataset 8
  1001. data.append(lineResume) // Dataset 9
  1002. data.append(lineSensor) // Dataset 10
  1003. data.append(lineNote) // Dataset 11
  1004. BGChartFull.highlightPerDragEnabled = true
  1005. BGChartFull.leftAxis.enabled = false
  1006. BGChartFull.leftAxis.axisMaximum = maxBasal
  1007. BGChartFull.leftAxis.axisMinimum = 0
  1008. BGChartFull.rightAxis.enabled = false
  1009. BGChartFull.rightAxis.axisMinimum = 0.0
  1010. BGChartFull.rightAxis.axisMaximum = Double(maxBG)
  1011. BGChartFull.xAxis.drawLabelsEnabled = false
  1012. BGChartFull.xAxis.drawGridLinesEnabled = false
  1013. BGChartFull.xAxis.drawAxisLineEnabled = false
  1014. BGChartFull.legend.enabled = false
  1015. BGChartFull.scaleYEnabled = false
  1016. BGChartFull.scaleXEnabled = false
  1017. BGChartFull.drawGridBackgroundEnabled = false
  1018. BGChartFull.data = data
  1019. }
  1020. func updateOverrideGraph() {
  1021. var dataIndex = 6
  1022. var yTop: Double = Double(topBG - 5)
  1023. var yBottom: Double = Double(topBG - 25)
  1024. var chart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  1025. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  1026. chart.clear()
  1027. smallChart.clear()
  1028. let thisData = overrideGraphData
  1029. var colors = [NSUIColor]()
  1030. for i in 0..<thisData.count{
  1031. let thisItem = thisData[i]
  1032. let multiplier = thisItem.insulNeedsScaleFactor as! Double * 100.0
  1033. var labelText = thisItem.reason + "\r\n"
  1034. labelText += String(Int(thisItem.insulNeedsScaleFactor * 100)) + "% "
  1035. if thisItem.correctionRange.count == 2 {
  1036. labelText += String(thisItem.correctionRange[0]) + "-" + String(thisItem.correctionRange[1])
  1037. }
  1038. if thisItem.enteredBy.count > 0 {
  1039. labelText += "\r\nEntered By: " + thisItem.enteredBy
  1040. }
  1041. // Start Dot
  1042. // Shift dots 30 seconds to create an empty 0 space between consecutive temps
  1043. let preStartDot = ChartDataEntry(x: Double(thisItem.date), y: yBottom, data: labelText)
  1044. BGChart.data?.dataSets[dataIndex].addEntry(preStartDot)
  1045. if UserDefaultsRepository.smallGraphTreatments.value {
  1046. BGChartFull.data?.dataSets[dataIndex].addEntry(preStartDot)
  1047. }
  1048. let startDot = ChartDataEntry(x: Double(thisItem.date + 1), y: yTop, data: labelText)
  1049. BGChart.data?.dataSets[dataIndex].addEntry(startDot)
  1050. if UserDefaultsRepository.smallGraphTreatments.value {
  1051. BGChartFull.data?.dataSets[dataIndex].addEntry(startDot)
  1052. }
  1053. // End Dot
  1054. let endDot = ChartDataEntry(x: Double(thisItem.endDate - 2), y: yTop, data: labelText)
  1055. BGChart.data?.dataSets[dataIndex].addEntry(endDot)
  1056. if UserDefaultsRepository.smallGraphTreatments.value {
  1057. BGChartFull.data?.dataSets[dataIndex].addEntry(endDot)
  1058. }
  1059. // Post end dot
  1060. let postEndDot = ChartDataEntry(x: Double(thisItem.endDate - 1), y: yBottom, data: labelText)
  1061. BGChart.data?.dataSets[dataIndex].addEntry(postEndDot)
  1062. if UserDefaultsRepository.smallGraphTreatments.value {
  1063. BGChartFull.data?.dataSets[dataIndex].addEntry(postEndDot)
  1064. }
  1065. }
  1066. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  1067. BGChart.data?.notifyDataChanged()
  1068. BGChart.notifyDataSetChanged()
  1069. if UserDefaultsRepository.smallGraphTreatments.value {
  1070. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  1071. BGChartFull.data?.notifyDataChanged()
  1072. BGChartFull.notifyDataSetChanged()
  1073. }
  1074. }
  1075. func formatPillText(line1: String, time: TimeInterval) -> String {
  1076. let dateFormatter = DateFormatter()
  1077. //let timezoneOffset = TimeZone.current.secondsFromGMT()
  1078. //let epochTimezoneOffset = value + Double(timezoneOffset)
  1079. if dateTimeUtils.is24Hour() {
  1080. dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm")
  1081. } else {
  1082. dateFormatter.setLocalizedDateFormatFromTemplate("hh:mm")
  1083. }
  1084. //let date = Date(timeIntervalSince1970: epochTimezoneOffset)
  1085. let date = Date(timeIntervalSince1970: time)
  1086. let formattedDate = dateFormatter.string(from: date)
  1087. return line1 + "\r\n" + formattedDate
  1088. }
  1089. }