Graphs.swift 25 KB

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