Merge pull request #260 from nightscout/fix/include-treatments-in-chartheight

include treatments in chart height
This commit is contained in:
Milos Kozak 2021-01-25 13:30:51 +01:00 committed by GitHub
commit 7f924deb7f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 41 deletions

View file

@ -308,8 +308,6 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
// **** BG **** // **** BG ****
graphData.addBgReadings(fromTime, toTime, lowLine, highLine, null) graphData.addBgReadings(fromTime, toTime, lowLine, highLine, null)
// set manual x bounds to have nice steps
graphData.formatAxis(fromTime, toTime)
// add target line // add target line
graphData.addTargetLine(fromTime, toTime, profile, null) graphData.addTargetLine(fromTime, toTime, profile, null)
@ -363,6 +361,10 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() {
} }
} }
} }
// set manual x bounds to have nice steps
graphData.setNumVerticalLables()
graphData.formatAxis(fromTime, toTime)
} }
// finally enforce drawing of graphs in UI thread // finally enforce drawing of graphs in UI thread
graphData.performUpdate() graphData.performUpdate()

View file

@ -846,11 +846,14 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList
graphData.addBgReadings(fromTime, toTime, lowLine, highLine, apsResult?.predictions) graphData.addBgReadings(fromTime, toTime, lowLine, highLine, apsResult?.predictions)
else graphData.addBgReadings(fromTime, toTime, lowLine, highLine, null) else graphData.addBgReadings(fromTime, toTime, lowLine, highLine, null)
// set manual x bounds to have nice steps
graphData.formatAxis(fromTime, endTime)
// Treatments // Treatments
graphData.addTreatments(fromTime, endTime) graphData.addTreatments(fromTime, endTime)
// set manual x bounds to have nice steps
graphData.setNumVerticalLables()
graphData.formatAxis(fromTime, endTime)
if (menuChartSettings[0][OverviewMenus.CharType.ACT.ordinal]) if (menuChartSettings[0][OverviewMenus.CharType.ACT.ordinal])
graphData.addActivity(fromTime, endTime, false, 0.8) graphData.addActivity(fromTime, endTime, false, 0.8)

View file

@ -17,11 +17,11 @@ import info.nightscout.androidaps.data.Profile
import info.nightscout.androidaps.db.BgReading import info.nightscout.androidaps.db.BgReading
import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.ActivePluginProvider
import info.nightscout.androidaps.interfaces.LoopInterface import info.nightscout.androidaps.interfaces.LoopInterface
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.interfaces.TreatmentsInterface import info.nightscout.androidaps.interfaces.TreatmentsInterface
import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.AAPSLogger
import info.nightscout.androidaps.logging.LTag import info.nightscout.androidaps.logging.LTag
import info.nightscout.androidaps.plugins.aps.openAPSSMB.SMBDefaults import info.nightscout.androidaps.plugins.aps.openAPSSMB.SMBDefaults
import info.nightscout.androidaps.interfaces.ProfileFunction
import info.nightscout.androidaps.plugins.general.overview.graphExtensions.* import info.nightscout.androidaps.plugins.general.overview.graphExtensions.*
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult import info.nightscout.androidaps.plugins.iob.iobCobCalculator.AutosensResult
import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin import info.nightscout.androidaps.plugins.iob.iobCobCalculator.IobCobCalculatorPlugin
@ -80,16 +80,20 @@ class GraphData(
for (prediction in predictions) if (prediction.value >= 40) bgListArray.add(prediction) for (prediction in predictions) if (prediction.value >= 40) bgListArray.add(prediction)
} }
maxBgValue = Profile.fromMgdlToUnits(maxBgValue, units) maxBgValue = Profile.fromMgdlToUnits(maxBgValue, units)
maxBgValue = if (units == Constants.MGDL) Round.roundTo(maxBgValue, 40.0) + 80 else Round.roundTo(maxBgValue, 2.0) + 4 maxBgValue = addUpperChartMargin(maxBgValue)
if (highLine > maxBgValue) maxBgValue = highLine if (highLine > maxBgValue) maxBgValue = highLine
val numOfVerticalLines = if (units == Constants.MGDL) (maxBgValue / 40 + 1).toInt() else (maxBgValue / 2 + 1).toInt()
maxY = maxBgValue maxY = maxBgValue
minY = 0.0 minY = 0.0
// set manual y bounds to have nice steps
graph.gridLabelRenderer.numVerticalLabels = numOfVerticalLines
addSeries(PointsWithLabelGraphSeries(Array(bgListArray.size) { i -> bgListArray[i] })) addSeries(PointsWithLabelGraphSeries(Array(bgListArray.size) { i -> bgListArray[i] }))
} }
internal fun setNumVerticalLables() {
graph.gridLabelRenderer.numVerticalLabels = if (units == Constants.MGDL) (maxY / 40 + 1).toInt() else (maxY / 2 + 1).toInt()
}
private fun addUpperChartMargin(maxBgValue: Double) =
if (units == Constants.MGDL) Round.roundTo(maxBgValue, 40.0) + 80 else Round.roundTo(maxBgValue, 2.0) + 4
fun addInRangeArea(fromTime: Long, toTime: Long, lowLine: Double, highLine: Double) { fun addInRangeArea(fromTime: Long, toTime: Long, lowLine: Double, highLine: Double) {
val inRangeAreaSeries: AreaGraphSeries<DoubleDataPoint> val inRangeAreaSeries: AreaGraphSeries<DoubleDataPoint>
val inRangeAreaDataPoints = arrayOf( val inRangeAreaDataPoints = arrayOf(
@ -233,44 +237,45 @@ class GraphData(
fun addTreatments(fromTime: Long, endTime: Long) { fun addTreatments(fromTime: Long, endTime: Long) {
val filteredTreatments: MutableList<DataPointWithLabelInterface> = ArrayList() val filteredTreatments: MutableList<DataPointWithLabelInterface> = ArrayList()
val treatments = treatmentsPlugin.treatmentsFromHistory treatmentsPlugin.treatmentsFromHistory
for (tx in treatments.indices) { .filterTimeframe(fromTime, endTime)
val t = treatments[tx] .filter { !it.isSMB || it.isValid }
if (t.x < fromTime || t.x > endTime) continue .forEach {
if (t.isSMB && !t.isValid) continue it.y = getNearestBg(it.x.toLong())
t.y = getNearestBg(t.x.toLong()) filteredTreatments.add(it)
filteredTreatments.add(t) }
}
// ProfileSwitch // ProfileSwitch
val profileSwitches = treatmentsPlugin.profileSwitchesFromHistory.list treatmentsPlugin.profileSwitchesFromHistory.list
for (tx in profileSwitches.indices) { .filterTimeframe(fromTime, endTime)
val t: DataPointWithLabelInterface = profileSwitches[tx] .forEach(filteredTreatments::add)
if (t.x < fromTime || t.x > endTime) continue
filteredTreatments.add(t)
}
// Extended bolus // Extended bolus
if (!activePlugin.activePump.isFakingTempsByExtendedBoluses) { if (!activePlugin.activePump.isFakingTempsByExtendedBoluses) {
val extendedBoluses = treatmentsPlugin.extendedBolusesFromHistory.list treatmentsPlugin.extendedBolusesFromHistory.list
for (tx in extendedBoluses.indices) { .filterTimeframe(fromTime, endTime)
val t: DataPointWithLabelInterface = extendedBoluses[tx] .filter { it.duration != 0L }
if (t.x + t.duration < fromTime || t.x > endTime) continue .forEach {
if (t.duration == 0L) continue it.y = getNearestBg(it.x.toLong())
t.y = getNearestBg(t.x.toLong()) filteredTreatments.add(it)
filteredTreatments.add(t) }
}
} }
// Careportal // Careportal
val careportalEvents = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime - 6 * 60 * 60 * 1000, true) MainApp.getDbHelper().getCareportalEventsFromTime(fromTime - 6 * 60 * 60 * 1000, true)
for (tx in careportalEvents.indices) { .filterTimeframe(fromTime, endTime)
val t: DataPointWithLabelInterface = careportalEvents[tx] .forEach {
if (t.x + t.duration < fromTime || t.x > endTime) continue it.y = getNearestBg(it.x.toLong())
t.y = getNearestBg(t.x.toLong()) filteredTreatments.add(it)
filteredTreatments.add(t) }
}
addSeries(PointsWithLabelGraphSeries(Array(filteredTreatments.size) { i -> filteredTreatments[i] })) // increase maxY if a treatment forces it's own height that's higher than a BG value
filteredTreatments.map { it.y }
.maxOrNull()
?.let(::addUpperChartMargin)
?.let { maxY = maxOf(maxY, it) }
addSeries(PointsWithLabelGraphSeries(filteredTreatments.toTypedArray()))
} }
private fun getNearestBg(date: Long): Double { private fun getNearestBg(date: Long): Double {
@ -604,3 +609,6 @@ class GraphData(
graph.onDataChanged(false, false) graph.onDataChanged(false, false)
} }
} }
private fun <E : DataPointWithLabelInterface> List<E>.filterTimeframe(fromTime: Long, endTime: Long): List<E> =
filter { it.x + it.duration >= fromTime && it.x <= endTime }