From df655a234e893aaa7741855306d78f344ac11325 Mon Sep 17 00:00:00 2001 From: AdrianLxM Date: Sun, 24 Jan 2021 20:56:31 +0100 Subject: [PATCH] include treatments in chart height --- .../historyBrowser/HistoryBrowseActivity.kt | 6 +- .../general/overview/OverviewFragment.kt | 9 ++- .../general/overview/graphData/GraphData.kt | 80 ++++++++++--------- 3 files changed, 54 insertions(+), 41 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.kt b/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.kt index 2e246a2c62..e4b37130e4 100644 --- a/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.kt +++ b/app/src/main/java/info/nightscout/androidaps/historyBrowser/HistoryBrowseActivity.kt @@ -308,8 +308,6 @@ class HistoryBrowseActivity : NoSplashAppCompatActivity() { // **** BG **** graphData.addBgReadings(fromTime, toTime, lowLine, highLine, null) - // set manual x bounds to have nice steps - graphData.formatAxis(fromTime, toTime) // add target line 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 graphData.performUpdate() diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt index cf12a53b27..9ad9c606bf 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/OverviewFragment.kt @@ -846,11 +846,14 @@ class OverviewFragment : DaggerFragment(), View.OnClickListener, OnLongClickList graphData.addBgReadings(fromTime, toTime, lowLine, highLine, apsResult?.predictions) else graphData.addBgReadings(fromTime, toTime, lowLine, highLine, null) - // set manual x bounds to have nice steps - graphData.formatAxis(fromTime, endTime) - // Treatments 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]) graphData.addActivity(fromTime, endTime, false, 0.8) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/graphData/GraphData.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/graphData/GraphData.kt index f3a779af22..e5d0ff4fab 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/graphData/GraphData.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/overview/graphData/GraphData.kt @@ -17,11 +17,11 @@ import info.nightscout.androidaps.data.Profile import info.nightscout.androidaps.db.BgReading import info.nightscout.androidaps.interfaces.ActivePluginProvider import info.nightscout.androidaps.interfaces.LoopInterface +import info.nightscout.androidaps.interfaces.ProfileFunction import info.nightscout.androidaps.interfaces.TreatmentsInterface import info.nightscout.androidaps.logging.AAPSLogger import info.nightscout.androidaps.logging.LTag 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.iob.iobCobCalculator.AutosensResult 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) } 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 - val numOfVerticalLines = if (units == Constants.MGDL) (maxBgValue / 40 + 1).toInt() else (maxBgValue / 2 + 1).toInt() maxY = maxBgValue minY = 0.0 - // set manual y bounds to have nice steps - graph.gridLabelRenderer.numVerticalLabels = numOfVerticalLines 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) { val inRangeAreaSeries: AreaGraphSeries val inRangeAreaDataPoints = arrayOf( @@ -233,44 +237,45 @@ class GraphData( fun addTreatments(fromTime: Long, endTime: Long) { val filteredTreatments: MutableList = ArrayList() - val treatments = treatmentsPlugin.treatmentsFromHistory - for (tx in treatments.indices) { - val t = treatments[tx] - if (t.x < fromTime || t.x > endTime) continue - if (t.isSMB && !t.isValid) continue - t.y = getNearestBg(t.x.toLong()) - filteredTreatments.add(t) - } + treatmentsPlugin.treatmentsFromHistory + .filterTimeframe(fromTime, endTime) + .filter { !it.isSMB || it.isValid } + .forEach { + it.y = getNearestBg(it.x.toLong()) + filteredTreatments.add(it) + } // ProfileSwitch - val profileSwitches = treatmentsPlugin.profileSwitchesFromHistory.list - for (tx in profileSwitches.indices) { - val t: DataPointWithLabelInterface = profileSwitches[tx] - if (t.x < fromTime || t.x > endTime) continue - filteredTreatments.add(t) - } + treatmentsPlugin.profileSwitchesFromHistory.list + .filterTimeframe(fromTime, endTime) + .forEach(filteredTreatments::add) // Extended bolus if (!activePlugin.activePump.isFakingTempsByExtendedBoluses) { - val extendedBoluses = treatmentsPlugin.extendedBolusesFromHistory.list - for (tx in extendedBoluses.indices) { - val t: DataPointWithLabelInterface = extendedBoluses[tx] - if (t.x + t.duration < fromTime || t.x > endTime) continue - if (t.duration == 0L) continue - t.y = getNearestBg(t.x.toLong()) - filteredTreatments.add(t) - } + treatmentsPlugin.extendedBolusesFromHistory.list + .filterTimeframe(fromTime, endTime) + .filter { it.duration != 0L } + .forEach { + it.y = getNearestBg(it.x.toLong()) + filteredTreatments.add(it) + } } // Careportal - val careportalEvents = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime - 6 * 60 * 60 * 1000, true) - for (tx in careportalEvents.indices) { - val t: DataPointWithLabelInterface = careportalEvents[tx] - if (t.x + t.duration < fromTime || t.x > endTime) continue - t.y = getNearestBg(t.x.toLong()) - filteredTreatments.add(t) - } - addSeries(PointsWithLabelGraphSeries(Array(filteredTreatments.size) { i -> filteredTreatments[i] })) + MainApp.getDbHelper().getCareportalEventsFromTime(fromTime - 6 * 60 * 60 * 1000, true) + .filterTimeframe(fromTime, endTime) + .forEach { + it.y = getNearestBg(it.x.toLong()) + filteredTreatments.add(it) + } + + // 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 { @@ -603,4 +608,7 @@ class GraphData( // draw it graph.onDataChanged(false, false) } -} \ No newline at end of file +} + +private fun List.filterTimeframe(fromTime: Long, endTime: Long): List = + filter { it.x + it.duration >= fromTime && it.x <= endTime }