temp commit calculation yaxis, will be using another approach in next commit

This commit is contained in:
Johan Degraeve 2019-10-27 14:12:59 +01:00
parent d1c28203a0
commit c5f9ad6165
5 changed files with 82 additions and 69 deletions

View File

@ -10,4 +10,38 @@ enum ConstantsGlucoseChart {
/// H is hour 24 hour format, "h a" is hour 12 hour format with a either am or pm
static let defaultTimeAxisLabelFormat = "H"
/// if unit is mgdl, we wil have a horizointal line each ... mgdl
static let chartTableIncrementForMgDL = 50.0
/// if unit is mmol, we wil have a horizointal line each ... mmol
static let chartTableIncrementForMmol = 2.0
/// usually 40.0 mgdl is the lowest value that cgm's give, putting it to 38 guarantees the points will always be visible
///
/// only in mgdl because the label will not be shown, hence no bizar values to be shown when going to mgdl
static let absoluteMinimumChartValueInMgdl = 38.0
/// the minimum value that a cgm can show, it should be greater than absoluteMinimumChartValueInMgdl
static let minimumCGMGlucoseValueInMgdl = 40.0
/// default initial max mgdl value in chart, in mgdl - xdrip will try to maximize the use of the available height. If there's no glucose reading higher than this value, then the maximum in the graph will be this value
///
/// it's default, because a user will be able to modify the value later in the settings - in the code, the value will be
static let defaultInitialMaxChartValueInMgdl = 200.0
/// default initial max mmol value in chart, in mmol - xdrip will try to maximize the use of the available height. If there's no glucose reading higher than this value, then the maximum in the graph will be this value
///
/// it's default, because a user will be able to modify the value later in the settings - in the code, the value will be
static let defaultInitialMaxChartValueInMmol = 11.0
/// default increase in maxium mgdl value in the chart, in mgdl - xdrip will try to maximize the use of the available height. If there's a glucose reading higher than the initial value, then the maximum will be increased with this value, until all readings fit in the chart
///
/// it's default, because a user will be able to modify the value later in the settings
static let defaultIncreaseMaxChartValueInMgdl = 100.0
/// default increase in maxium mmol value in the chart, in mmol - xdrip will try to maximize the use of the available height. If there's a glucose reading higher than the initial value, then the maximum will be increased with this value, until all readings fit in the chart
///
/// it's default, because a user will be able to modify the value later in the settings
static let defaultIncreaseMaxChartValueInMmol = 6.0
}

View File

@ -23,15 +23,6 @@ extension HKUnit {
return HKUnit.moleUnit(with: .milli, molarMass: HKUnitMolarMassBloodGlucose).unitDivided(by: HKUnit.liter())
}()
/// The smallest value expected to be visible on a chart
var chartableIncrement: Double {
if unitString == "mg/dL" {
return 1
} else {
return 1 / 25
}
}
// A formatting helper for determining the preferred decimal style for a given unit
var preferredFractionDigits: Int {
if self.unitString == "mg/dL" {

View File

@ -30,34 +30,6 @@ public final class StatusChartsManager {
}
}
/// need to remove this, add an observer for UserDefaults.standard.bloodGlucoseUnitIsMgDl, then call glucoseDisplayRange didset whenever the value is changed
public var glucoseUnit: HKUnit = UserDefaults.standard.bloodGlucoseUnitIsMgDl ? .milligramsPerDeciliter : .millimolesPerLiter {
didSet {
if glucoseUnit != oldValue {
// this will call the didSet of variable glucoseDisplayRange to be called (see next)
let oldRange = glucoseDisplayRange
glucoseDisplayRange = oldRange
}
}
}
/// this is the range of the chart, in currently chosen unit (mgdl or mmol)
///
/// Whenever glucoseDisplayRange is assigned a new value, glucoseChart is set to nil
public var glucoseDisplayRange: (min: HKQuantity, max: HKQuantity)? {
didSet {
if let range = glucoseDisplayRange {
glucoseDisplayRangePoints = [
ChartPoint(x: ChartAxisValue(scalar: 0), y: ChartAxisValueDouble(range.min.doubleValue(for: glucoseUnit))),
ChartPoint(x: ChartAxisValue(scalar: 0), y: ChartAxisValueDouble(range.max.doubleValue(for: glucoseUnit)))
]
} else {
glucoseDisplayRangePoints = []
}
}
}
/// chartpoint array with actually reading values
///
/// Whenever glucoseChartPoints is assigned a new value, glucoseChart is set to nil
@ -68,6 +40,7 @@ public final class StatusChartsManager {
if let lastDate = glucoseChartPoints.last?.x as? ChartAxisValueDate {
updateEndDate(lastDate.date)
}
}
}
@ -138,13 +111,6 @@ public final class StatusChartsManager {
private var glucoseChartCache: ChartPointsTouchHighlightLayerViewCache?
/// two chartPoints, one for minimum glucose value, one for maximum, value depends on chosen unit
private var glucoseDisplayRangePoints: [ChartPoint] = [] {
didSet {
glucoseChart = nil
}
}
/// dateformatter for chartpoints ???
private let dateFormatter: DateFormatter = {
let dateFormatter = DateFormatter()
@ -235,7 +201,7 @@ public final class StatusChartsManager {
}
}
public func glucoseChartWithFrame(_ frame: CGRect) -> Chart? {
public func glucoseChartWithFrame(_ frame: CGRect, chartTableIncrement: Double) -> Chart? {
if let chart = glucoseChart, chart.frame != frame {
@ -245,7 +211,7 @@ public final class StatusChartsManager {
}
if glucoseChart == nil {
glucoseChart = generateGlucoseChartWithFrame(frame)
glucoseChart = generateGlucoseChartWithFrame(frame, chartTableIncrement: chartTableIncrement)
}
return glucoseChart
@ -262,25 +228,52 @@ public final class StatusChartsManager {
// MARK: - private functions
private func generateGlucoseChartWithFrame(_ frame: CGRect) -> Chart? {
private func generateGlucoseChartWithFrame(_ frame: CGRect, chartTableIncrement: Double) -> Chart? {
guard let xAxisModel = xAxisModel, let xAxisValues = xAxisValues else {
return nil
guard let xAxisModel = xAxisModel, let xAxisValues = xAxisValues else {return nil}
// just to save typing
let unitIsMgDl = UserDefaults.standard.bloodGlucoseUnitIsMgDl
// create yAxisValues, start with 38 mgdl, this is to make sure we show a bit lower than the real lowest value which is isually 40 mgdl, make the label invisible
let firstYAxisValue: ChartAxisValue = ChartAxisValueDouble((ConstantsGlucoseChart.absoluteMinimumChartValueInMgdl).mgdlToMmol(mgdl: unitIsMgDl), labelSettings: chartLabelSettings)
// now create 40, still make the label invisible
let secondYAxisValue: ChartAxisValue = ChartAxisValueDouble((ConstantsGlucoseChart.minimumCGMGlucoseValueInMgdl).mgdlToMmol(mgdl: unitIsMgDl), labelSettings: chartLabelSettings)
secondYAxisValue.hidden = true
// create now the yAxisValues and add the first two
var yAxisValues = [firstYAxisValue, secondYAxisValue]
// determine the maximum value in the glucosechartPoints, in mgdl
// start with maximum value defined in constants
var maximumValueInGlucoseChartPointsInMgdl = ConstantsGlucoseChart.defaultInitialMaxChartValueInMgdl
// now iterate through glucosechartpoints to determine the maximum
for glucoseChartPoint in glucoseChartPoints {
maximumValueInGlucoseChartPointsInMgdl = max(maximumValueInGlucoseChartPointsInMgdl, glucoseChartPoint.y.scalar.mmolToMgdl(mgdl: unitIsMgDl))
}
let points = glucoseChartPoints + glucoseDisplayRangePoints
guard points.count > 1 else {
return nil
// now the maximum in the chart needs to be calculated
var maximumToShowInChartInUserUnit = unitIsMgDl ? ConstantsGlucoseChart.defaultInitialMaxChartValueInMgdl:ConstantsGlucoseChart.defaultInitialMaxChartValueInMmol
// increase the maximum as long as the valule is lower than maximumValueInGlucoseChartPointsInMgdl
while maximumToShowInChartInUserUnit < maximumValueInGlucoseChartPointsInMgdl.mgdlToMmol(mgdl: unitIsMgDl) {
maximumToShowInChartInUserUnit += unitIsMgDl ? ConstantsGlucoseChart.defaultIncreaseMaxChartValueInMgdl:ConstantsGlucoseChart.defaultIncreaseMaxChartValueInMmol
}
let yAxisValues = ChartAxisValuesStaticGenerator.generateYAxisValuesWithChartPoints(points, minSegmentCount: 2, maxSegmentCount: 4, multiple: glucoseUnit.chartableIncrement * 25, axisValueGenerator: {
ChartAxisValueDouble($0, labelSettings: self.chartLabelSettings)
// get increment to use on yAxis
let incrementToUse = unitIsMgDl ? ConstantsGlucoseChart.chartTableIncrementForMgDL : ConstantsGlucoseChart.chartTableIncrementForMmol
// add the remaining chartpoints
while yAxisValues.last!.scalar < maximumToShowInChartInUserUnit {
yAxisValues.append(ChartAxisValueDouble(yAxisValues.last!.scalar + incrementToUse, labelSettings: chartLabelSettings))
}
// the last label should not be visible
yAxisValues.last?.hidden = true
yAxisValues.map {
debuglogging("scalar = " + $0.scalar.description)
}
, addPaddingSegmentIfEdge: false
)
let yAxisModel = ChartAxisModel(axisValues: yAxisValues, lineColor: colors.axisLine, labelSpaceReservationMode: .fixed(labelsWidthY))

View File

@ -12,7 +12,7 @@ import SwiftCharts
extension ChartPoint {
static func pointsForGlucoseRangeSchedule(_ glucoseRangeSchedule: GlucoseRangeSchedule, xAxisValues: [ChartAxisValue]) -> [ChartPoint] {
static func pointsForGlucoseRangeSchedule(_ glucoseRangeSchedule: GlucoseRangeSchedule, xAxisValues: [ChartAxisValue], chartTableIncrement: Double) -> [ChartPoint] {
let targetRanges = glucoseRangeSchedule.between(
start: ChartAxisValueDate.dateFromScalar(xAxisValues.first!.scalar),
end: ChartAxisValueDate.dateFromScalar(xAxisValues.last!.scalar)
@ -37,7 +37,7 @@ extension ChartPoint {
endDate = ChartAxisValueDate(date: targetRanges[index + 1].startDate, formatter: dateFormatter)
}
let value = range.value.rangeWithMinimumIncremement(glucoseRangeSchedule.unit.chartableIncrement)
let value = range.value.rangeWithMinimumIncremement(chartTableIncrement)
let minValue = ChartAxisValueDouble(value.minValue)
let maxValue = ChartAxisValueDouble(value.maxValue)
@ -55,8 +55,8 @@ extension ChartPoint {
return maxPoints + minPoints.reversed()
}
static func pointsForGlucoseRangeScheduleOverride(_ override: GlucoseRangeSchedule.Override, unit: HKUnit, xAxisValues: [ChartAxisValue], extendEndDateToChart: Bool = false) -> [ChartPoint] {
let range = override.value.rangeWithMinimumIncremement(unit.chartableIncrement)
static func pointsForGlucoseRangeScheduleOverride(_ override: GlucoseRangeSchedule.Override, chartTableIncrement: Double, xAxisValues: [ChartAxisValue], extendEndDateToChart: Bool = false) -> [ChartPoint] {
let range = override.value.rangeWithMinimumIncremement(chartTableIncrement)
let startDate = Date()
let endDate = override.end ?? .distantFuture

View File

@ -158,11 +158,6 @@ final class RootViewController: UIViewController {
}()
)
statusChartsManager.glucoseDisplayRange = (
min: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 100),
max: HKQuantity(unit: .milligramsPerDeciliter, doubleValue: 175)
)
return statusChartsManager
}()
@ -193,7 +188,7 @@ final class RootViewController: UIViewController {
self.statusChartsManager.prerender()
self.chartOutlet.chartGenerator = { [weak self] (frame) in
return self?.statusChartsManager.glucoseChartWithFrame(frame)?.view
return self?.statusChartsManager.glucoseChartWithFrame(frame, chartTableIncrement: UserDefaults.standard.bloodGlucoseUnitIsMgDl ? ConstantsGlucoseChart.chartTableIncrementForMgDL : ConstantsGlucoseChart.chartTableIncrementForMmol)?.view
}