Graphs.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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. // Add Now Line
  175. createNowLine()
  176. startGraphNowTimer()
  177. // Setup the main graph overall details
  178. BGChart.xAxis.valueFormatter = ChartXValueFormatter()
  179. BGChart.xAxis.granularity = 1800
  180. BGChart.xAxis.labelTextColor = NSUIColor.label
  181. BGChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
  182. BGChart.xAxis.drawGridLinesEnabled = false
  183. BGChart.leftAxis.enabled = true
  184. BGChart.leftAxis.labelPosition = YAxis.LabelPosition.insideChart
  185. BGChart.leftAxis.axisMaximum = maxBasal
  186. BGChart.leftAxis.axisMinimum = 0.0
  187. BGChart.leftAxis.drawGridLinesEnabled = false
  188. BGChart.rightAxis.labelTextColor = NSUIColor.label
  189. BGChart.rightAxis.labelPosition = YAxis.LabelPosition.insideChart
  190. BGChart.rightAxis.axisMinimum = Double(UserDefaultsRepository.minBGValue.value)
  191. BGChart.rightAxis.axisMaximum = Double(maxBG)
  192. BGChart.rightAxis.gridLineDashLengths = [5.0, 5.0]
  193. BGChart.rightAxis.valueFormatter = ChartYMMOLValueFormatter()
  194. BGChart.legend.enabled = false
  195. BGChart.scaleYEnabled = false
  196. BGChart.drawGridBackgroundEnabled = false
  197. //BGChart.gridBackgroundColor = NSUIColor.secondarySystemBackground
  198. BGChart.data = data
  199. BGChart.setExtraOffsets(left: 10, top: 10, right: 10, bottom: 10)
  200. }
  201. func createNowLine() {
  202. BGChart.xAxis.removeAllLimitLines()
  203. let ul = ChartLimitLine()
  204. ul.limit = Double(dateTimeUtils.getNowTimeIntervalUTC())
  205. ul.lineColor = NSUIColor.systemGray.withAlphaComponent(0.5)
  206. ul.lineWidth = 1
  207. BGChart.xAxis.addLimitLine(ul)
  208. }
  209. func updateBGGraphSettings() {
  210. let dataIndex = 0
  211. let dataIndexPrediction = 1
  212. let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  213. let linePrediction = BGChart.lineData!.dataSets[dataIndexPrediction] as! LineChartDataSet
  214. if UserDefaultsRepository.showLines.value {
  215. lineBG.lineWidth = 2
  216. linePrediction.lineWidth = 2
  217. } else {
  218. lineBG.lineWidth = 0
  219. linePrediction.lineWidth = 0
  220. }
  221. if UserDefaultsRepository.showDots.value {
  222. lineBG.drawCirclesEnabled = true
  223. linePrediction.drawCirclesEnabled = true
  224. } else {
  225. lineBG.drawCirclesEnabled = false
  226. linePrediction.drawCirclesEnabled = false
  227. }
  228. BGChart.rightAxis.axisMinimum = Double(UserDefaultsRepository.minBGValue.value)
  229. // Clear limit lines so they don't add multiples when changing the settings
  230. BGChart.rightAxis.removeAllLimitLines()
  231. //Add lower red line based on low alert value
  232. let ll = ChartLimitLine()
  233. ll.limit = Double(UserDefaultsRepository.lowLine.value)
  234. ll.lineColor = NSUIColor.systemRed.withAlphaComponent(0.5)
  235. BGChart.rightAxis.addLimitLine(ll)
  236. //Add upper yellow line based on low alert value
  237. let ul = ChartLimitLine()
  238. ul.limit = Double(UserDefaultsRepository.highLine.value)
  239. ul.lineColor = NSUIColor.systemYellow.withAlphaComponent(0.5)
  240. BGChart.rightAxis.addLimitLine(ul)
  241. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  242. BGChart.data?.notifyDataChanged()
  243. BGChart.notifyDataSetChanged()
  244. }
  245. func updateBGGraph() {
  246. let dataIndex = 0
  247. let entries = bgData
  248. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  249. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  250. mainChart.clear()
  251. smallChart.clear()
  252. var maxBG = UserDefaultsRepository.minBGScale.value
  253. var maxBGOffset: Float = 0.0
  254. if UserDefaultsRepository.offsetCarbsBolus.value {
  255. maxBGOffset = 40.0
  256. }
  257. var colors = [NSUIColor]()
  258. for i in 0..<entries.count{
  259. if Float(entries[i].sgv) > maxBG - maxBGOffset {
  260. maxBG = Float(entries[i].sgv) + maxBGOffset
  261. }
  262. let value = ChartDataEntry(x: Double(entries[i].date), y: Double(entries[i].sgv))
  263. mainChart.addEntry(value)
  264. smallChart.addEntry(value)
  265. if Double(entries[i].sgv) >= Double(UserDefaultsRepository.highLine.value) {
  266. colors.append(NSUIColor.systemYellow)
  267. } else if Double(entries[i].sgv) <= Double(UserDefaultsRepository.lowLine.value) {
  268. colors.append(NSUIColor.systemRed)
  269. } else {
  270. colors.append(NSUIColor.systemGreen)
  271. }
  272. }
  273. // Set Colors
  274. let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  275. let lineBGSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  276. lineBG.colors.removeAll()
  277. lineBG.circleColors.removeAll()
  278. lineBGSmall.colors.removeAll()
  279. lineBGSmall.circleColors.removeAll()
  280. if colors.count > 0 {
  281. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: colors") }
  282. for i in 0..<colors.count{
  283. mainChart.addColor(colors[i])
  284. mainChart.circleColors.append(colors[i])
  285. smallChart.addColor(colors[i])
  286. smallChart.circleColors.append(colors[i])
  287. }
  288. }
  289. BGChart.rightAxis.axisMaximum = Double(maxBG)
  290. BGChart.setVisibleXRangeMinimum(600)
  291. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  292. BGChart.data?.notifyDataChanged()
  293. BGChart.notifyDataSetChanged()
  294. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  295. BGChartFull.data?.notifyDataChanged()
  296. BGChartFull.notifyDataSetChanged()
  297. if firstGraphLoad {
  298. var scaleX = CGFloat(UserDefaultsRepository.chartScaleX.value)
  299. print("Scale: \(scaleX)")
  300. if( scaleX > CGFloat(ScaleXMax) ) {
  301. scaleX = CGFloat(ScaleXMax)
  302. UserDefaultsRepository.chartScaleX.value = ScaleXMax
  303. }
  304. BGChart.zoom(scaleX: scaleX, scaleY: 1, x: 1, y: 1)
  305. firstGraphLoad = false
  306. }
  307. if BGChart.chartXMax > dateTimeUtils.getNowTimeIntervalUTC() {
  308. BGChart.moveViewToAnimated(xValue: dateTimeUtils.getNowTimeIntervalUTC() - (BGChart.visibleXRange * 0.7), yValue: 0.0, axis: .right, duration: 1, easingOption: .easeInBack)
  309. }
  310. }
  311. func updatePredictionGraph() {
  312. let dataIndex = 1
  313. var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  314. var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
  315. mainChart.clear()
  316. smallChart.clear()
  317. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: print prediction") }
  318. var colors = [NSUIColor]()
  319. for i in 0..<predictionData.count {
  320. var predictionVal = Double(predictionData[i].sgv)
  321. // Below can be turned on to prevent out of range on the graph if desired.
  322. // It currently just drops them out of view
  323. if predictionVal > 400 {
  324. predictionVal = 400
  325. colors.append(NSUIColor.systemYellow)
  326. } else if predictionVal < 0 {
  327. predictionVal = 0
  328. colors.append(NSUIColor.systemRed)
  329. } else {
  330. colors.append(NSUIColor.systemPurple)
  331. }
  332. let value = ChartDataEntry(x: predictionData[i].date, y: predictionVal)
  333. mainChart.addEntry(value)
  334. smallChart.addEntry(value)
  335. }
  336. smallChart.circleColors.removeAll()
  337. smallChart.colors.removeAll()
  338. mainChart.colors.removeAll()
  339. mainChart.circleColors.removeAll()
  340. if colors.count > 0 {
  341. if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: prediction colors") }
  342. for i in 0..<colors.count{
  343. mainChart.addColor(colors[i])
  344. mainChart.circleColors.append(colors[i])
  345. smallChart.addColor(colors[i])
  346. smallChart.circleColors.append(colors[i])
  347. }
  348. }
  349. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  350. BGChart.data?.notifyDataChanged()
  351. BGChart.notifyDataSetChanged()
  352. BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
  353. BGChartFull.data?.notifyDataChanged()
  354. BGChartFull.notifyDataSetChanged()
  355. }
  356. func updateBasalGraph() {
  357. var dataIndex = 2
  358. BGChart.lineData?.dataSets[dataIndex].clear()
  359. var maxBasal = UserDefaultsRepository.minBasalScale.value
  360. for i in 0..<basalData.count{
  361. let value = ChartDataEntry(x: Double(basalData[i].date), y: Double(basalData[i].basalRate))
  362. BGChart.data?.dataSets[dataIndex].addEntry(value)
  363. if basalData[i].basalRate > maxBasal {
  364. maxBasal = basalData[i].basalRate
  365. }
  366. }
  367. BGChart.leftAxis.axisMaximum = maxBasal
  368. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  369. BGChart.data?.notifyDataChanged()
  370. BGChart.notifyDataSetChanged()
  371. }
  372. func updateBasalScheduledGraph() {
  373. var dataIndex = 5
  374. BGChart.lineData?.dataSets[dataIndex].clear()
  375. for i in 0..<basalScheduleData.count{
  376. let value = ChartDataEntry(x: Double(basalScheduleData[i].date), y: Double(basalScheduleData[i].basalRate))
  377. BGChart.data?.dataSets[dataIndex].addEntry(value)
  378. }
  379. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  380. BGChart.data?.notifyDataChanged()
  381. BGChart.notifyDataSetChanged()
  382. }
  383. func updateBolusGraph() {
  384. var dataIndex = 3
  385. BGChart.lineData?.dataSets[dataIndex].clear()
  386. for i in 0..<bolusData.count{
  387. let formatter = NumberFormatter()
  388. formatter.minimumFractionDigits = 0
  389. formatter.maximumFractionDigits = 2
  390. formatter.minimumIntegerDigits = 1
  391. var offset = 0
  392. if UserDefaultsRepository.offsetCarbsBolus.value {
  393. offset = 10
  394. }
  395. let value = ChartDataEntry(x: Double(bolusData[i].date), y: Double(bolusData[i].sgv + offset), data: formatter.string(from: NSNumber(value: bolusData[i].value)))
  396. BGChart.data?.dataSets[dataIndex].addEntry(value)
  397. }
  398. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  399. BGChart.data?.notifyDataChanged()
  400. BGChart.notifyDataSetChanged()
  401. }
  402. func updateCarbGraph() {
  403. var dataIndex = 4
  404. BGChart.lineData?.dataSets[dataIndex].clear()
  405. for i in 0..<carbData.count{
  406. let formatter = NumberFormatter()
  407. formatter.minimumFractionDigits = 0
  408. formatter.maximumFractionDigits = 2
  409. formatter.minimumIntegerDigits = 1
  410. var offset = 0
  411. if UserDefaultsRepository.offsetCarbsBolus.value {
  412. offset = 30
  413. }
  414. let value = ChartDataEntry(x: Double(carbData[i].date), y: Double(carbData[i].sgv + offset), data: formatter.string(from: NSNumber(value: carbData[i].value)))
  415. BGChart.data?.dataSets[dataIndex].addEntry(value)
  416. }
  417. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  418. BGChart.data?.notifyDataChanged()
  419. BGChart.notifyDataSetChanged()
  420. }
  421. func createSmallBGGraph(){
  422. let entries = bgData
  423. var bgChartEntry = [ChartDataEntry]()
  424. var colors = [NSUIColor]()
  425. let lineBG = LineChartDataSet(entries:bgChartEntry, label: "")
  426. lineBG.drawCirclesEnabled = false
  427. //line2.setDrawHighlightIndicators(false)
  428. lineBG.highlightEnabled = true
  429. lineBG.drawHorizontalHighlightIndicatorEnabled = false
  430. lineBG.drawVerticalHighlightIndicatorEnabled = false
  431. lineBG.highlightColor = NSUIColor.label
  432. lineBG.drawValuesEnabled = false
  433. lineBG.lineWidth = 2
  434. // Setup Prediction line details
  435. var predictionChartEntry = [ChartDataEntry]()
  436. let linePrediction = LineChartDataSet(entries:predictionChartEntry, label: "")
  437. linePrediction.drawCirclesEnabled = false
  438. //line2.setDrawHighlightIndicators(false)
  439. linePrediction.setColor(NSUIColor.systemPurple)
  440. linePrediction.highlightEnabled = true
  441. linePrediction.drawHorizontalHighlightIndicatorEnabled = false
  442. linePrediction.drawVerticalHighlightIndicatorEnabled = false
  443. linePrediction.highlightColor = NSUIColor.label
  444. linePrediction.drawValuesEnabled = false
  445. linePrediction.lineWidth = 2
  446. let data = LineChartData()
  447. data.addDataSet(lineBG)
  448. data.addDataSet(linePrediction)
  449. BGChartFull.highlightPerDragEnabled = true
  450. BGChartFull.leftAxis.enabled = false
  451. BGChartFull.rightAxis.enabled = false
  452. BGChartFull.xAxis.enabled = false
  453. BGChartFull.legend.enabled = false
  454. BGChartFull.scaleYEnabled = false
  455. BGChartFull.scaleXEnabled = false
  456. BGChartFull.drawGridBackgroundEnabled = false
  457. BGChartFull.data = data
  458. }
  459. func updateOverrideGraph() {
  460. var dataIndex = 6
  461. var chart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  462. chart.clear()
  463. var colors = [NSUIColor]()
  464. for i in 0..<overrideData.count{
  465. let value = ChartDataEntry(x: Double(overrideData[i].date), y: Double(overrideData[i].sgv), data: overrideData[i].value)
  466. BGChart.data?.dataSets[dataIndex].addEntry(value)
  467. if Double(overrideData[i].value) == 1.0 {
  468. colors.append(NSUIColor.systemGray.withAlphaComponent(0.0))
  469. } else if i >= overrideData.count - 2 {
  470. colors.append(NSUIColor.systemGreen)
  471. } else {
  472. colors.append(NSUIColor.systemGray.withAlphaComponent(CGFloat(overrideData[i].value / 2)))
  473. }
  474. }
  475. // Set Colors
  476. let line = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
  477. line.colors.removeAll()
  478. line.circleColors.removeAll()
  479. if colors.count > 0 {
  480. for i in 0..<colors.count{
  481. chart.addColor(colors[i])
  482. chart.circleColors.append(colors[i])
  483. }
  484. }
  485. BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
  486. BGChart.data?.notifyDataChanged()
  487. BGChart.notifyDataSetChanged()
  488. }
  489. }