Graphs.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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. }
  18. func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat) {
  19. if chartView == BGChart {
  20. let currentMatrix = chartView.viewPortHandler.touchMatrix
  21. //BGChartFull.viewPortHandler.refresh(newMatrix: currentMatrix, chart: BGChartFull, invalidate: true)
  22. //BGChartFull.highlightValue(x: Double(currentMatrix.tx), y: Double(currentMatrix.ty), dataSetIndex: 0)
  23. }
  24. }
  25. func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat) {
  26. print("Chart Scaled: \(BGChart.scaleX), \(BGChart.scaleY)")
  27. // dont store huge values
  28. var scale: Float = Float(BGChart.scaleX)
  29. if(scale > ScaleXMax ) {
  30. scale = ScaleXMax
  31. }
  32. UserDefaultsRepository.chartScaleX.value = Float(scale)
  33. }
  34. func createGraph(){
  35. self.BGChart.clear()
  36. // Create the BG Graph Data
  37. let entries = bgData
  38. var bgChartEntry = [ChartDataEntry]()
  39. var colors = [NSUIColor]()
  40. var maxBG: Float = UserDefaultsRepository.minBGScale.value
  41. // Setup BG line details
  42. let lineBG = LineChartDataSet(entries:bgChartEntry, label: "")
  43. lineBG.circleRadius = 3
  44. lineBG.circleColors = [NSUIColor.systemGreen]
  45. lineBG.drawCircleHoleEnabled = false
  46. lineBG.axisDependency = YAxis.AxisDependency.right
  47. lineBG.highlightEnabled = true
  48. lineBG.drawValuesEnabled = false
  49. if UserDefaultsRepository.showLines.value {
  50. lineBG.lineWidth = 2
  51. } else {
  52. lineBG.lineWidth = 0
  53. }
  54. if UserDefaultsRepository.showDots.value {
  55. lineBG.drawCirclesEnabled = true
  56. } else {
  57. lineBG.drawCirclesEnabled = false
  58. }
  59. lineBG.setDrawHighlightIndicators(false)
  60. lineBG.valueFont.withSize(50)
  61. // Setup Prediction line details
  62. var predictionChartEntry = [ChartDataEntry]()
  63. let linePrediction = LineChartDataSet(entries:predictionChartEntry, label: "")
  64. linePrediction.circleRadius = 3
  65. linePrediction.circleColors = [NSUIColor.systemPurple]
  66. linePrediction.colors = [NSUIColor.systemPurple]
  67. linePrediction.drawCircleHoleEnabled = false
  68. linePrediction.axisDependency = YAxis.AxisDependency.right
  69. linePrediction.highlightEnabled = true
  70. linePrediction.drawValuesEnabled = false
  71. if UserDefaultsRepository.showLines.value {
  72. linePrediction.lineWidth = 2
  73. } else {
  74. linePrediction.lineWidth = 0
  75. }
  76. if UserDefaultsRepository.showDots.value {
  77. linePrediction.drawCirclesEnabled = true
  78. } else {
  79. linePrediction.drawCirclesEnabled = false
  80. }
  81. linePrediction.setDrawHighlightIndicators(false)
  82. linePrediction.valueFont.withSize(50)
  83. // create Basal graph data
  84. var chartEntry = [ChartDataEntry]()
  85. var maxBasal = UserDefaultsRepository.minBasalScale.value
  86. let lineBasal = LineChartDataSet(entries:chartEntry, label: "")
  87. lineBasal.setDrawHighlightIndicators(false)
  88. lineBasal.setColor(NSUIColor.systemBlue, alpha: 0.5)
  89. lineBasal.lineWidth = 0
  90. lineBasal.drawFilledEnabled = true
  91. lineBasal.fillColor = NSUIColor.systemBlue.withAlphaComponent(0.8)
  92. lineBasal.drawCirclesEnabled = false
  93. lineBasal.axisDependency = YAxis.AxisDependency.left
  94. lineBasal.highlightEnabled = true
  95. lineBasal.drawValuesEnabled = false
  96. // Boluses
  97. var chartEntryBolus = [ChartDataEntry]()
  98. let lineBolus = LineChartDataSet(entries:chartEntryBolus, label: "")
  99. lineBolus.circleRadius = 7
  100. lineBolus.circleColors = [NSUIColor.systemBlue.withAlphaComponent(0.75)]
  101. lineBolus.drawCircleHoleEnabled = false
  102. lineBolus.setDrawHighlightIndicators(false)
  103. lineBolus.setColor(NSUIColor.systemBlue, alpha: 1.0)
  104. lineBolus.drawCirclesEnabled = true
  105. lineBolus.lineWidth = 0
  106. lineBolus.highlightEnabled = false
  107. lineBolus.axisDependency = YAxis.AxisDependency.right
  108. lineBolus.valueFormatter = ChartYDataValueFormatter()
  109. lineBolus.drawValuesEnabled = true
  110. lineBolus.valueTextColor = NSUIColor.label
  111. // Carbs
  112. var chartEntryCarbs = [ChartDataEntry]()
  113. let lineCarbs = LineChartDataSet(entries:chartEntryCarbs, label: "")
  114. lineCarbs.circleRadius = 7
  115. lineCarbs.circleColors = [NSUIColor.systemOrange.withAlphaComponent(0.75)]
  116. lineCarbs.drawCircleHoleEnabled = false
  117. lineCarbs.setDrawHighlightIndicators(false)
  118. lineCarbs.setColor(NSUIColor.systemBlue, alpha: 1.0)
  119. lineCarbs.drawCirclesEnabled = true
  120. lineCarbs.lineWidth = 0
  121. lineCarbs.highlightEnabled = false
  122. lineCarbs.axisDependency = YAxis.AxisDependency.right
  123. lineCarbs.valueFormatter = ChartYDataValueFormatter()
  124. lineCarbs.drawValuesEnabled = true
  125. lineCarbs.valueTextColor = NSUIColor.label
  126. // create Scheduled Basal graph data
  127. var chartBasalScheduledEntry = [ChartDataEntry]()
  128. let lineBasalScheduled = LineChartDataSet(entries:chartBasalScheduledEntry, label: "")
  129. lineBasalScheduled.setDrawHighlightIndicators(false)
  130. lineBasalScheduled.setColor(NSUIColor.systemBlue, alpha: 0.8)
  131. lineBasalScheduled.lineWidth = 2
  132. lineBasalScheduled.drawFilledEnabled = false
  133. lineBasalScheduled.drawCirclesEnabled = false
  134. lineBasalScheduled.axisDependency = YAxis.AxisDependency.left
  135. lineBasalScheduled.highlightEnabled = false
  136. lineBasalScheduled.drawValuesEnabled = false
  137. lineBasalScheduled.lineDashLengths = [10.0, 5.0]
  138. // create Override graph data
  139. var chartOverrideEntry = [ChartDataEntry]()
  140. let lineOverride = LineChartDataSet(entries:chartOverrideEntry, label: "")
  141. lineOverride.setDrawHighlightIndicators(false)
  142. lineOverride.setColor(NSUIColor.systemTeal, alpha: 0.8 )
  143. lineOverride.lineWidth = 10
  144. lineOverride.drawFilledEnabled = false
  145. lineOverride.drawCirclesEnabled = false
  146. lineOverride.axisDependency = YAxis.AxisDependency.right
  147. lineOverride.highlightEnabled = true
  148. lineOverride.drawValuesEnabled = false
  149. // Setup the chart data of all lines
  150. let data = LineChartData()
  151. data.addDataSet(lineBG) // Dataset 0
  152. data.addDataSet(linePrediction) // Dataset 1
  153. data.addDataSet(lineBasal) // Dataset 2
  154. data.addDataSet(lineBolus) // Dataset 3
  155. data.addDataSet(lineCarbs) // Dataset 4
  156. data.addDataSet(lineBasalScheduled) // Dataset 5
  157. data.addDataSet(lineOverride) // Dataset 6
  158. data.setValueFont(UIFont.systemFont(ofSize: 12))
  159. // Add marker popups for bolus and carbs
  160. let marker = PillMarker(color: .secondarySystemBackground, font: UIFont.boldSystemFont(ofSize: 14), textColor: .label)
  161. BGChart.marker = marker
  162. // Clear limit lines so they don't add multiples when changing the settings
  163. BGChart.rightAxis.removeAllLimitLines()
  164. //Add lower red line based on low alert value
  165. let ll = ChartLimitLine()
  166. ll.limit = Double(UserDefaultsRepository.lowLine.value)
  167. ll.lineColor = NSUIColor.systemRed.withAlphaComponent(0.5)
  168. BGChart.rightAxis.addLimitLine(ll)
  169. //Add upper yellow line based on low alert value
  170. let ul = ChartLimitLine()
  171. ul.limit = Double(UserDefaultsRepository.highLine.value)
  172. ul.lineColor = NSUIColor.systemYellow.withAlphaComponent(0.5)
  173. BGChart.rightAxis.addLimitLine(ul)
  174. // Setup the main graph overall details
  175. BGChart.xAxis.valueFormatter = ChartXValueFormatter()
  176. BGChart.xAxis.granularity = 1800
  177. BGChart.xAxis.labelTextColor = NSUIColor.label
  178. BGChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
  179. BGChart.xAxis.drawGridLinesEnabled = false
  180. BGChart.leftAxis.enabled = true
  181. BGChart.leftAxis.labelPosition = YAxis.LabelPosition.insideChart
  182. BGChart.leftAxis.axisMaximum = maxBasal
  183. BGChart.leftAxis.axisMinimum = 0.0
  184. BGChart.leftAxis.drawGridLinesEnabled = false
  185. BGChart.rightAxis.labelTextColor = NSUIColor.label
  186. BGChart.rightAxis.labelPosition = YAxis.LabelPosition.insideChart
  187. BGChart.rightAxis.axisMinimum = Double(UserDefaultsRepository.minBGValue.value)
  188. BGChart.rightAxis.axisMaximum = Double(maxBG)
  189. BGChart.rightAxis.gridLineDashLengths = [5.0, 5.0]
  190. BGChart.rightAxis.valueFormatter = ChartYMMOLValueFormatter()
  191. BGChart.legend.enabled = false
  192. BGChart.scaleYEnabled = false
  193. BGChart.drawGridBackgroundEnabled = false
  194. //BGChart.gridBackgroundColor = NSUIColor.secondarySystemBackground
  195. BGChart.data = data
  196. BGChart.setExtraOffsets(left: 10, top: 10, right: 10, bottom: 10)
  197. }
  198. func updateBGGraphSettings() {
  199. let dataIndex = 0
  200. let dataIndexPrediction = 1
  201. let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  202. let linePrediction = BGChart.lineData!.dataSets[dataIndexPrediction] as! LineChartDataSet
  203. if UserDefaultsRepository.showLines.value {
  204. lineBG.lineWidth = 2
  205. linePrediction.lineWidth = 2
  206. } else {
  207. lineBG.lineWidth = 0
  208. linePrediction.lineWidth = 0
  209. }
  210. if UserDefaultsRepository.showDots.value {
  211. lineBG.drawCirclesEnabled = true
  212. linePrediction.drawCirclesEnabled = true
  213. } else {
  214. lineBG.drawCirclesEnabled = false
  215. linePrediction.drawCirclesEnabled = false
  216. }
  217. BGChart.rightAxis.axisMinimum = Double(UserDefaultsRepository.minBGValue.value)
  218. // Clear limit lines so they don't add multiples when changing the settings
  219. BGChart.rightAxis.removeAllLimitLines()
  220. //Add lower red line based on low alert value
  221. let ll = ChartLimitLine()
  222. ll.limit = Double(UserDefaultsRepository.lowLine.value)
  223. ll.lineColor = NSUIColor.systemRed.withAlphaComponent(0.5)
  224. BGChart.rightAxis.addLimitLine(ll)
  225. //Add upper yellow line based on low alert value
  226. let ul = ChartLimitLine()
  227. ul.limit = Double(UserDefaultsRepository.highLine.value)
  228. ul.lineColor = NSUIColor.systemYellow.withAlphaComponent(0.5)
  229. BGChart.rightAxis.addLimitLine(ul)
  230. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  231. BGChart.data?.notifyDataChanged()
  232. BGChart.notifyDataSetChanged()
  233. }
  234. func updateBGGraph() {
  235. let dataIndex = 0
  236. let entries = bgData
  237. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  238. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  239. mainChart.clear()
  240. smallChart.clear()
  241. var maxBG = UserDefaultsRepository.minBGScale.value
  242. var maxBGOffset: Float = 0.0
  243. if UserDefaultsRepository.offsetCarbsBolus.value {
  244. maxBGOffset = 40.0
  245. }
  246. var colors = [NSUIColor]()
  247. for i in 0..<entries.count{
  248. if Float(entries[i].sgv) > maxBG - maxBGOffset {
  249. maxBG = Float(entries[i].sgv) + maxBGOffset
  250. }
  251. let value = ChartDataEntry(x: Double(entries[i].date), y: Double(entries[i].sgv))
  252. mainChart.addEntry(value)
  253. smallChart.addEntry(value)
  254. if Double(entries[i].sgv) >= Double(UserDefaultsRepository.highLine.value) {
  255. colors.append(NSUIColor.systemYellow)
  256. } else if Double(entries[i].sgv) <= Double(UserDefaultsRepository.lowLine.value) {
  257. colors.append(NSUIColor.systemRed)
  258. } else {
  259. colors.append(NSUIColor.systemGreen)
  260. }
  261. }
  262. // Set Colors
  263. let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  264. let lineBGSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  265. lineBG.colors.removeAll()
  266. lineBG.circleColors.removeAll()
  267. lineBGSmall.colors.removeAll()
  268. lineBGSmall.circleColors.removeAll()
  269. if colors.count > 0 {
  270. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: colors") }
  271. for i in 0..<colors.count{
  272. mainChart.addColor(colors[i])
  273. mainChart.circleColors.append(colors[i])
  274. smallChart.addColor(colors[i])
  275. smallChart.circleColors.append(colors[i])
  276. }
  277. }
  278. BGChart.rightAxis.axisMaximum = Double(maxBG)
  279. BGChart.setVisibleXRangeMinimum(600)
  280. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  281. BGChart.data?.notifyDataChanged()
  282. BGChart.notifyDataSetChanged()
  283. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  284. BGChartFull.data?.notifyDataChanged()
  285. BGChartFull.notifyDataSetChanged()
  286. if firstGraphLoad {
  287. var scaleX = CGFloat(UserDefaultsRepository.chartScaleX.value)
  288. print("Scale: \(scaleX)")
  289. if( scaleX > CGFloat(ScaleXMax) ) {
  290. scaleX = CGFloat(ScaleXMax)
  291. UserDefaultsRepository.chartScaleX.value = ScaleXMax
  292. }
  293. BGChart.zoom(scaleX: scaleX, scaleY: 1, x: 1, y: 1)
  294. firstGraphLoad = false
  295. }
  296. if BGChart.chartXMax > dateTimeUtils.getNowTimeIntervalUTC() {
  297. BGChart.moveViewToAnimated(xValue: dateTimeUtils.getNowTimeIntervalUTC() - (BGChart.visibleXRange * 0.7), yValue: 0.0, axis: .right, duration: 1, easingOption: .easeInBack)
  298. }
  299. }
  300. func updatePredictionGraph() {
  301. let dataIndex = 1
  302. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  303. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  304. mainChart.clear()
  305. smallChart.clear()
  306. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: print prediction") }
  307. var colors = [NSUIColor]()
  308. for i in 0..<predictionData.count {
  309. var predictionVal = Double(predictionData[i].sgv)
  310. // Below can be turned on to prevent out of range on the graph if desired.
  311. // It currently just drops them out of view
  312. if predictionVal > 400 {
  313. predictionVal = 400
  314. colors.append(NSUIColor.systemYellow)
  315. } else if predictionVal < 0 {
  316. predictionVal = 0
  317. colors.append(NSUIColor.systemRed)
  318. } else {
  319. colors.append(NSUIColor.systemPurple)
  320. }
  321. let value = ChartDataEntry(x: predictionData[i].date, y: predictionVal)
  322. mainChart.addEntry(value)
  323. smallChart.addEntry(value)
  324. }
  325. smallChart.circleColors.removeAll()
  326. smallChart.colors.removeAll()
  327. mainChart.colors.removeAll()
  328. mainChart.circleColors.removeAll()
  329. if colors.count > 0 {
  330. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: prediction colors") }
  331. for i in 0..<colors.count{
  332. mainChart.addColor(colors[i])
  333. mainChart.circleColors.append(colors[i])
  334. smallChart.addColor(colors[i])
  335. smallChart.circleColors.append(colors[i])
  336. }
  337. }
  338. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  339. BGChart.data?.notifyDataChanged()
  340. BGChart.notifyDataSetChanged()
  341. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  342. BGChartFull.data?.notifyDataChanged()
  343. BGChartFull.notifyDataSetChanged()
  344. }
  345. func updateBasalGraph() {
  346. var dataIndex = 2
  347. BGChart.lineData?.dataSets[dataIndex].clear()
  348. var maxBasal = UserDefaultsRepository.minBasalScale.value
  349. for i in 0..<basalData.count{
  350. let value = ChartDataEntry(x: Double(basalData[i].date), y: Double(basalData[i].basalRate))
  351. BGChart.data?.dataSets[dataIndex].addEntry(value)
  352. if basalData[i].basalRate > maxBasal {
  353. maxBasal = basalData[i].basalRate
  354. }
  355. }
  356. BGChart.leftAxis.axisMaximum = maxBasal
  357. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  358. BGChart.data?.notifyDataChanged()
  359. BGChart.notifyDataSetChanged()
  360. }
  361. func updateBasalScheduledGraph() {
  362. var dataIndex = 5
  363. BGChart.lineData?.dataSets[dataIndex].clear()
  364. for i in 0..<basalScheduleData.count{
  365. let value = ChartDataEntry(x: Double(basalScheduleData[i].date), y: Double(basalScheduleData[i].basalRate))
  366. BGChart.data?.dataSets[dataIndex].addEntry(value)
  367. }
  368. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  369. BGChart.data?.notifyDataChanged()
  370. BGChart.notifyDataSetChanged()
  371. }
  372. func updateBolusGraph() {
  373. var dataIndex = 3
  374. BGChart.lineData?.dataSets[dataIndex].clear()
  375. for i in 0..<bolusData.count{
  376. let formatter = NumberFormatter()
  377. formatter.minimumFractionDigits = 0
  378. formatter.maximumFractionDigits = 2
  379. formatter.minimumIntegerDigits = 1
  380. var offset = 0
  381. if UserDefaultsRepository.offsetCarbsBolus.value {
  382. offset = 10
  383. }
  384. let value = ChartDataEntry(x: Double(bolusData[i].date), y: Double(bolusData[i].sgv + offset), data: formatter.string(from: NSNumber(value: bolusData[i].value)))
  385. BGChart.data?.dataSets[dataIndex].addEntry(value)
  386. }
  387. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  388. BGChart.data?.notifyDataChanged()
  389. BGChart.notifyDataSetChanged()
  390. }
  391. func updateCarbGraph() {
  392. var dataIndex = 4
  393. BGChart.lineData?.dataSets[dataIndex].clear()
  394. for i in 0..<carbData.count{
  395. let formatter = NumberFormatter()
  396. formatter.minimumFractionDigits = 0
  397. formatter.maximumFractionDigits = 2
  398. formatter.minimumIntegerDigits = 1
  399. var offset = 0
  400. if UserDefaultsRepository.offsetCarbsBolus.value {
  401. offset = 30
  402. }
  403. let value = ChartDataEntry(x: Double(carbData[i].date), y: Double(carbData[i].sgv + offset), data: formatter.string(from: NSNumber(value: carbData[i].value)))
  404. BGChart.data?.dataSets[dataIndex].addEntry(value)
  405. }
  406. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  407. BGChart.data?.notifyDataChanged()
  408. BGChart.notifyDataSetChanged()
  409. }
  410. func createSmallBGGraph(){
  411. let entries = bgData
  412. var bgChartEntry = [ChartDataEntry]()
  413. var colors = [NSUIColor]()
  414. let lineBG = LineChartDataSet(entries:bgChartEntry, label: "")
  415. lineBG.drawCirclesEnabled = false
  416. //line2.setDrawHighlightIndicators(false)
  417. lineBG.highlightEnabled = true
  418. lineBG.drawHorizontalHighlightIndicatorEnabled = false
  419. lineBG.drawVerticalHighlightIndicatorEnabled = false
  420. lineBG.highlightColor = NSUIColor.label
  421. lineBG.drawValuesEnabled = false
  422. lineBG.lineWidth = 2
  423. // Setup Prediction line details
  424. var predictionChartEntry = [ChartDataEntry]()
  425. let linePrediction = LineChartDataSet(entries:predictionChartEntry, label: "")
  426. linePrediction.drawCirclesEnabled = false
  427. //line2.setDrawHighlightIndicators(false)
  428. linePrediction.setColor(NSUIColor.systemPurple)
  429. linePrediction.highlightEnabled = true
  430. linePrediction.drawHorizontalHighlightIndicatorEnabled = false
  431. linePrediction.drawVerticalHighlightIndicatorEnabled = false
  432. linePrediction.highlightColor = NSUIColor.label
  433. linePrediction.drawValuesEnabled = false
  434. linePrediction.lineWidth = 2
  435. let data = LineChartData()
  436. data.addDataSet(lineBG)
  437. data.addDataSet(linePrediction)
  438. BGChartFull.highlightPerDragEnabled = true
  439. BGChartFull.leftAxis.enabled = false
  440. BGChartFull.rightAxis.enabled = false
  441. BGChartFull.xAxis.enabled = false
  442. BGChartFull.legend.enabled = false
  443. BGChartFull.scaleYEnabled = false
  444. BGChartFull.scaleXEnabled = false
  445. BGChartFull.drawGridBackgroundEnabled = false
  446. BGChartFull.data = data
  447. }
  448. func updateOverrideGraph() {
  449. var dataIndex = 6
  450. var chart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  451. chart.clear()
  452. var colors = [NSUIColor]()
  453. for i in 0..<overrideData.count{
  454. let value = ChartDataEntry(x: Double(overrideData[i].date), y: Double(overrideData[i].sgv), data: overrideData[i].value)
  455. BGChart.data?.dataSets[dataIndex].addEntry(value)
  456. if Double(overrideData[i].value) == 1.0 {
  457. colors.append(NSUIColor.systemGray.withAlphaComponent(0.0))
  458. } else if i >= overrideData.count - 2 {
  459. colors.append(NSUIColor.systemGreen)
  460. } else {
  461. colors.append(NSUIColor.systemGray.withAlphaComponent(CGFloat(overrideData[i].value / 2)))
  462. }
  463. }
  464. // Set Colors
  465. let line = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  466. line.colors.removeAll()
  467. line.circleColors.removeAll()
  468. if colors.count > 0 {
  469. for i in 0..<colors.count{
  470. chart.addColor(colors[i])
  471. chart.circleColors.append(colors[i])
  472. }
  473. }
  474. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  475. BGChart.data?.notifyDataChanged()
  476. BGChart.notifyDataSetChanged()
  477. }
  478. }