Graphs.swift 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // rewrite this func
  13. func chartTranslated(_ chartView: ChartViewBase, dX: CGFloat, dY: CGFloat) {
  14. print("chartTranslated")
  15. if chartView == BGChart {
  16. let currentMatrix = chartView.viewPortHandler.touchMatrix
  17. BasalChart.viewPortHandler.refresh(newMatrix: currentMatrix, chart: BasalChart, invalidate: true)
  18. }else {
  19. let currentMatrix = BasalChart.viewPortHandler.touchMatrix
  20. BGChart.viewPortHandler.refresh(newMatrix: currentMatrix, chart: BGChart, invalidate: true)
  21. }
  22. }
  23. func createGraph(entries: [sgvData]){
  24. var bgChartEntry = [ChartDataEntry]()
  25. var colors = [NSUIColor]()
  26. var maxBG: Int = 250
  27. for i in 0..<entries.count{
  28. var dateString = String(entries[i].date).prefix(10)
  29. let dateSecondsOnly = Double(String(dateString))!
  30. if entries[i].sgv > maxBG {
  31. maxBG = entries[i].sgv
  32. }
  33. let value = ChartDataEntry(x: Double(entries[i].date), y: Double(entries[i].sgv))
  34. bgChartEntry.append(value)
  35. if Double(entries[i].sgv) >= Double(UserDefaultsRepository.highLine.value) {
  36. colors.append(NSUIColor.systemYellow)
  37. } else if Double(entries[i].sgv) <= Double(UserDefaultsRepository.lowLine.value) {
  38. colors.append(NSUIColor.systemRed)
  39. } else {
  40. colors.append(NSUIColor.systemGreen)
  41. }
  42. }
  43. // Add Prediction Data
  44. if predictionData.count > 0 {
  45. var startingTime = bgChartEntry[bgChartEntry.count - 1].x + 300
  46. var i = 0
  47. // Add 1 hour of predictions
  48. while i < 12 {
  49. var predictionVal = Double(predictionData[i])
  50. // Below can be turned on to prevent out of range on the graph if desired.
  51. // It currently just drops them out of view
  52. if predictionVal > 400 {
  53. // predictionVal = 400
  54. } else if predictionVal < 0 {
  55. // predictionVal = 0
  56. }
  57. let value = ChartDataEntry(x: startingTime + 5, y: predictionVal)
  58. bgChartEntry.append(value)
  59. colors.append(NSUIColor.systemPurple)
  60. startingTime += 300
  61. i += 1
  62. }
  63. }
  64. let line1 = LineChartDataSet(entries:bgChartEntry, label: "")
  65. line1.circleRadius = 3
  66. line1.circleColors = [NSUIColor.systemGreen]
  67. line1.drawCircleHoleEnabled = false
  68. if UserDefaultsRepository.showLines.value {
  69. line1.lineWidth = 2
  70. } else {
  71. line1.lineWidth = 0
  72. }
  73. if UserDefaultsRepository.showDots.value {
  74. line1.drawCirclesEnabled = true
  75. } else {
  76. line1.drawCirclesEnabled = false
  77. }
  78. line1.setDrawHighlightIndicators(false)
  79. line1.valueFont.withSize(50)
  80. for i in 1..<colors.count{
  81. line1.addColor(colors[i])
  82. line1.circleColors.append(colors[i])
  83. }
  84. let data = LineChartData()
  85. data.addDataSet(line1)
  86. data.setValueFont(UIFont(name: UIFont.systemFont(ofSize: 10).fontName, size: 10)!)
  87. data.setDrawValues(false)
  88. // Clear limit lines so they don't add multiples when changing the settings
  89. BGChart.rightAxis.removeAllLimitLines()
  90. //Add lower red line based on low alert value
  91. let ll = ChartLimitLine()
  92. ll.limit = Double(UserDefaultsRepository.lowLine.value)
  93. ll.lineColor = NSUIColor.systemRed
  94. BGChart.rightAxis.addLimitLine(ll)
  95. //Add upper yellow line based on low alert value
  96. let ul = ChartLimitLine()
  97. ul.limit = Double(UserDefaultsRepository.highLine.value)
  98. ul.lineColor = NSUIColor.systemYellow
  99. BGChart.rightAxis.addLimitLine(ul)
  100. BGChart.xAxis.valueFormatter = ChartXValueFormatter()
  101. BGChart.xAxis.granularity = 1800
  102. BGChart.xAxis.labelTextColor = NSUIColor.label
  103. BGChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
  104. BGChart.rightAxis.labelTextColor = NSUIColor.label
  105. BGChart.rightAxis.labelPosition = YAxis.LabelPosition.insideChart
  106. BGChart.rightAxis.axisMinimum = 40
  107. BGChart.leftAxis.axisMinimum = 40
  108. BGChart.rightAxis.axisMaximum = Double(maxBG)
  109. BGChart.leftAxis.axisMaximum = Double(maxBG)
  110. BGChart.leftAxis.enabled = false
  111. BGChart.legend.enabled = false
  112. BGChart.scaleYEnabled = false
  113. BGChart.data = data
  114. BGChart.setExtraOffsets(left: 10, top: 10, right: 10, bottom: 10)
  115. BGChart.setVisibleXRangeMinimum(10)
  116. BGChart.drawGridBackgroundEnabled = true
  117. BGChart.gridBackgroundColor = NSUIColor.secondarySystemBackground
  118. if firstGraphLoad {
  119. BGChart.zoom(scaleX: 18, scaleY: 1, x: 1, y: 1)
  120. firstGraphLoad = false
  121. }
  122. // 7000 only shows 30 minutes of the hour predictions, leaving the rest on the right of the screen requiring a scroll
  123. BGChart.moveViewToX(Date().timeIntervalSince1970)
  124. createSmallBGGraph(bgChartEntry: bgChartEntry, colors: colors)
  125. }
  126. func createSmallBGGraph(bgChartEntry: [ChartDataEntry], colors: [NSUIColor]){
  127. //24 Hour Small Graph
  128. let line2 = LineChartDataSet(entries:bgChartEntry, label: "Number")
  129. line2.drawCirclesEnabled = false
  130. line2.setDrawHighlightIndicators(false)
  131. line2.lineWidth = 1
  132. for i in 1..<colors.count{
  133. line2.addColor(colors[i])
  134. line2.circleColors.append(colors[i])
  135. }
  136. let data2 = LineChartData()
  137. data2.addDataSet(line2)
  138. BGChartFull.leftAxis.enabled = false
  139. BGChartFull.rightAxis.enabled = false
  140. BGChartFull.xAxis.enabled = false
  141. BGChartFull.legend.enabled = false
  142. BGChartFull.scaleYEnabled = false
  143. BGChartFull.scaleXEnabled = false
  144. BGChartFull.drawGridBackgroundEnabled = false
  145. BGChartFull.data = data2
  146. }
  147. func createBasalGraph(entries: [basalGraphStruct]){
  148. var chartEntry = [ChartDataEntry]()
  149. for i in 0..<entries.count{
  150. let value = ChartDataEntry(x: Double(entries[i].date), y: Double(entries[i].basalRate))
  151. chartEntry.append(value)
  152. }
  153. let line1 = LineChartDataSet(entries:chartEntry, label: "")
  154. line1.circleRadius = 3
  155. line1.circleColors = [NSUIColor.systemBlue]
  156. line1.drawCircleHoleEnabled = false
  157. line1.setDrawHighlightIndicators(false)
  158. line1.setColor(NSUIColor.systemBlue, alpha: 1.0)
  159. line1.lineWidth = 3
  160. line1.drawFilledEnabled = true
  161. line1.fillColor = NSUIColor.systemBlue
  162. line1.drawCirclesEnabled = false
  163. let data = LineChartData()
  164. data.addDataSet(line1)
  165. data.setValueFont(UIFont(name: UIFont.systemFont(ofSize: 10).fontName, size: 10)!)
  166. data.setDrawValues(false)
  167. BasalChart.xAxis.valueFormatter = ChartXValueFormatter()
  168. BasalChart.xAxis.granularity = 1800
  169. BasalChart.xAxis.labelTextColor = NSUIColor.label
  170. BasalChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
  171. BasalChart.rightAxis.labelTextColor = NSUIColor.label
  172. BasalChart.rightAxis.labelPosition = YAxis.LabelPosition.insideChart
  173. BasalChart.leftAxis.enabled = false
  174. BasalChart.legend.enabled = false
  175. BasalChart.scaleYEnabled = false
  176. BasalChart.data = data
  177. BasalChart.setExtraOffsets(left: 10, top: 10, right: 10, bottom: 10)
  178. BasalChart.setVisibleXRangeMinimum(10)
  179. BasalChart.drawGridBackgroundEnabled = true
  180. BasalChart.gridBackgroundColor = NSUIColor.secondarySystemBackground
  181. if firstBasalGraphLoad {
  182. BasalChart.zoom(scaleX: 18, scaleY: 1, x: 1, y: 1)
  183. firstBasalGraphLoad = false
  184. }
  185. // 7000 only shows 30 minutes of the hour predictions, leaving the rest on the right of the screen requiring a scroll
  186. BasalChart.moveViewToX(Date().timeIntervalSince1970)
  187. // Bar Chart Build
  188. /*var chartEntry = [BarChartDataEntry]()
  189. for i in 0..<entries.count{
  190. let value = BarChartDataEntry(x: Double(entries[i].date), y: Double(entries[i].basalRate))
  191. chartEntry.append(value)
  192. }
  193. let bar1 = BarChartDataSet(entries:chartEntry, label: "")
  194. bar1.setColor(NSUIColor.systemBlue, alpha: 0.5)
  195. let data = BarChartData()
  196. data.addDataSet(bar1)
  197. data.setValueFont(UIFont(name: UIFont.systemFont(ofSize: 10).fontName, size: 10)!)
  198. data
  199. data.barWidth = 290
  200. BasalChart.xAxis.valueFormatter = ChartXValueFormatter()
  201. BasalChart.xAxis.granularity = 1800
  202. BasalChart.data = data
  203. BasalChart.legend.enabled = false
  204. BasalChart.xAxis.drawGridLinesEnabled = false
  205. BasalChart.leftAxis.drawGridLinesEnabled = false
  206. BasalChart.rightAxis.drawGridLinesEnabled = false
  207. BasalChart.leftAxis.enabled = false
  208. BasalChart.scaleYEnabled = false
  209. if firstBasalGraphLoad {
  210. BasalChart.zoom(scaleX: 18, scaleY: 1, x: 1, y: 1)
  211. firstBasalGraphLoad = false
  212. }
  213. BasalChart.moveViewToX(BGChart.chartXMax)
  214. */
  215. }
  216. }