Graphs.swift 22 KB

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