// // Graphs.swift // LoopFollow // // Created by Jon Fawcett on 6/16/20. // Copyright © 2020 Jon Fawcett. All rights reserved. // import Foundation import Charts import UIKit extension MainViewController { func createGraph(){ self.BGChart.clear() // Create the BG Graph Data let entries = bgData var bgChartEntry = [ChartDataEntry]() var colors = [NSUIColor]() var maxBG: Int = 250 if bgData.count > 0 { for i in 0.. maxBG - 40 { maxBG = entries[i].sgv + 40 } let value = ChartDataEntry(x: Double(entries[i].date), y: Double(entries[i].sgv)) bgChartEntry.append(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) } } } // Add Prediction Data if predictionData.count > 0 && bgData.count > 0 && UserDefaultsRepository.graphPrediction.value { var startingTime = bgChartEntry[bgChartEntry.count - 1].x + 300 var i = 0 // Add 1 hour of predictions while i < 12 { var predictionVal = Double(predictionData[i]) // Below can be turned on to prevent out of range on the graph if desired. // It currently just drops them out of view if predictionVal > 400 { // predictionVal = 400 } else if predictionVal < 0 { // predictionVal = 0 } let value = ChartDataEntry(x: startingTime + 5, y: predictionVal) bgChartEntry.append(value) colors.append(NSUIColor.systemPurple) startingTime += 300 i += 1 } } // Setup BG line details let lineBG = LineChartDataSet(entries:bgChartEntry, label: "") lineBG.circleRadius = 3 lineBG.circleColors = [NSUIColor.systemGreen] lineBG.drawCircleHoleEnabled = false lineBG.axisDependency = YAxis.AxisDependency.right lineBG.highlightEnabled = false 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) if colors.count > 0 { for i in 1.. 0 && UserDefaultsRepository.graphBasal.value { for i in 0.. maxBasal { maxBasal = basalData[i].basalRate } } } // Setup Basal line details 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.withAlphaComponent(0.8) lineBasal.drawCirclesEnabled = false lineBasal.axisDependency = YAxis.AxisDependency.left lineBasal.highlightEnabled = false lineBasal.drawValuesEnabled = false // Boluses var chartEntryBolus = [ChartDataEntry]() if bolusData.count > 0 && UserDefaultsRepository.graphBolus.value { for i in 0.. 0 && UserDefaultsRepository.graphCarbs.value { for i in 0.. 0 { createSmallBGGraph(bgChartEntry: bgChartEntry, colors: colors) } } func createSmallBGGraph(bgChartEntry: [ChartDataEntry], colors: [NSUIColor]){ //24 Hour Small Graph let line2 = LineChartDataSet(entries:bgChartEntry, label: "Number") line2.drawCirclesEnabled = false line2.setDrawHighlightIndicators(false) line2.lineWidth = 1 for i in 1..