From 19f107fe53a22be991689ab6217bbf3cd5ff73db Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Wed, 18 Oct 2017 23:56:13 +0200 Subject: [PATCH] move preparing graph series to extra class #2 --- .../plugins/Overview/OverviewFragment.java | 68 +------ .../plugins/Overview/graphData/GraphData.java | 172 ++++++++++++------ .../Overview/graphExtensions/Scale.java | 17 ++ .../graphExtensions/ScaledDataPoint.java | 46 +++++ 4 files changed, 182 insertions(+), 121 deletions(-) create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/Scale.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/ScaledDataPoint.java 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 fc668032b5..638b44374f 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 @@ -1440,19 +1440,18 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, } // remove old data from graph - bgGraph.getSecondScale().getSeries().clear(); bgGraph.getSeries().clear(); - GraphData graphData = new GraphData(bgGraph); + GraphData graphData = new GraphData(); // **** In range Area **** - graphData.addInRangeArea(fromTime, endTime, lowLine, highLine); + graphData.addInRangeArea(bgGraph, fromTime, endTime, lowLine, highLine); // **** BG **** if (showPrediction) - graphData.addBgReadings(fromTime, toTime, lowLine, highLine, (DetermineBasalResultAMA) finalLastRun.constraintsProcessed); + graphData.addBgReadings(bgGraph, fromTime, toTime, lowLine, highLine, (DetermineBasalResultAMA) finalLastRun.constraintsProcessed); else - graphData.addBgReadings(fromTime, toTime, lowLine, highLine, null); + graphData.addBgReadings(bgGraph, fromTime, toTime, lowLine, highLine, null); // set manual x bounds to have nice steps bgGraph.getViewport().setMaxX(endTime); @@ -1468,70 +1467,17 @@ public class OverviewFragment extends Fragment implements View.OnClickListener, // Treatments - List filteredTreatments = new ArrayList<>(); - - List treatments = MainApp.getConfigBuilder().getTreatmentsFromHistory(); - - for (int tx = 0; tx < treatments.size(); tx++) { - Treatment t = treatments.get(tx); - if (t.getX() < fromTime || t.getX() > endTime) continue; - t.setY(graphData.getNearestBg((long) t.getX())); - filteredTreatments.add(t); - } - - // ProfileSwitch - List profileSwitches = MainApp.getConfigBuilder().getProfileSwitchesFromHistory().getList(); - - for (int tx = 0; tx < profileSwitches.size(); tx++) { - DataPointWithLabelInterface t = profileSwitches.get(tx); - if (t.getX() < fromTime || t.getX() > endTime) continue; - filteredTreatments.add(t); - } - - // Extended bolus - if (!pump.isFakingTempsByExtendedBoluses()) { - List extendedBoluses = MainApp.getConfigBuilder().getExtendedBolusesFromHistory().getList(); - - for (int tx = 0; tx < extendedBoluses.size(); tx++) { - DataPointWithLabelInterface t = extendedBoluses.get(tx); - if (t.getX() + t.getDuration() < fromTime || t.getX() > endTime) continue; - if (t.getDuration() == 0) continue; - t.setY(graphData.getNearestBg((long) t.getX())); - filteredTreatments.add(t); - } - } - - // Careportal - List careportalEvents = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime, true); - - for (int tx = 0; tx < careportalEvents.size(); tx++) { - DataPointWithLabelInterface t = careportalEvents.get(tx); - if (t.getX() + t.getDuration() < fromTime || t.getX() > endTime) continue; - t.setY(graphData.getNearestBg((long) t.getX())); - filteredTreatments.add(t); - } - - DataPointWithLabelInterface[] treatmentsArray = new DataPointWithLabelInterface[filteredTreatments.size()]; - treatmentsArray = filteredTreatments.toArray(treatmentsArray); - if (treatmentsArray.length > 0) { - addSeriesWithoutInvalidate(new PointsWithLabelGraphSeries<>(treatmentsArray), bgGraph); - } - - // set manual y bounds to have nice steps - bgGraph.getViewport().setMaxY(graphData.maxBgValue); - bgGraph.getViewport().setMinY(0); - bgGraph.getViewport().setYAxisBoundsManual(true); - bgGraph.getGridLabelRenderer().setNumVerticalLabels(graphData.numOfVertLines); + graphData.addTreatmnets(bgGraph, fromTime, endTime); // add basal data if (pump.getPumpDescription().isTempBasalCapable && showBasalsView.isChecked()) { - graphData.addBasalsToSecondScale(fromTime, now, graphData.maxBgValue / lowLine * 1.2d); + graphData.addBasals(bgGraph, fromTime, now, lowLine / graphData.maxX / 1.2d); } // **** NOW line **** DataPoint[] nowPoints = new DataPoint[]{ new DataPoint(now, 0), - new DataPoint(now, graphData.maxBgValue) + new DataPoint(now, graphData.maxX) }; addSeriesWithoutInvalidate(seriesNow = new LineGraphSeries<>(nowPoints), bgGraph); seriesNow.setDrawDataPoints(false); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java index d586109564..ea6de9f9a4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphData/GraphData.java @@ -10,6 +10,8 @@ import com.jjoe64.graphview.series.DataPoint; import com.jjoe64.graphview.series.LineGraphSeries; import com.jjoe64.graphview.series.Series; +import org.mozilla.javascript.tools.debugger.Main; + import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -19,6 +21,10 @@ import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; import info.nightscout.androidaps.data.Profile; import info.nightscout.androidaps.db.BgReading; +import info.nightscout.androidaps.db.CareportalEvent; +import info.nightscout.androidaps.db.ExtendedBolus; +import info.nightscout.androidaps.db.ProfileSwitch; +import info.nightscout.androidaps.db.Treatment; import info.nightscout.androidaps.plugins.IobCobCalculator.IobCobCalculatorPlugin; import info.nightscout.androidaps.plugins.IobCobCalculator.events.BasalData; import info.nightscout.androidaps.plugins.OpenAPSAMA.DetermineBasalResultAMA; @@ -26,6 +32,8 @@ import info.nightscout.androidaps.plugins.Overview.graphExtensions.AreaGraphSeri import info.nightscout.androidaps.plugins.Overview.graphExtensions.DataPointWithLabelInterface; import info.nightscout.androidaps.plugins.Overview.graphExtensions.DoubleDataPoint; import info.nightscout.androidaps.plugins.Overview.graphExtensions.PointsWithLabelGraphSeries; +import info.nightscout.androidaps.plugins.Overview.graphExtensions.Scale; +import info.nightscout.androidaps.plugins.Overview.graphExtensions.ScaledDataPoint; import info.nightscout.utils.Round; /** @@ -34,19 +42,16 @@ import info.nightscout.utils.Round; public class GraphData { - public GraphData(GraphView bgGraph) { - this.bgGraph = bgGraph; + public GraphData() { units = MainApp.getConfigBuilder().getProfileUnits(); } - private GraphView bgGraph; - public double maxBgValue = 0d; - public int numOfVertLines; + public double maxX = 0; private List bgReadingsArray; private String units; - public void addBgReadings(long fromTime, long toTime, double lowLine, double highLine, DetermineBasalResultAMA amaResult) { - maxBgValue = 0d; + public void addBgReadings(GraphView bgGraph, long fromTime, long toTime, double lowLine, double highLine, DetermineBasalResultAMA amaResult) { + double maxBgValue = 0d; bgReadingsArray = MainApp.getDbHelper().getBgreadingsDataFromTime(fromTime, true); List bgListArray = new ArrayList<>(); @@ -68,18 +73,25 @@ public class GraphData { maxBgValue = Profile.fromMgdlToUnits(maxBgValue, units); maxBgValue = units.equals(Constants.MGDL) ? Round.roundTo(maxBgValue, 40d) + 80 : Round.roundTo(maxBgValue, 2d) + 4; if (highLine > maxBgValue) maxBgValue = highLine; - numOfVertLines = units.equals(Constants.MGDL) ? (int) (maxBgValue / 40 + 1) : (int) (maxBgValue / 2 + 1); + int numOfVertLines = units.equals(Constants.MGDL) ? (int) (maxBgValue / 40 + 1) : (int) (maxBgValue / 2 + 1); DataPointWithLabelInterface[] bg = new DataPointWithLabelInterface[bgListArray.size()]; bg = bgListArray.toArray(bg); if (bg.length > 0) { - addSeriesWithoutInvalidate(new PointsWithLabelGraphSeries<>(bg)); + addSeriesWithoutInvalidate(bgGraph, new PointsWithLabelGraphSeries<>(bg)); } + maxX = maxBgValue; + // set manual y bounds to have nice steps + bgGraph.getViewport().setMaxY(maxX); + bgGraph.getViewport().setMinY(0); + bgGraph.getViewport().setYAxisBoundsManual(true); + bgGraph.getGridLabelRenderer().setNumVerticalLabels(numOfVertLines); + } - public void addInRangeArea(long fromTime, long toTime, double lowLine, double highLine) { + public void addInRangeArea(GraphView bgGraph, long fromTime, long toTime, double lowLine, double highLine) { AreaGraphSeries inRangeAreaSeries; DoubleDataPoint[] inRangeAreaDataPoints = new DoubleDataPoint[]{ @@ -87,24 +99,26 @@ public class GraphData { new DoubleDataPoint(toTime, lowLine, highLine) }; inRangeAreaSeries = new AreaGraphSeries<>(inRangeAreaDataPoints); - addSeriesWithoutInvalidate(inRangeAreaSeries); + addSeriesWithoutInvalidate(bgGraph, inRangeAreaSeries); inRangeAreaSeries.setColor(0); inRangeAreaSeries.setDrawBackground(true); inRangeAreaSeries.setBackgroundColor(MainApp.sResources.getColor(R.color.inrangebackground)); } - public void addBasalsToSecondScale(long fromTime, long toTime, double scale) { - LineGraphSeries basalsLineSeries; - LineGraphSeries absoluteBasalsLineSeries; - LineGraphSeries baseBasalsSeries; - LineGraphSeries tempBasalsSeries; + // scale in % of vertical size (like 0.3) + public void addBasals(GraphView bgGraph, long fromTime, long toTime, double scale) { + LineGraphSeries basalsLineSeries; + LineGraphSeries absoluteBasalsLineSeries; + LineGraphSeries baseBasalsSeries; + LineGraphSeries tempBasalsSeries; - Double maxBasalValueFound = 0d; + double maxBasalValueFound = 0d; + Scale basalScale = new Scale(); - List baseBasalArray = new ArrayList<>(); - List tempBasalArray = new ArrayList<>(); - List basalLineArray = new ArrayList<>(); - List absoluteBasalLineArray = new ArrayList<>(); + List baseBasalArray = new ArrayList<>(); + List tempBasalArray = new ArrayList<>(); + List basalLineArray = new ArrayList<>(); + List absoluteBasalLineArray = new ArrayList<>(); double lastLineBasal = 0; double lastAbsoluteLineBasal = 0; double lastBaseBasal = 0; @@ -118,33 +132,33 @@ public class GraphData { if (basalData.isTempBasalRunning) { absoluteLineValue = tempBasalValue = basalData.tempBasalAbsolute; if (tempBasalValue != lastTempBasal) { - tempBasalArray.add(new DataPoint(time, lastTempBasal)); - tempBasalArray.add(new DataPoint(time, basal = tempBasalValue)); + tempBasalArray.add(new ScaledDataPoint(time, lastTempBasal, basalScale)); + tempBasalArray.add(new ScaledDataPoint(time, basal = tempBasalValue, basalScale)); } if (lastBaseBasal != 0d) { - baseBasalArray.add(new DataPoint(time, lastBaseBasal)); - baseBasalArray.add(new DataPoint(time, 0d)); + baseBasalArray.add(new ScaledDataPoint(time, lastBaseBasal, basalScale)); + baseBasalArray.add(new ScaledDataPoint(time, 0d, basalScale)); lastBaseBasal = 0d; } } else { if (baseBasalValue != lastBaseBasal) { - baseBasalArray.add(new DataPoint(time, lastBaseBasal)); - baseBasalArray.add(new DataPoint(time, basal = baseBasalValue)); + baseBasalArray.add(new ScaledDataPoint(time, lastBaseBasal, basalScale)); + baseBasalArray.add(new ScaledDataPoint(time, basal = baseBasalValue, basalScale)); lastBaseBasal = baseBasalValue; } if (lastTempBasal != 0) { - tempBasalArray.add(new DataPoint(time, lastTempBasal)); - tempBasalArray.add(new DataPoint(time, 0d)); + tempBasalArray.add(new ScaledDataPoint(time, lastTempBasal, basalScale)); + tempBasalArray.add(new ScaledDataPoint(time, 0d, basalScale)); } } if (baseBasalValue != lastLineBasal) { - basalLineArray.add(new DataPoint(time, lastLineBasal)); - basalLineArray.add(new DataPoint(time, baseBasalValue)); + basalLineArray.add(new ScaledDataPoint(time, lastLineBasal, basalScale)); + basalLineArray.add(new ScaledDataPoint(time, baseBasalValue, basalScale)); } if (absoluteLineValue != lastAbsoluteLineBasal) { - absoluteBasalLineArray.add(new DataPoint(time, lastAbsoluteLineBasal)); - absoluteBasalLineArray.add(new DataPoint(time, basal)); + absoluteBasalLineArray.add(new ScaledDataPoint(time, lastAbsoluteLineBasal, basalScale)); + absoluteBasalLineArray.add(new ScaledDataPoint(time, basal, basalScale)); } lastAbsoluteLineBasal = absoluteLineValue; @@ -153,26 +167,26 @@ public class GraphData { maxBasalValueFound = Math.max(maxBasalValueFound, basal); } - basalLineArray.add(new DataPoint(toTime, lastLineBasal)); - baseBasalArray.add(new DataPoint(toTime, lastBaseBasal)); - tempBasalArray.add(new DataPoint(toTime, lastTempBasal)); - absoluteBasalLineArray.add(new DataPoint(toTime, lastAbsoluteLineBasal)); + basalLineArray.add(new ScaledDataPoint(toTime, lastLineBasal, basalScale)); + baseBasalArray.add(new ScaledDataPoint(toTime, lastBaseBasal, basalScale)); + tempBasalArray.add(new ScaledDataPoint(toTime, lastTempBasal, basalScale)); + absoluteBasalLineArray.add(new ScaledDataPoint(toTime, lastAbsoluteLineBasal, basalScale)); - DataPoint[] baseBasal = new DataPoint[baseBasalArray.size()]; + ScaledDataPoint[] baseBasal = new ScaledDataPoint[baseBasalArray.size()]; baseBasal = baseBasalArray.toArray(baseBasal); baseBasalsSeries = new LineGraphSeries<>(baseBasal); baseBasalsSeries.setDrawBackground(true); baseBasalsSeries.setBackgroundColor(MainApp.sResources.getColor(R.color.basebasal)); baseBasalsSeries.setThickness(0); - DataPoint[] tempBasal = new DataPoint[tempBasalArray.size()]; + ScaledDataPoint[] tempBasal = new ScaledDataPoint[tempBasalArray.size()]; tempBasal = tempBasalArray.toArray(tempBasal); tempBasalsSeries = new LineGraphSeries<>(tempBasal); tempBasalsSeries.setDrawBackground(true); tempBasalsSeries.setBackgroundColor(MainApp.sResources.getColor(R.color.tempbasal)); tempBasalsSeries.setThickness(0); - DataPoint[] basalLine = new DataPoint[basalLineArray.size()]; + ScaledDataPoint[] basalLine = new ScaledDataPoint[basalLineArray.size()]; basalLine = basalLineArray.toArray(basalLine); basalsLineSeries = new LineGraphSeries<>(basalLine); Paint paint = new Paint(); @@ -182,7 +196,7 @@ public class GraphData { paint.setColor(MainApp.sResources.getColor(R.color.basal)); basalsLineSeries.setCustomPaint(paint); - DataPoint[] absoluteBasalLine = new DataPoint[absoluteBasalLineArray.size()]; + ScaledDataPoint[] absoluteBasalLine = new ScaledDataPoint[absoluteBasalLineArray.size()]; absoluteBasalLine = absoluteBasalLineArray.toArray(absoluteBasalLine); absoluteBasalsLineSeries = new LineGraphSeries<>(absoluteBasalLine); Paint absolutePaint = new Paint(); @@ -191,27 +205,65 @@ public class GraphData { absolutePaint.setColor(MainApp.sResources.getColor(R.color.basal)); absoluteBasalsLineSeries.setCustomPaint(absolutePaint); - bgGraph.getSecondScale().setMinY(0); - bgGraph.getSecondScale().setMaxY(scale * maxBasalValueFound); - bgGraph.getSecondScale().addSeries(baseBasalsSeries); - bgGraph.getSecondScale().addSeries(tempBasalsSeries); - bgGraph.getSecondScale().addSeries(basalsLineSeries); - bgGraph.getSecondScale().addSeries(absoluteBasalsLineSeries); + basalScale.setValue(maxX * scale / maxBasalValueFound); - bgGraph.getSecondScale().setLabelFormatter(new LabelFormatter() { - @Override - public String formatLabel(double value, boolean isValueX) { - return ""; - } - - @Override - public void setViewport(Viewport viewport) { - - } - }); + addSeriesWithoutInvalidate(bgGraph, baseBasalsSeries); + addSeriesWithoutInvalidate(bgGraph, tempBasalsSeries); + addSeriesWithoutInvalidate(bgGraph, basalsLineSeries); + addSeriesWithoutInvalidate(bgGraph, absoluteBasalsLineSeries); } - public double getNearestBg(long date) { + public void addTreatmnets(GraphView bgGraph, long fromTime, long endTime) { + List filteredTreatments = new ArrayList<>(); + + List treatments = MainApp.getConfigBuilder().getTreatmentsFromHistory(); + + for (int tx = 0; tx < treatments.size(); tx++) { + Treatment t = treatments.get(tx); + if (t.getX() < fromTime || t.getX() > endTime) continue; + t.setY(getNearestBg((long) t.getX())); + filteredTreatments.add(t); + } + + // ProfileSwitch + List profileSwitches = MainApp.getConfigBuilder().getProfileSwitchesFromHistory().getList(); + + for (int tx = 0; tx < profileSwitches.size(); tx++) { + DataPointWithLabelInterface t = profileSwitches.get(tx); + if (t.getX() < fromTime || t.getX() > endTime) continue; + filteredTreatments.add(t); + } + + // Extended bolus + if (!MainApp.getConfigBuilder().isFakingTempsByExtendedBoluses()) { + List extendedBoluses = MainApp.getConfigBuilder().getExtendedBolusesFromHistory().getList(); + + for (int tx = 0; tx < extendedBoluses.size(); tx++) { + DataPointWithLabelInterface t = extendedBoluses.get(tx); + if (t.getX() + t.getDuration() < fromTime || t.getX() > endTime) continue; + if (t.getDuration() == 0) continue; + t.setY(getNearestBg((long) t.getX())); + filteredTreatments.add(t); + } + } + + // Careportal + List careportalEvents = MainApp.getDbHelper().getCareportalEventsFromTime(fromTime, true); + + for (int tx = 0; tx < careportalEvents.size(); tx++) { + DataPointWithLabelInterface t = careportalEvents.get(tx); + if (t.getX() + t.getDuration() < fromTime || t.getX() > endTime) continue; + t.setY(getNearestBg((long) t.getX())); + filteredTreatments.add(t); + } + + DataPointWithLabelInterface[] treatmentsArray = new DataPointWithLabelInterface[filteredTreatments.size()]; + treatmentsArray = filteredTreatments.toArray(treatmentsArray); + if (treatmentsArray.length > 0) { + addSeriesWithoutInvalidate(bgGraph, new PointsWithLabelGraphSeries<>(treatmentsArray)); + } } + + double getNearestBg(long date) { double bg = 0; for (int r = bgReadingsArray.size() - 1; r >= 0; r--) { BgReading reading = bgReadingsArray.get(r); @@ -223,7 +275,7 @@ public class GraphData { } - private void addSeriesWithoutInvalidate(Series s) { + private void addSeriesWithoutInvalidate(GraphView bgGraph, Series s) { s.onGraphViewAttached(bgGraph); bgGraph.getSeries().add(s); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/Scale.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/Scale.java new file mode 100644 index 0000000000..1acb357a4b --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/Scale.java @@ -0,0 +1,17 @@ +package info.nightscout.androidaps.plugins.Overview.graphExtensions; + +/** + * Created by mike on 18.10.2017. + */ + +public class Scale { + private double value; + + public void setValue(double value) { + this.value = value; + } + + public double getValue() { + return value; + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/ScaledDataPoint.java b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/ScaledDataPoint.java new file mode 100644 index 0000000000..e5d2364be7 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/Overview/graphExtensions/ScaledDataPoint.java @@ -0,0 +1,46 @@ +package info.nightscout.androidaps.plugins.Overview.graphExtensions; + +import com.jjoe64.graphview.series.DataPointInterface; + +import java.io.Serializable; +import java.util.Date; + +/** + * Created by mike on 18.10.2017. + */ + +public class ScaledDataPoint implements DataPointInterface, Serializable { + private static final long serialVersionUID=1428263342645L; + + private double x; + private double y; + + private Scale scale; + + public ScaledDataPoint(double x, double y, Scale scale) { + this.x=x; + this.y=y; + this.scale = scale; + } + + public ScaledDataPoint(Date x, double y, Scale scale) { + this.x = x.getTime(); + this.y = y; + this.scale = scale; + } + + @Override + public double getX() { + return x; + } + + @Override + public double getY() { + return y * scale.getValue(); + } + + @Override + public String toString() { + return "["+x+"/"+y+"]"; + } +}