From 352a2a747523d6a75c13a8ba01dd4392ed9233fc Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Thu, 4 May 2017 19:51:58 +0200 Subject: [PATCH] deviations graph --- .../IobCobCalculator/AutosensData.java | 6 +- .../IobCobCalculatorPlugin.java | 5 +- .../plugins/Overview/OverviewFragment.java | 76 ++++-- app/src/main/res/layout/overview_fragment.xml | 217 +++++++++--------- app/src/main/res/values/colors.xml | 1 + app/src/main/res/values/strings.xml | 1 + 6 files changed, 174 insertions(+), 132 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java index 7210a171b6..ce66a988c3 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/AutosensData.java @@ -9,14 +9,16 @@ import java.util.Date; public class AutosensData { long time = 0L; String pastSensitivity = ""; - double deviation = 0d; + public double deviation = 0d; boolean calculateWithDeviation = false; double absorbed = 0d; double carbsFromBolus = 0d; public double cob = 0; + public double bgi = 0d; + public double delta = 0d; public String log(long time) { - return "AutosensData: " + new Date(time).toLocaleString() + " " + pastSensitivity + " Deviation=" + deviation + " Absorbed=" + absorbed + " CarbsFromBolus=" + carbsFromBolus + " COB=" + cob; + return "AutosensData: " + new Date(time).toLocaleString() + " " + pastSensitivity + " Delta=" + delta + " Bgi=" + bgi + " Deviation=" + deviation + " Absorbed=" + absorbed + " CarbsFromBolus=" + carbsFromBolus + " COB=" + cob; } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java index bbc06169b9..cda6af287a 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/IobCobCalculator/IobCobCalculatorPlugin.java @@ -280,12 +280,11 @@ public class IobCobCalculatorPlugin implements PluginBase { log.error("! value < 39"); continue; } - avgDelta = (bg - bucketed_data.get(i + 3).value) / 3; delta = (bg - bucketed_data.get(i + 1).value); IobTotal iob = calulateFromTreatmentsAndTemps(bgTime); - double bgi = Math.round((-iob.activity * sens * 5) * 100) / 100d; + double bgi = -iob.activity * sens * 5; double deviation = delta - bgi; List recentTreatments = treatmentsInterface.getTreatments5MinBack(bgTime); @@ -304,6 +303,8 @@ public class IobCobCalculatorPlugin implements PluginBase { } autosensData.cob += autosensData.carbsFromBolus; autosensData.deviation = deviation; + autosensData.bgi = bgi; + autosensData.delta = delta; // calculate autosens only without COB if (autosensData.cob <= 0) { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java index 747c9c067c..da05ad20c0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/OverviewFragment.java @@ -39,7 +39,9 @@ import com.crashlytics.android.answers.Answers; import com.crashlytics.android.answers.CustomEvent; import com.jjoe64.graphview.GraphView; import com.jjoe64.graphview.LabelFormatter; +import com.jjoe64.graphview.ValueDependentColor; import com.jjoe64.graphview.Viewport; +import com.jjoe64.graphview.series.BarGraphSeries; import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries; import com.jjoe64.graphview.series.PointsGraphSeries; @@ -147,13 +149,13 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, LinearLayout pumpStatusLayout; GraphView bgGraph; GraphView iobGraph; - RelativeLayout iobGraphLayout; ImageButton menuButton; CheckBox showPredictionView; CheckBox showBasalsView; CheckBox showIobView; CheckBox showCobView; + CheckBox showDeviationsView; RecyclerView notificationsView; LinearLayoutManager llm; @@ -223,7 +225,6 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, bgGraph = (GraphView) view.findViewById(R.id.overview_bggraph); iobGraph = (GraphView) view.findViewById(R.id.overview_iobgraph); - iobGraphLayout = (RelativeLayout) view.findViewById(R.id.overview_iobgraphlayout); menuButton = (ImageButton) view.findViewById(R.id.overview_menuButton); menuButton.setOnClickListener(this); @@ -250,6 +251,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, showBasalsView = (CheckBox) view.findViewById(R.id.overview_showbasals); showIobView = (CheckBox) view.findViewById(R.id.overview_showiob); showCobView = (CheckBox) view.findViewById(R.id.overview_showcob); + showDeviationsView = (CheckBox) view.findViewById(R.id.overview_showdeviations); showPredictionView.setChecked(SP.getBoolean("showprediction", false)); showBasalsView.setChecked(SP.getBoolean("showbasals", false)); showIobView.setChecked(SP.getBoolean("showiob", false)); @@ -258,6 +260,7 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, showBasalsView.setOnCheckedChangeListener(this); showIobView.setOnCheckedChangeListener(this); showCobView.setOnCheckedChangeListener(this); + showDeviationsView.setOnCheckedChangeListener(this); notificationsView = (RecyclerView) view.findViewById(R.id.overview_notifications); notificationsView.setHasFixedSize(true); @@ -320,9 +323,21 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, scheduleUpdateGUI("onIobCheckedChanged"); break; case R.id.overview_showcob: + showDeviationsView.setOnCheckedChangeListener(null); + showDeviationsView.setChecked(false); + showDeviationsView.setOnCheckedChangeListener(this); SP.putBoolean("showcob", showCobView.isChecked()); + SP.putBoolean("showdeviations", showDeviationsView.isChecked()); scheduleUpdateGUI("onCobCheckedChanged"); break; + case R.id.overview_showdeviations: + showCobView.setOnCheckedChangeListener(null); + showCobView.setChecked(false); + showCobView.setOnCheckedChangeListener(this); + SP.putBoolean("showcob", showCobView.isChecked()); + SP.putBoolean("showdeviations", showDeviationsView.isChecked()); + scheduleUpdateGUI("onDeviationsCheckedChanged"); + break; } } @@ -1185,25 +1200,32 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, // **** IOB COB graph **** FixedLineGraphSeries iobSeries; FixedLineGraphSeries cobSeries; + BarGraphSeries devSeries; Double maxIobValueFound = 0d; Double maxCobValueFound = 0d; + Double maxDevValueFound = 0d; - if (showIobView.isChecked() || showCobView.isChecked()) { + if (showIobView.isChecked() || showCobView.isChecked() || showDeviationsView.isChecked()) { //Date start = new Date(); List iobArray = new ArrayList<>(); List cobArray = new ArrayList<>(); + List devArray = new ArrayList<>(); for (long time = fromTime; time <= now; time += 5 * 60 * 1000L) { if (showIobView.isChecked()) { IobTotal iob = IobCobCalculatorPlugin.calulateFromTreatmentsAndTemps(time); iobArray.add(new DataPoint(time, iob.iob)); maxIobValueFound = Math.max(maxIobValueFound, Math.abs(iob.iob)); } - if (showCobView.isChecked()) { + if (showCobView.isChecked() || showDeviationsView.isChecked()) { AutosensData autosensData = IobCobCalculatorPlugin.getAutosensData(time); - if (autosensData != null) { + if (autosensData != null && showCobView.isChecked()) { cobArray.add(new DataPoint(time, autosensData.cob)); maxCobValueFound = Math.max(maxCobValueFound, autosensData.cob); } + if (autosensData != null && showDeviationsView.isChecked()) { + devArray.add(new DataPoint(time, autosensData.deviation)); + maxDevValueFound = Math.max(maxDevValueFound, Math.abs(autosensData.deviation)); + } } } //Profiler.log(log, "IOB processed", start); @@ -1214,17 +1236,21 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, iobSeries.setBackgroundColor(0x80FFFFFF & MainApp.sResources.getColor(R.color.iob)); //50% iobSeries.setColor(MainApp.sResources.getColor(R.color.iob)); iobSeries.setThickness(3); - iobSeries.setTitle("IOB"); - iobGraph.getGridLabelRenderer().setVerticalLabelsAlign(Paint.Align.LEFT); - if (showIobView.isChecked() && showCobView.isChecked()) { + if (showIobView.isChecked() && (showCobView.isChecked() || showDeviationsView.isChecked())) { List cobArrayRescaled = new ArrayList<>(); + List devArrayRescaled = new ArrayList<>(); for (int ci = 0; ci < cobArray.size(); ci++) { cobArrayRescaled.add(new DataPoint(cobArray.get(ci).getX(), cobArray.get(ci).getY() * maxIobValueFound / maxCobValueFound / 2)); } + for (int ci = 0; ci < devArray.size(); ci++) { + devArrayRescaled.add(new DataPoint(devArray.get(ci).getX(), devArray.get(ci).getY() * maxIobValueFound / maxDevValueFound)); + } cobArray = cobArrayRescaled; + devArray = devArrayRescaled; } + // COB DataPoint[] cobData = new DataPoint[cobArray.size()]; cobData = cobArray.toArray(cobData); cobSeries = new FixedLineGraphSeries<>(cobData); @@ -1232,7 +1258,21 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, cobSeries.setBackgroundColor(0xB0FFFFFF & MainApp.sResources.getColor(R.color.cob)); //50% cobSeries.setColor(MainApp.sResources.getColor(R.color.cob)); cobSeries.setThickness(3); - cobSeries.setTitle("COB"); + + // DEVIATIONS + DataPoint[] devData = new DataPoint[devArray.size()]; + devData = devArray.toArray(devData); + devSeries = new BarGraphSeries<>(devData); + devSeries.setValueDependentColor(new ValueDependentColor() { + @Override + public int get(DataPoint data) { + if (data.getY() < 0) return Color.RED; + else return Color.GREEN; + } + }); + //devSeries.setBackgroundColor(0xB0FFFFFF & MainApp.sResources.getColor(R.color.cob)); //50% + //devSeries.setColor(MainApp.sResources.getColor(R.color.cob)); + //devSeries.setThickness(3); iobGraph.removeAllSeries(); @@ -1241,21 +1281,13 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } if (showCobView.isChecked() && cobData.length > 0) { iobGraph.addSeries(cobSeries); - /* iobGraph.getSecondScale().setLabelFormatter(new LabelFormatter() { - @Override - public String formatLabel(double value, boolean isValueX) { - return ""; - } - - @Override - public void setViewport(Viewport viewport) { - } - }); -*/ } - iobGraphLayout.setVisibility(View.VISIBLE); + if (showDeviationsView.isChecked() && devData.length > 0) { + iobGraph.addSeries(devSeries); + } + iobGraph.setVisibility(View.VISIBLE); } else { - iobGraphLayout.setVisibility(View.GONE); + iobGraph.setVisibility(View.GONE); } // remove old data from graph diff --git a/app/src/main/res/layout/overview_fragment.xml b/app/src/main/res/layout/overview_fragment.xml index 3200fe5014..6f0956a67c 100644 --- a/app/src/main/res/layout/overview_fragment.xml +++ b/app/src/main/res/layout/overview_fragment.xml @@ -205,112 +205,6 @@ android:layout_gravity="center_horizontal" android:textAppearance="?android:attr/textAppearanceSmall" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index cc665a46c8..553d7b1593 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -9,6 +9,7 @@ #FFFF00 #505050 #f0003f59 + #FF0000 #77dd77 #303F9F diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index e995845ea0..df2dcc3a71 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -599,4 +599,5 @@ Bluetooh status About Missing SMS permission + DEV