| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277 |
- //
- // Graphs.swift
- // LoopFollow
- //
- // Created by Jon Fawcett on 6/16/20.
- // Copyright © 2020 Jon Fawcett. All rights reserved.
- //
- import Foundation
- import Charts
- import UIKit
- let ScaleXMax:Float = 150.0
- extension MainViewController {
-
- func chartValueSelected(_ chartView: ChartViewBase, entry: ChartDataEntry, highlight: Highlight) {
- if chartView == BGChartFull {
- BGChart.moveViewToX(entry.x)
- }
- if entry.data as? String == "hide"{
- BGChart.highlightValue(nil, callDelegate: false)
- }
-
- }
-
- func chartScaled(_ chartView: ChartViewBase, scaleX: CGFloat, scaleY: CGFloat) {
- print("Chart Scaled: \(BGChart.scaleX), \(BGChart.scaleY)")
-
- // dont store huge values
- var scale: Float = Float(BGChart.scaleX)
- if(scale > ScaleXMax ) {
- scale = ScaleXMax
- }
- UserDefaultsRepository.chartScaleX.value = Float(scale)
- }
- func createGraph(){
- self.BGChart.clear()
-
- // Create the BG Graph Data
- let entries = bgData
- var bgChartEntry = [ChartDataEntry]()
- var colors = [NSUIColor]()
- var maxBG: Float = UserDefaultsRepository.minBGScale.value
-
- // Setup BG line details
- let lineBG = LineChartDataSet(entries:bgChartEntry, label: "")
- lineBG.circleRadius = CGFloat(globalVariables.dotBG)
- lineBG.circleColors = [NSUIColor.systemGreen]
- lineBG.drawCircleHoleEnabled = false
- lineBG.axisDependency = YAxis.AxisDependency.right
- lineBG.highlightEnabled = true
- lineBG.drawValuesEnabled = false
-
- if UserDefaultsRepository.showLines.value {
- lineBG.lineWidth = 2
- } else {
- lineBG.lineWidth = 0
- }
- if UserDefaultsRepository.showDots.value {
- lineBG.drawCirclesEnabled = true
- } else {
- lineBG.drawCirclesEnabled = false
- }
- lineBG.setDrawHighlightIndicators(false)
- lineBG.valueFont.withSize(50)
-
- // Setup Prediction line details
- var predictionChartEntry = [ChartDataEntry]()
- let linePrediction = LineChartDataSet(entries:predictionChartEntry, label: "")
- linePrediction.circleRadius = CGFloat(globalVariables.dotBG)
- linePrediction.circleColors = [NSUIColor.systemPurple]
- linePrediction.colors = [NSUIColor.systemPurple]
- linePrediction.drawCircleHoleEnabled = false
- linePrediction.axisDependency = YAxis.AxisDependency.right
- linePrediction.highlightEnabled = true
- linePrediction.drawValuesEnabled = false
-
- if UserDefaultsRepository.showLines.value {
- linePrediction.lineWidth = 2
- } else {
- linePrediction.lineWidth = 0
- }
- if UserDefaultsRepository.showDots.value {
- linePrediction.drawCirclesEnabled = true
- } else {
- linePrediction.drawCirclesEnabled = false
- }
- linePrediction.setDrawHighlightIndicators(false)
- linePrediction.valueFont.withSize(50)
-
-
- // create Basal graph data
- var chartEntry = [ChartDataEntry]()
- var maxBasal = UserDefaultsRepository.minBasalScale.value
- let lineBasal = LineChartDataSet(entries:chartEntry, label: "")
- lineBasal.setDrawHighlightIndicators(false)
- lineBasal.setColor(NSUIColor.systemBlue, alpha: 0.5)
- lineBasal.lineWidth = 0
- lineBasal.drawFilledEnabled = true
- lineBasal.fillColor = NSUIColor.systemBlue
- lineBasal.fillAlpha = 0.5
- lineBasal.drawCirclesEnabled = false
- lineBasal.axisDependency = YAxis.AxisDependency.left
- lineBasal.highlightEnabled = true
- lineBasal.drawValuesEnabled = false
- lineBasal.fillFormatter = basalFillFormatter()
-
- // Boluses
- var chartEntryBolus = [ChartDataEntry]()
- let lineBolus = LineChartDataSet(entries:chartEntryBolus, label: "")
- lineBolus.circleRadius = CGFloat(globalVariables.dotBolus)
- lineBolus.circleColors = [NSUIColor.systemBlue.withAlphaComponent(0.75)]
- lineBolus.drawCircleHoleEnabled = false
- lineBolus.setDrawHighlightIndicators(false)
- lineBolus.setColor(NSUIColor.systemBlue, alpha: 1.0)
- lineBolus.lineWidth = 0
- lineBolus.axisDependency = YAxis.AxisDependency.right
- lineBolus.valueFormatter = ChartYDataValueFormatter()
- lineBolus.valueTextColor = NSUIColor.label
- lineBolus.fillColor = NSUIColor.systemBlue
- lineBolus.fillAlpha = 0.6
-
- lineBolus.drawCirclesEnabled = true
- lineBolus.drawFilledEnabled = false
-
- if UserDefaultsRepository.showValues.value {
- lineBolus.drawValuesEnabled = true
- lineBolus.highlightEnabled = false
- } else {
- lineBolus.drawValuesEnabled = false
- lineBolus.highlightEnabled = true
- }
-
-
- // Carbs
- var chartEntryCarbs = [ChartDataEntry]()
- let lineCarbs = LineChartDataSet(entries:chartEntryCarbs, label: "")
- lineCarbs.circleRadius = CGFloat(globalVariables.dotCarb)
- lineCarbs.circleColors = [NSUIColor.systemOrange.withAlphaComponent(0.75)]
- lineCarbs.drawCircleHoleEnabled = false
- lineCarbs.setDrawHighlightIndicators(false)
- lineCarbs.setColor(NSUIColor.systemBlue, alpha: 1.0)
- lineCarbs.lineWidth = 0
- lineCarbs.axisDependency = YAxis.AxisDependency.right
- lineCarbs.valueFormatter = ChartYDataValueFormatter()
- lineCarbs.valueTextColor = NSUIColor.label
- lineCarbs.fillColor = NSUIColor.systemOrange
- lineCarbs.fillAlpha = 0.6
-
- lineCarbs.drawCirclesEnabled = true
- lineCarbs.drawFilledEnabled = false
-
- if UserDefaultsRepository.showValues.value {
- lineCarbs.drawValuesEnabled = true
- lineCarbs.highlightEnabled = false
- } else {
- lineCarbs.drawValuesEnabled = false
- lineCarbs.highlightEnabled = true
- }
-
-
- // create Scheduled Basal graph data
- var chartBasalScheduledEntry = [ChartDataEntry]()
- let lineBasalScheduled = LineChartDataSet(entries:chartBasalScheduledEntry, label: "")
- lineBasalScheduled.setDrawHighlightIndicators(false)
- lineBasalScheduled.setColor(NSUIColor.systemBlue, alpha: 0.8)
- lineBasalScheduled.lineWidth = 2
- lineBasalScheduled.drawFilledEnabled = false
- lineBasalScheduled.drawCirclesEnabled = false
- lineBasalScheduled.axisDependency = YAxis.AxisDependency.left
- lineBasalScheduled.highlightEnabled = false
- lineBasalScheduled.drawValuesEnabled = false
- lineBasalScheduled.lineDashLengths = [10.0, 5.0]
-
- // create Override graph data
- var chartOverrideEntry = [ChartDataEntry]()
- let lineOverride = LineChartDataSet(entries:chartOverrideEntry, label: "")
- lineOverride.setDrawHighlightIndicators(false)
- lineOverride.lineWidth = 0
- lineOverride.drawFilledEnabled = true
- lineOverride.fillFormatter = OverrideFillFormatter()
- lineOverride.fillColor = NSUIColor.systemGreen
- lineOverride.fillAlpha = 0.6
- lineOverride.drawCirclesEnabled = false
- lineOverride.axisDependency = YAxis.AxisDependency.right
- lineOverride.highlightEnabled = true
- lineOverride.drawValuesEnabled = false
- // lineOverride.circleColors = [NSUIColor.systemGreen.withAlphaComponent(0.75)]
- // lineOverride.valueFormatter = ChartYDataValueFormatter()
- // lineOverride.drawCircleHoleEnabled = false
-
- // BG Check
- var chartEntryBGCheck = [ChartDataEntry]()
- let lineBGCheck = LineChartDataSet(entries:chartEntryBGCheck, label: "")
- lineBGCheck.circleRadius = CGFloat(globalVariables.dotOther)
- lineBGCheck.circleColors = [NSUIColor.systemRed.withAlphaComponent(0.75)]
- lineBGCheck.drawCircleHoleEnabled = false
- lineBGCheck.setDrawHighlightIndicators(false)
- lineBGCheck.setColor(NSUIColor.systemRed, alpha: 1.0)
- lineBGCheck.drawCirclesEnabled = true
- lineBGCheck.lineWidth = 0
- lineBGCheck.highlightEnabled = true
- lineBGCheck.axisDependency = YAxis.AxisDependency.right
- lineBGCheck.valueFormatter = ChartYDataValueFormatter()
- lineBGCheck.drawValuesEnabled = false
-
- // Suspend Pump
- var chartEntrySuspend = [ChartDataEntry]()
- let lineSuspend = LineChartDataSet(entries:chartEntrySuspend, label: "")
- lineSuspend.circleRadius = CGFloat(globalVariables.dotOther)
- lineSuspend.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
- lineSuspend.drawCircleHoleEnabled = false
- lineSuspend.setDrawHighlightIndicators(false)
- lineSuspend.setColor(NSUIColor.systemGray2, alpha: 1.0)
- lineSuspend.drawCirclesEnabled = true
- lineSuspend.lineWidth = 0
- lineSuspend.highlightEnabled = true
- lineSuspend.axisDependency = YAxis.AxisDependency.right
- lineSuspend.valueFormatter = ChartYDataValueFormatter()
- lineSuspend.drawValuesEnabled = false
-
- // Resume Pump
- var chartEntryResume = [ChartDataEntry]()
- let lineResume = LineChartDataSet(entries:chartEntryResume, label: "")
- lineResume.circleRadius = CGFloat(globalVariables.dotOther)
- lineResume.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
- lineResume.drawCircleHoleEnabled = false
- lineResume.setDrawHighlightIndicators(false)
- lineResume.setColor(NSUIColor.systemGray4, alpha: 1.0)
- lineResume.drawCirclesEnabled = true
- lineResume.lineWidth = 0
- lineResume.highlightEnabled = true
- lineResume.axisDependency = YAxis.AxisDependency.right
- lineResume.valueFormatter = ChartYDataValueFormatter()
- lineResume.drawValuesEnabled = false
-
- // Sensor Start
- var chartEntrySensor = [ChartDataEntry]()
- let lineSensor = LineChartDataSet(entries:chartEntrySensor, label: "")
- lineSensor.circleRadius = CGFloat(globalVariables.dotOther)
- lineSensor.circleColors = [NSUIColor.systemIndigo.withAlphaComponent(0.75)]
- lineSensor.drawCircleHoleEnabled = false
- lineSensor.setDrawHighlightIndicators(false)
- lineSensor.setColor(NSUIColor.systemGray3, alpha: 1.0)
- lineSensor.drawCirclesEnabled = true
- lineSensor.lineWidth = 0
- lineSensor.highlightEnabled = true
- lineSensor.axisDependency = YAxis.AxisDependency.right
- lineSensor.valueFormatter = ChartYDataValueFormatter()
- lineSensor.drawValuesEnabled = false
-
- // Notes
- var chartEntryNote = [ChartDataEntry]()
- let lineNote = LineChartDataSet(entries:chartEntryNote, label: "")
- lineNote.circleRadius = CGFloat(globalVariables.dotOther)
- lineNote.circleColors = [NSUIColor.systemGray.withAlphaComponent(0.75)]
- lineNote.drawCircleHoleEnabled = false
- lineNote.setDrawHighlightIndicators(false)
- lineNote.setColor(NSUIColor.systemGray3, alpha: 1.0)
- lineNote.drawCirclesEnabled = true
- lineNote.lineWidth = 0
- lineNote.highlightEnabled = true
- lineNote.axisDependency = YAxis.AxisDependency.right
- lineNote.valueFormatter = ChartYDataValueFormatter()
- lineNote.drawValuesEnabled = false
-
- // Setup the chart data of all lines
- let data = LineChartData()
-
- data.append(lineBG) // Dataset 0
- data.append(linePrediction) // Dataset 1
- data.append(lineBasal) // Dataset 2
- data.append(lineBolus) // Dataset 3
- data.append(lineCarbs) // Dataset 4
- data.append(lineBasalScheduled) // Dataset 5
- data.append(lineOverride) // Dataset 6
- data.append(lineBGCheck) // Dataset 7
- data.append(lineSuspend) // Dataset 8
- data.append(lineResume) // Dataset 9
- data.append(lineSensor) // Dataset 10
- data.append(lineNote) // Dataset 11
-
- data.setValueFont(UIFont.systemFont(ofSize: 12))
-
- // Add marker popups for bolus and carbs
- let marker = PillMarker(color: .secondarySystemBackground, font: UIFont.boldSystemFont(ofSize: 14), textColor: .label)
- BGChart.marker = marker
-
- // Clear limit lines so they don't add multiples when changing the settings
- BGChart.rightAxis.removeAllLimitLines()
-
- //Add lower red line based on low alert value
- let ll = ChartLimitLine()
- ll.limit = Double(UserDefaultsRepository.lowLine.value)
- ll.lineColor = NSUIColor.systemRed.withAlphaComponent(0.5)
- BGChart.rightAxis.addLimitLine(ll)
-
- //Add upper yellow line based on low alert value
- let ul = ChartLimitLine()
- ul.limit = Double(UserDefaultsRepository.highLine.value)
- ul.lineColor = NSUIColor.systemYellow.withAlphaComponent(0.5)
- BGChart.rightAxis.addLimitLine(ul)
-
- // Add vertical lines as configured
- createVerticalLines()
- startGraphNowTimer()
-
- // Setup the main graph overall details
- BGChart.xAxis.valueFormatter = ChartXValueFormatter()
- BGChart.xAxis.granularity = 1800
- BGChart.xAxis.labelTextColor = NSUIColor.label
- BGChart.xAxis.labelPosition = XAxis.LabelPosition.bottom
- BGChart.xAxis.drawGridLinesEnabled = false
-
- BGChart.leftAxis.enabled = true
- BGChart.leftAxis.labelPosition = YAxis.LabelPosition.insideChart
- BGChart.leftAxis.axisMaximum = maxBasal
- BGChart.leftAxis.axisMinimum = 0
- BGChart.leftAxis.drawGridLinesEnabled = false
- BGChart.leftAxis.granularityEnabled = true
- BGChart.leftAxis.granularity = 0.5
-
- BGChart.rightAxis.labelTextColor = NSUIColor.label
- BGChart.rightAxis.labelPosition = YAxis.LabelPosition.insideChart
- BGChart.rightAxis.axisMinimum = 0.0
- BGChart.rightAxis.axisMaximum = Double(maxBG)
- BGChart.rightAxis.gridLineDashLengths = [5.0, 5.0]
- BGChart.rightAxis.drawGridLinesEnabled = false
- BGChart.rightAxis.valueFormatter = ChartYMMOLValueFormatter()
- BGChart.rightAxis.granularityEnabled = true
- BGChart.rightAxis.granularity = 50
-
- BGChart.maxHighlightDistance = 15.0
- BGChart.legend.enabled = false
- BGChart.scaleYEnabled = false
- BGChart.drawGridBackgroundEnabled = true
- BGChart.gridBackgroundColor = NSUIColor.secondarySystemBackground
-
- BGChart.highlightValue(nil, callDelegate: false)
-
- BGChart.data = data
- BGChart.setExtraOffsets(left: 5, top: 10, right: 5, bottom: 10)
-
- }
-
- func createVerticalLines() {
- BGChart.xAxis.removeAllLimitLines()
- BGChartFull.xAxis.removeAllLimitLines()
- createNowAndDIALines()
- createMidnightLines()
- }
-
- func createNowAndDIALines() {
- let ul = ChartLimitLine()
- ul.limit = Double(dateTimeUtils.getNowTimeIntervalUTC())
- ul.lineColor = NSUIColor.systemGray.withAlphaComponent(0.5)
- ul.lineWidth = 1
- BGChart.xAxis.addLimitLine(ul)
-
- if UserDefaultsRepository.show30MinLine.value {
- let ul2 = ChartLimitLine()
- ul2.limit = Double(dateTimeUtils.getNowTimeIntervalUTC().advanced(by: -30 * 60))
- ul2.lineColor = NSUIColor.systemBlue.withAlphaComponent(0.5)
- ul2.lineWidth = 1
- BGChart.xAxis.addLimitLine(ul2)
- }
-
- if UserDefaultsRepository.showDIALines.value {
- for i in 1..<7 {
- let ul = ChartLimitLine()
- ul.limit = Double(dateTimeUtils.getNowTimeIntervalUTC() - Double(i * 60 * 60))
- ul.lineColor = NSUIColor.systemGray.withAlphaComponent(0.3)
- let dash = 10.0 - Double(i)
- let space = 5.0 + Double(i)
- ul.lineDashLengths = [CGFloat(dash), CGFloat(space)]
- ul.lineWidth = 1
- BGChart.xAxis.addLimitLine(ul)
- }
- }
- if UserDefaultsRepository.show90MinLine.value {
- let ul3 = ChartLimitLine()
- ul3.limit = Double(dateTimeUtils.getNowTimeIntervalUTC().advanced(by: -90 * 60))
- ul3.lineColor = NSUIColor.systemOrange.withAlphaComponent(0.5)
- ul3.lineWidth = 1
- BGChart.xAxis.addLimitLine(ul3)
- }
- }
-
- func createMidnightLines() {
- // Draw a line at midnight: useful when showing multiple days of data
- if UserDefaultsRepository.showMidnightLines.value {
- var midnightTimeInterval = dateTimeUtils.getTimeIntervalMidnightToday()
- let graphHours = 24 * UserDefaultsRepository.downloadDays.value
- let graphStart = dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours)
- while midnightTimeInterval > graphStart {
- // Large chart
- let ul = ChartLimitLine()
- ul.limit = Double(midnightTimeInterval)
- ul.lineColor = NSUIColor.systemTeal.withAlphaComponent(0.5)
- ul.lineDashLengths = [CGFloat(2), CGFloat(5)]
- ul.lineWidth = 1
- BGChart.xAxis.addLimitLine(ul)
- // Small chart
- let sl = ChartLimitLine()
- sl.limit = Double(midnightTimeInterval)
- sl.lineColor = NSUIColor.systemTeal
- sl.lineDashLengths = [CGFloat(2), CGFloat(2)]
- sl.lineWidth = 1
- BGChartFull.xAxis.addLimitLine(sl)
-
- midnightTimeInterval = midnightTimeInterval.advanced(by: -24*60*60)
- }
- }
- }
-
- func updateBGGraphSettings() {
- let dataIndex = 0
- let dataIndexPrediction = 1
- let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
- let linePrediction = BGChart.lineData!.dataSets[dataIndexPrediction] as! LineChartDataSet
- if UserDefaultsRepository.showLines.value {
- lineBG.lineWidth = 2
- linePrediction.lineWidth = 2
- } else {
- lineBG.lineWidth = 0
- linePrediction.lineWidth = 0
- }
- if UserDefaultsRepository.showDots.value {
- lineBG.drawCirclesEnabled = true
- linePrediction.drawCirclesEnabled = true
- } else {
- lineBG.drawCirclesEnabled = false
- linePrediction.drawCirclesEnabled = false
- }
-
- BGChart.rightAxis.axisMinimum = 0
-
- // Clear limit lines so they don't add multiples when changing the settings
- BGChart.rightAxis.removeAllLimitLines()
-
- //Add lower red line based on low alert value
- let ll = ChartLimitLine()
- ll.limit = Double(UserDefaultsRepository.lowLine.value)
- ll.lineColor = NSUIColor.systemRed.withAlphaComponent(0.5)
- BGChart.rightAxis.addLimitLine(ll)
-
- //Add upper yellow line based on low alert value
- let ul = ChartLimitLine()
- ul.limit = Double(UserDefaultsRepository.highLine.value)
- ul.lineColor = NSUIColor.systemYellow.withAlphaComponent(0.5)
- BGChart.rightAxis.addLimitLine(ul)
-
- // Re-create vertical markers in case their settings changed
- createVerticalLines()
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
-
- }
-
- func updateBGGraph() {
- if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "##### Start BG Graph #####") }
- let dataIndex = 0
- let entries = bgData
- if entries.count < 1 { return }
- var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
- var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
- mainChart.clear()
- smallChart.clear()
- var maxBGOffset: Float = 50
-
- var colors = [NSUIColor]()
- for i in 0..<entries.count{
- if Float(entries[i].sgv) > topBG - maxBGOffset {
- topBG = Float(entries[i].sgv) + maxBGOffset
- }
- let value = ChartDataEntry(x: Double(entries[i].date), y: Double(entries[i].sgv), data: formatPillText(line1: bgUnits.toDisplayUnits(String(entries[i].sgv)), time: entries[i].date))
- if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "BG: " + value.description) }
- mainChart.addEntry(value)
- smallChart.addEntry(value)
-
- if Double(entries[i].sgv) >= Double(UserDefaultsRepository.highLine.value) {
- colors.append(NSUIColor.systemYellow)
- } else if Double(entries[i].sgv) <= Double(UserDefaultsRepository.lowLine.value) {
- colors.append(NSUIColor.systemRed)
- } else {
- colors.append(NSUIColor.systemGreen)
- }
- }
-
- if (predictionData.count < 1) {
- let value = ChartDataEntry(x: Double(entries[entries.count-1].date+1200), y: 0)
- mainChart.addEntry(value)
- colors.append(NSUIColor.systemGray.withAlphaComponent(0.0))
- }
-
- if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "Total Graph BGs: " + mainChart.entries.count.description) }
-
-
- // Set Colors
- let lineBG = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
- let lineBGSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
- lineBG.colors.removeAll()
- lineBG.circleColors.removeAll()
- lineBGSmall.colors.removeAll()
- lineBGSmall.circleColors.removeAll()
- if colors.count > 0 {
- for i in 0..<colors.count{
- mainChart.addColor(colors[i])
- mainChart.circleColors.append(colors[i])
- smallChart.addColor(colors[i])
- smallChart.circleColors.append(colors[i])
- }
- }
-
- if UserDefaultsRepository.debugLog.value { writeDebugLog(value: "Total Colors: " + mainChart.colors.count.description) }
-
- BGChart.rightAxis.axisMaximum = Double(topBG)
- BGChart.setVisibleXRangeMinimum(600)
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- BGChartFull.rightAxis.axisMaximum = Double(topBG)
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
-
- if firstGraphLoad {
- var scaleX = CGFloat(UserDefaultsRepository.chartScaleX.value)
- print("Scale: \(scaleX)")
- if( scaleX > CGFloat(ScaleXMax) ) {
- scaleX = CGFloat(ScaleXMax)
- UserDefaultsRepository.chartScaleX.value = ScaleXMax
- }
- BGChart.zoom(scaleX: scaleX, scaleY: 1, x: 1, y: 1)
- firstGraphLoad = false
- }
-
- // Move to current reading everytime new readings load
- BGChart.moveViewToAnimated(xValue: dateTimeUtils.getNowTimeIntervalUTC() - (BGChart.visibleXRange * 0.7), yValue: 0.0, axis: .right, duration: 1, easingOption: .easeInBack)
- }
-
- func updatePredictionGraph() {
- let dataIndex = 1
- var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
- var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
- mainChart.clear()
- smallChart.clear()
- if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: print prediction") }
- var colors = [NSUIColor]()
- let maxBGOffset: Float = 20
- for i in 0..<predictionData.count {
- var predictionVal = Double(predictionData[i].sgv)
- if Float(predictionVal) > topBG - maxBGOffset {
- topBG = Float(predictionVal) + maxBGOffset
- }
- if i == 0 {
- if UserDefaultsRepository.showDots.value {
- colors.append(NSUIColor.systemPurple.withAlphaComponent(0.0))
- } else {
- colors.append(NSUIColor.systemPurple.withAlphaComponent(1.0))
- }
-
- } else if predictionVal > 400 {
- predictionVal = 400
- colors.append(NSUIColor.systemYellow)
- } else if predictionVal < 0 {
- predictionVal = 0
- colors.append(NSUIColor.systemRed)
- } else {
- colors.append(NSUIColor.systemPurple)
- }
- let value = ChartDataEntry(x: predictionData[i].date, y: predictionVal, data: formatPillText(line1: bgUnits.toDisplayUnits(String(predictionData[i].sgv)), time: predictionData[i].date))
- mainChart.addEntry(value)
- smallChart.addEntry(value)
- }
-
- smallChart.circleColors.removeAll()
- smallChart.colors.removeAll()
- mainChart.colors.removeAll()
- mainChart.circleColors.removeAll()
- if colors.count > 0 {
- if UserDefaultsRepository.debugLog.value { self.writeDebugLog(value: "Graph: prediction colors") }
- for i in 0..<colors.count{
- mainChart.addColor(colors[i])
- mainChart.circleColors.append(colors[i])
- smallChart.addColor(colors[i])
- smallChart.circleColors.append(colors[i])
- }
- }
- BGChart.rightAxis.axisMaximum = Double(topBG)
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
-
- func updateBasalGraph() {
- var dataIndex = 2
- BGChart.lineData?.dataSets[dataIndex].clear()
- BGChartFull.lineData?.dataSets[dataIndex].clear()
- var maxBasal = UserDefaultsRepository.minBasalScale.value
- var maxBasalSmall: Double = 0.0
- for i in 0..<basalData.count{
- let value = ChartDataEntry(x: Double(basalData[i].date), y: Double(basalData[i].basalRate), data: formatPillText(line1: String(basalData[i].basalRate), time: basalData[i].date))
- BGChart.data?.dataSets[dataIndex].addEntry(value)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(value)
- }
- if basalData[i].basalRate > maxBasal {
- maxBasal = basalData[i].basalRate
- }
- if basalData[i].basalRate > maxBasalSmall {
- maxBasalSmall = basalData[i].basalRate
- }
- }
-
- BGChart.leftAxis.axisMaximum = maxBasal
- BGChartFull.leftAxis.axisMaximum = maxBasalSmall
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
-
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func updateBasalScheduledGraph() {
- var dataIndex = 5
- BGChart.lineData?.dataSets[dataIndex].clear()
- BGChartFull.lineData?.dataSets[dataIndex].clear()
- for i in 0..<basalScheduleData.count{
- let value = ChartDataEntry(x: Double(basalScheduleData[i].date), y: Double(basalScheduleData[i].basalRate))
- BGChart.data?.dataSets[dataIndex].addEntry(value)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(value)
- }
- }
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func updateBolusGraph() {
- var dataIndex = 3
- var yTop: Double = 370
- var yBottom: Double = 345
- var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
- var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
- mainChart.clear()
- smallChart.clear()
-
- var colors = [NSUIColor]()
- for i in 0..<bolusData.count{
- let formatter = NumberFormatter()
- formatter.minimumFractionDigits = 0
- formatter.maximumFractionDigits = 2
- formatter.minimumIntegerDigits = 0
-
- // Check overlapping carbs to shift left if needed
- let bolusShift = findNextBolusTime(timeWithin: 240, needle: bolusData[i].date, haystack: bolusData, startingIndex: i)
- var dateTimeStamp = bolusData[i].date
-
- // Alpha colors for DIA
- let nowTime = dateTimeUtils.getNowTimeIntervalUTC()
- let diffTimeHours = (nowTime - dateTimeStamp) / 60 / 60
- if diffTimeHours <= 1 {
- colors.append(NSUIColor.systemBlue.withAlphaComponent(1.0))
- } else if diffTimeHours > 6 {
- colors.append(NSUIColor.systemBlue.withAlphaComponent(0.25))
- } else {
- let thisAlpha = 1.0 - (0.15 * diffTimeHours)
- colors.append(NSUIColor.systemBlue.withAlphaComponent(CGFloat(thisAlpha)))
- }
-
- if bolusShift {
- // Move it half the distance between BG readings
- dateTimeStamp = dateTimeStamp - 150
- }
-
- // skip if outside of visible area
- let graphHours = 24 * UserDefaultsRepository.downloadDays.value
- if dateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
-
- let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(bolusData[i].sgv), data: formatter.string(from: NSNumber(value: bolusData[i].value)))
- mainChart.addEntry(dot)
- if UserDefaultsRepository.smallGraphTreatments.value {
- smallChart.addEntry(dot)
- }
- }
-
- // Set Colors
- let lineBolus = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
- let lineBolusSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
- lineBolus.colors.removeAll()
- lineBolus.circleColors.removeAll()
- lineBolusSmall.colors.removeAll()
- lineBolusSmall.circleColors.removeAll()
-
- if colors.count > 0 {
- for i in 0..<colors.count{
- mainChart.addColor(colors[i])
- mainChart.circleColors.append(colors[i])
- smallChart.addColor(colors[i])
- smallChart.circleColors.append(colors[i])
- }
- }
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func updateCarbGraph() {
- var dataIndex = 4
- var mainChart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
- var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
- mainChart.clear()
- smallChart.clear()
-
- var colors = [NSUIColor]()
- for i in 0..<carbData.count{
- let formatter = NumberFormatter()
- formatter.minimumFractionDigits = 0
- formatter.maximumFractionDigits = 2
- formatter.minimumIntegerDigits = 1
-
- var valueString: String = formatter.string(from: NSNumber(value: carbData[i].value))!
-
- var hours = 3
- if carbData[i].absorptionTime > 0 && UserDefaultsRepository.showAbsorption.value {
- hours = carbData[i].absorptionTime / 60
- valueString += " " + String(hours) + "h"
- }
-
- // Check overlapping carbs to shift left if needed
- let carbShift = findNextCarbTime(timeWithin: 250, needle: carbData[i].date, haystack: carbData, startingIndex: i)
- var dateTimeStamp = carbData[i].date
-
- // Alpha colors for DIA
- let nowTime = dateTimeUtils.getNowTimeIntervalUTC()
- let diffTimeHours = (nowTime - dateTimeStamp) / 60 / 60
- if diffTimeHours <= 0.5 {
- colors.append(NSUIColor.systemOrange.withAlphaComponent(1.0))
- } else if diffTimeHours > Double(hours) {
- colors.append(NSUIColor.systemOrange.withAlphaComponent(0.25))
- } else {
- let thisAlpha = 1.0 - ((0.75 / Double(hours)) * diffTimeHours)
- colors.append(NSUIColor.systemOrange.withAlphaComponent(CGFloat(thisAlpha)))
- }
-
- // skip if outside of visible area
- let graphHours = 24 * UserDefaultsRepository.downloadDays.value
- if dateTimeStamp < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
-
- if carbShift {
- dateTimeStamp = dateTimeStamp - 250
- }
-
-
- let dot = ChartDataEntry(x: Double(dateTimeStamp), y: Double(carbData[i].sgv), data: valueString)
- BGChart.data?.dataSets[dataIndex].addEntry(dot)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(dot)
- }
-
-
- }
-
- // Set Colors
- let lineCarbs = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
- let lineCarbsSmall = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
- lineCarbs.colors.removeAll()
- lineCarbs.circleColors.removeAll()
- lineCarbsSmall.colors.removeAll()
- lineCarbsSmall.circleColors.removeAll()
-
- if colors.count > 0 {
- for i in 0..<colors.count{
- mainChart.addColor(colors[i])
- mainChart.circleColors.append(colors[i])
- smallChart.addColor(colors[i])
- smallChart.circleColors.append(colors[i])
- }
- }
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func updateBGCheckGraph() {
- var dataIndex = 7
- BGChart.lineData?.dataSets[dataIndex].clear()
- BGChartFull.lineData?.dataSets[dataIndex].clear()
-
- for i in 0..<bgCheckData.count{
- let formatter = NumberFormatter()
- formatter.minimumFractionDigits = 0
- formatter.maximumFractionDigits = 2
- formatter.minimumIntegerDigits = 1
-
- // skip if outside of visible area
- let graphHours = 24 * UserDefaultsRepository.downloadDays.value
- if bgCheckData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
-
- let value = ChartDataEntry(x: Double(bgCheckData[i].date), y: Double(bgCheckData[i].sgv), data: formatPillText(line1: bgUnits.toDisplayUnits(String(bgCheckData[i].sgv)), time: bgCheckData[i].date))
- BGChart.data?.dataSets[dataIndex].addEntry(value)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(value)
- }
- }
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func updateSuspendGraph() {
- var dataIndex = 8
- BGChart.lineData?.dataSets[dataIndex].clear()
- BGChartFull.lineData?.dataSets[dataIndex].clear()
- let thisData = suspendGraphData
- for i in 0..<thisData.count{
- // skip if outside of visible area
- let graphHours = 24 * UserDefaultsRepository.downloadDays.value
- if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
-
- let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Suspend Pump", time: thisData[i].date))
- BGChart.data?.dataSets[dataIndex].addEntry(value)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(value)
- }
- }
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func updateResumeGraph() {
- var dataIndex = 9
- BGChart.lineData?.dataSets[dataIndex].clear()
- BGChartFull.lineData?.dataSets[dataIndex].clear()
- let thisData = resumeGraphData
- for i in 0..<thisData.count{
- // skip if outside of visible area
- let graphHours = 24 * UserDefaultsRepository.downloadDays.value
- if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
-
- let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Resume Pump", time: thisData[i].date))
- BGChart.data?.dataSets[dataIndex].addEntry(value)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(value)
- }
- }
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func updateSensorStart() {
- var dataIndex = 10
- BGChart.lineData?.dataSets[dataIndex].clear()
- BGChartFull.lineData?.dataSets[dataIndex].clear()
- let thisData = sensorStartGraphData
- for i in 0..<thisData.count{
- // skip if outside of visible area
- let graphHours = 24 * UserDefaultsRepository.downloadDays.value
- if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
-
- let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: "Start Sensor", time: thisData[i].date))
- BGChart.data?.dataSets[dataIndex].addEntry(value)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(value)
- }
- }
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func updateNotes() {
- var dataIndex = 11
- BGChart.lineData?.dataSets[dataIndex].clear()
- BGChartFull.lineData?.dataSets[dataIndex].clear()
- let thisData = noteGraphData
- for i in 0..<thisData.count{
-
- // skip if outside of visible area
- let graphHours = 24 * UserDefaultsRepository.downloadDays.value
- if thisData[i].date < dateTimeUtils.getTimeIntervalNHoursAgo(N: graphHours) { continue }
-
- let value = ChartDataEntry(x: Double(thisData[i].date), y: Double(thisData[i].sgv), data: formatPillText(line1: thisData[i].note, time: thisData[i].date))
- BGChart.data?.dataSets[dataIndex].addEntry(value)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(value)
- }
- }
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func createSmallBGGraph(){
- let entries = bgData
- var bgChartEntry = [ChartDataEntry]()
- var colors = [NSUIColor]()
- var maxBG: Float = UserDefaultsRepository.minBGScale.value
-
- let lineBG = LineChartDataSet(entries:bgChartEntry, label: "")
-
- lineBG.drawCirclesEnabled = false
- //line2.setDrawHighlightIndicators(false)
- lineBG.highlightEnabled = true
- lineBG.drawHorizontalHighlightIndicatorEnabled = false
- lineBG.drawVerticalHighlightIndicatorEnabled = false
- lineBG.highlightColor = NSUIColor.label
- lineBG.drawValuesEnabled = false
- lineBG.lineWidth = 1.5
- lineBG.axisDependency = YAxis.AxisDependency.right
-
- // Setup Prediction line details
- var predictionChartEntry = [ChartDataEntry]()
- let linePrediction = LineChartDataSet(entries:predictionChartEntry, label: "")
- linePrediction.drawCirclesEnabled = false
- //line2.setDrawHighlightIndicators(false)
- linePrediction.setColor(NSUIColor.systemPurple)
- linePrediction.highlightEnabled = true
- linePrediction.drawHorizontalHighlightIndicatorEnabled = false
- linePrediction.drawVerticalHighlightIndicatorEnabled = false
- linePrediction.highlightColor = NSUIColor.label
- linePrediction.drawValuesEnabled = false
- linePrediction.lineWidth = 1.5
- linePrediction.axisDependency = YAxis.AxisDependency.right
-
- // create Basal graph data
- var chartEntry = [ChartDataEntry]()
- var maxBasal = UserDefaultsRepository.minBasalScale.value
- let lineBasal = LineChartDataSet(entries:chartEntry, label: "")
- lineBasal.setDrawHighlightIndicators(false)
- lineBasal.setColor(NSUIColor.systemBlue, alpha: 0.5)
- lineBasal.lineWidth = 0
- lineBasal.drawFilledEnabled = true
- lineBasal.fillColor = NSUIColor.systemBlue
- lineBasal.fillAlpha = 0.35
- lineBasal.drawCirclesEnabled = false
- lineBasal.axisDependency = YAxis.AxisDependency.left
- lineBasal.highlightEnabled = false
- lineBasal.drawValuesEnabled = false
- lineBasal.fillFormatter = basalFillFormatter()
-
- // Boluses
- var chartEntryBolus = [ChartDataEntry]()
- let lineBolus = LineChartDataSet(entries:chartEntryBolus, label: "")
- lineBolus.circleRadius = 2
- lineBolus.circleColors = [NSUIColor.systemBlue.withAlphaComponent(0.75)]
- lineBolus.drawCircleHoleEnabled = false
- lineBolus.setDrawHighlightIndicators(false)
- lineBolus.setColor(NSUIColor.systemBlue, alpha: 1.0)
- lineBolus.lineWidth = 0
- lineBolus.axisDependency = YAxis.AxisDependency.right
- lineBolus.valueFormatter = ChartYDataValueFormatter()
- lineBolus.valueTextColor = NSUIColor.label
- lineBolus.fillColor = NSUIColor.systemBlue
- lineBolus.fillAlpha = 0.6
- lineBolus.drawCirclesEnabled = true
- lineBolus.drawFilledEnabled = false
- lineBolus.drawValuesEnabled = false
- lineBolus.highlightEnabled = false
-
-
- // Carbs
- var chartEntryCarbs = [ChartDataEntry]()
- let lineCarbs = LineChartDataSet(entries:chartEntryCarbs, label: "")
- lineCarbs.circleRadius = 2
- lineCarbs.circleColors = [NSUIColor.systemOrange.withAlphaComponent(0.75)]
- lineCarbs.drawCircleHoleEnabled = false
- lineCarbs.setDrawHighlightIndicators(false)
- lineCarbs.setColor(NSUIColor.systemBlue, alpha: 1.0)
- lineCarbs.lineWidth = 0
- lineCarbs.axisDependency = YAxis.AxisDependency.right
- lineCarbs.valueFormatter = ChartYDataValueFormatter()
- lineCarbs.valueTextColor = NSUIColor.label
- lineCarbs.fillColor = NSUIColor.systemOrange
- lineCarbs.fillAlpha = 0.6
- lineCarbs.drawCirclesEnabled = true
- lineCarbs.drawFilledEnabled = false
- lineCarbs.drawValuesEnabled = false
- lineCarbs.highlightEnabled = false
-
-
-
- // create Scheduled Basal graph data
- var chartBasalScheduledEntry = [ChartDataEntry]()
- let lineBasalScheduled = LineChartDataSet(entries:chartBasalScheduledEntry, label: "")
- lineBasalScheduled.setDrawHighlightIndicators(false)
- lineBasalScheduled.setColor(NSUIColor.systemBlue, alpha: 0.8)
- lineBasalScheduled.lineWidth = 0.5
- lineBasalScheduled.drawFilledEnabled = false
- lineBasalScheduled.drawCirclesEnabled = false
- lineBasalScheduled.axisDependency = YAxis.AxisDependency.left
- lineBasalScheduled.highlightEnabled = false
- lineBasalScheduled.drawValuesEnabled = false
- lineBasalScheduled.lineDashLengths = [2, 1]
-
- // create Override graph data
- var chartOverrideEntry = [ChartDataEntry]()
- let lineOverride = LineChartDataSet(entries:chartOverrideEntry, label: "")
- lineOverride.setDrawHighlightIndicators(false)
- lineOverride.lineWidth = 0
- lineOverride.drawFilledEnabled = true
- lineOverride.fillFormatter = OverrideFillFormatter()
- lineOverride.fillColor = NSUIColor.systemGreen
- lineOverride.fillAlpha = 0.6
- lineOverride.drawCirclesEnabled = false
- lineOverride.axisDependency = YAxis.AxisDependency.right
- lineOverride.highlightEnabled = true
- lineOverride.drawValuesEnabled = false
-
- // BG Check
- var chartEntryBGCheck = [ChartDataEntry]()
- let lineBGCheck = LineChartDataSet(entries:chartEntryBGCheck, label: "")
- lineBGCheck.circleRadius = 2
- lineBGCheck.circleColors = [NSUIColor.systemRed.withAlphaComponent(0.75)]
- lineBGCheck.drawCircleHoleEnabled = false
- lineBGCheck.setDrawHighlightIndicators(false)
- lineBGCheck.setColor(NSUIColor.systemRed, alpha: 1.0)
- lineBGCheck.drawCirclesEnabled = true
- lineBGCheck.lineWidth = 0
- lineBGCheck.highlightEnabled = false
- lineBGCheck.axisDependency = YAxis.AxisDependency.right
- lineBGCheck.valueFormatter = ChartYDataValueFormatter()
- lineBGCheck.drawValuesEnabled = false
-
- // Suspend Pump
- var chartEntrySuspend = [ChartDataEntry]()
- let lineSuspend = LineChartDataSet(entries:chartEntrySuspend, label: "")
- lineSuspend.circleRadius = 2
- lineSuspend.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
- lineSuspend.drawCircleHoleEnabled = false
- lineSuspend.setDrawHighlightIndicators(false)
- lineSuspend.setColor(NSUIColor.systemGray2, alpha: 1.0)
- lineSuspend.drawCirclesEnabled = true
- lineSuspend.lineWidth = 0
- lineSuspend.highlightEnabled = false
- lineSuspend.axisDependency = YAxis.AxisDependency.right
- lineSuspend.valueFormatter = ChartYDataValueFormatter()
- lineSuspend.drawValuesEnabled = false
-
- // Resume Pump
- var chartEntryResume = [ChartDataEntry]()
- let lineResume = LineChartDataSet(entries:chartEntryResume, label: "")
- lineResume.circleRadius = 2
- lineResume.circleColors = [NSUIColor.systemTeal.withAlphaComponent(0.75)]
- lineResume.drawCircleHoleEnabled = false
- lineResume.setDrawHighlightIndicators(false)
- lineResume.setColor(NSUIColor.systemGray4, alpha: 1.0)
- lineResume.drawCirclesEnabled = true
- lineResume.lineWidth = 0
- lineResume.highlightEnabled = false
- lineResume.axisDependency = YAxis.AxisDependency.right
- lineResume.valueFormatter = ChartYDataValueFormatter()
- lineResume.drawValuesEnabled = false
-
- // Sensor Start
- var chartEntrySensor = [ChartDataEntry]()
- let lineSensor = LineChartDataSet(entries:chartEntrySensor, label: "")
- lineSensor.circleRadius = 2
- lineSensor.circleColors = [NSUIColor.systemIndigo.withAlphaComponent(0.75)]
- lineSensor.drawCircleHoleEnabled = false
- lineSensor.setDrawHighlightIndicators(false)
- lineSensor.setColor(NSUIColor.systemGray3, alpha: 1.0)
- lineSensor.drawCirclesEnabled = true
- lineSensor.lineWidth = 0
- lineSensor.highlightEnabled = false
- lineSensor.axisDependency = YAxis.AxisDependency.right
- lineSensor.valueFormatter = ChartYDataValueFormatter()
- lineSensor.drawValuesEnabled = false
-
- // Notes
- var chartEntryNote = [ChartDataEntry]()
- let lineNote = LineChartDataSet(entries:chartEntryNote, label: "")
- lineNote.circleRadius = 2
- lineNote.circleColors = [NSUIColor.systemGray.withAlphaComponent(0.75)]
- lineNote.drawCircleHoleEnabled = false
- lineNote.setDrawHighlightIndicators(false)
- lineNote.setColor(NSUIColor.systemGray3, alpha: 1.0)
- lineNote.drawCirclesEnabled = true
- lineNote.lineWidth = 0
- lineNote.highlightEnabled = false
- lineNote.axisDependency = YAxis.AxisDependency.right
- lineNote.valueFormatter = ChartYDataValueFormatter()
- lineNote.drawValuesEnabled = false
-
- // Setup the chart data of all lines
- let data = LineChartData()
- data.append(lineBG) // Dataset 0
- data.append(linePrediction) // Dataset 1
- data.append(lineBasal) // Dataset 2
- data.append(lineBolus) // Dataset 3
- data.append(lineCarbs) // Dataset 4
- data.append(lineBasalScheduled) // Dataset 5
- data.append(lineOverride) // Dataset 6
- data.append(lineBGCheck) // Dataset 7
- data.append(lineSuspend) // Dataset 8
- data.append(lineResume) // Dataset 9
- data.append(lineSensor) // Dataset 10
- data.append(lineNote) // Dataset 11
-
- BGChartFull.highlightPerDragEnabled = true
- BGChartFull.leftAxis.enabled = false
- BGChartFull.leftAxis.axisMaximum = maxBasal
- BGChartFull.leftAxis.axisMinimum = 0
-
- BGChartFull.rightAxis.enabled = false
- BGChartFull.rightAxis.axisMinimum = 0.0
- BGChartFull.rightAxis.axisMaximum = Double(maxBG)
-
- BGChartFull.xAxis.drawLabelsEnabled = false
- BGChartFull.xAxis.drawGridLinesEnabled = false
- BGChartFull.xAxis.drawAxisLineEnabled = false
- BGChartFull.legend.enabled = false
- BGChartFull.scaleYEnabled = false
- BGChartFull.scaleXEnabled = false
- BGChartFull.drawGridBackgroundEnabled = false
- BGChartFull.data = data
-
-
- }
-
- func updateOverrideGraph() {
- var dataIndex = 6
- var yTop: Double = Double(topBG - 5)
- var yBottom: Double = Double(topBG - 25)
- var chart = BGChart.lineData!.dataSets[dataIndex] as! LineChartDataSet
- var smallChart = BGChartFull.lineData!.dataSets[dataIndex] as! LineChartDataSet
- chart.clear()
- smallChart.clear()
- let thisData = overrideGraphData
-
- var colors = [NSUIColor]()
- for i in 0..<thisData.count{
- let thisItem = thisData[i]
- let multiplier = thisItem.insulNeedsScaleFactor as! Double * 100.0
- var labelText = thisItem.reason + "\r\n"
- labelText += String(Int(thisItem.insulNeedsScaleFactor * 100)) + "% "
- if thisItem.correctionRange.count == 2 {
- labelText += String(thisItem.correctionRange[0]) + "-" + String(thisItem.correctionRange[1])
- }
- if thisItem.enteredBy.count > 0 {
- labelText += "\r\nEntered By: " + thisItem.enteredBy
- }
-
-
- // Start Dot
- // Shift dots 30 seconds to create an empty 0 space between consecutive temps
- let preStartDot = ChartDataEntry(x: Double(thisItem.date), y: yBottom, data: labelText)
- BGChart.data?.dataSets[dataIndex].addEntry(preStartDot)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(preStartDot)
- }
-
- let startDot = ChartDataEntry(x: Double(thisItem.date + 1), y: yTop, data: labelText)
- BGChart.data?.dataSets[dataIndex].addEntry(startDot)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(startDot)
- }
- // End Dot
- let endDot = ChartDataEntry(x: Double(thisItem.endDate - 2), y: yTop, data: labelText)
- BGChart.data?.dataSets[dataIndex].addEntry(endDot)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(endDot)
- }
-
- // Post end dot
- let postEndDot = ChartDataEntry(x: Double(thisItem.endDate - 1), y: yBottom, data: labelText)
- BGChart.data?.dataSets[dataIndex].addEntry(postEndDot)
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].addEntry(postEndDot)
- }
- }
-
- BGChart.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChart.data?.notifyDataChanged()
- BGChart.notifyDataSetChanged()
- if UserDefaultsRepository.smallGraphTreatments.value {
- BGChartFull.data?.dataSets[dataIndex].notifyDataSetChanged()
- BGChartFull.data?.notifyDataChanged()
- BGChartFull.notifyDataSetChanged()
- }
- }
-
- func formatPillText(line1: String, time: TimeInterval) -> String {
- let dateFormatter = DateFormatter()
- //let timezoneOffset = TimeZone.current.secondsFromGMT()
- //let epochTimezoneOffset = value + Double(timezoneOffset)
- if dateTimeUtils.is24Hour() {
- dateFormatter.setLocalizedDateFormatFromTemplate("HH:mm")
- } else {
- dateFormatter.setLocalizedDateFormatFromTemplate("hh:mm")
- }
-
- //let date = Date(timeIntervalSince1970: epochTimezoneOffset)
- let date = Date(timeIntervalSince1970: time)
- let formattedDate = dateFormatter.string(from: date)
- return line1 + "\r\n" + formattedDate
- }
-
- }
|