Graphs.swift 26 KB

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