basic graph on everview fragment

This commit is contained in:
Milos Kozak 2016-06-09 00:01:28 +02:00
parent 3b883dee31
commit 45cceddfcc
7 changed files with 102 additions and 16 deletions

View file

@ -31,4 +31,5 @@ dependencies {
compile 'com.j256.ormlite:ormlite-android:4.46' compile 'com.j256.ormlite:ormlite-android:4.46'
compile 'com.github.tony19:logback-android-classic:1.1.1-4' compile 'com.github.tony19:logback-android-classic:1.1.1-4'
compile 'org.slf4j:slf4j-api:1.7.12' compile 'org.slf4j:slf4j-api:1.7.12'
compile 'com.jjoe64:graphview:4.0.1'
} }

View file

@ -2,6 +2,8 @@ package info.nightscout.androidaps.db;
import com.j256.ormlite.field.DatabaseField; import com.j256.ormlite.field.DatabaseField;
import com.j256.ormlite.table.DatabaseTable; import com.j256.ormlite.table.DatabaseTable;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.DataPointInterface;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -14,7 +16,7 @@ import info.nightscout.client.data.NSCal;
import info.nightscout.client.data.NSSgv; import info.nightscout.client.data.NSSgv;
@DatabaseTable(tableName = "BgReadings") @DatabaseTable(tableName = "BgReadings")
public class BgReading { public class BgReading implements DataPointInterface {
private static Logger log = LoggerFactory.getLogger(BgReading.class); private static Logger log = LoggerFactory.getLogger(BgReading.class);
public static final DecimalFormat mmolFormat = new DecimalFormat("0.0"); public static final DecimalFormat mmolFormat = new DecimalFormat("0.0");
public static final DecimalFormat mgdlFormat = new DecimalFormat("0"); public static final DecimalFormat mgdlFormat = new DecimalFormat("0");
@ -45,6 +47,8 @@ public class BgReading {
@DatabaseField @DatabaseField
public int battery_level; public int battery_level;
public static String units = Constants.MGDL;
public BgReading() {} public BgReading() {}
public BgReading(NSSgv sgv) { public BgReading(NSSgv sgv) {
@ -77,4 +81,14 @@ public class BgReading {
", battery_level=" + battery_level + ", battery_level=" + battery_level +
'}'; '}';
} }
@Override
public double getX() {
return timestamp;
}
@Override
public double getY() {
return valueToUnits(units);
}
} }

View file

@ -1,6 +1,7 @@
package info.nightscout.androidaps.db; package info.nightscout.androidaps.db;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -11,6 +12,7 @@ import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao; import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.stmt.PreparedQuery; import com.j256.ormlite.stmt.PreparedQuery;
import com.j256.ormlite.stmt.QueryBuilder; import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.j256.ormlite.support.ConnectionSource; import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils; import com.j256.ormlite.table.TableUtils;
@ -140,4 +142,24 @@ public class DatabaseHelper extends OrmLiteSqliteOpenHelper {
return null; return null;
} }
public List<BgReading> get12HoursOfBg() {
try {
Dao<BgReading, Long> daoBgreadings = getDaoBgReadings();
List<BgReading> bgReadings;
QueryBuilder<BgReading, Long> queryBuilder = daoBgreadings.queryBuilder();
queryBuilder.orderBy("timeIndex", false);
Where where = queryBuilder.where();
long now = new Date().getTime();
long dayAgo = now - 12 * 60 * 60 * 1000l;
where.ge("timeIndex", (long) Math.ceil(dayAgo / 60000d));
PreparedQuery<BgReading> preparedQuery = queryBuilder.prepare();
bgReadings = daoBgreadings.query(preparedQuery);
return bgReadings;
} catch (SQLException e) {
e.printStackTrace();
}
return new ArrayList<BgReading>();
}
} }

View file

@ -1,22 +1,24 @@
package info.nightscout.androidaps.plugins.Overview; package info.nightscout.androidaps.plugins.Overview;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.TextView; import android.widget.TextView;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.helper.DateAsXAxisLabelFormatter;
import com.jjoe64.graphview.series.PointsGraphSeries;
import com.squareup.otto.Subscribe; import com.squareup.otto.Subscribe;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.List;
import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.db.BgReading; import info.nightscout.androidaps.db.BgReading;
@ -29,6 +31,7 @@ public class OverviewFragment extends Fragment {
private static Logger log = LoggerFactory.getLogger(OverviewFragment.class); private static Logger log = LoggerFactory.getLogger(OverviewFragment.class);
TextView bg; TextView bg;
GraphView bgGraph;
public static OverviewFragment newInstance() { public static OverviewFragment newInstance() {
OverviewFragment fragment = new OverviewFragment(); OverviewFragment fragment = new OverviewFragment();
@ -46,6 +49,9 @@ public class OverviewFragment extends Fragment {
Bundle savedInstanceState) { Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.overview_fragment, container, false); View view = inflater.inflate(R.layout.overview_fragment, container, false);
bg = (TextView) view.findViewById(R.id.overview_bg); bg = (TextView) view.findViewById(R.id.overview_bg);
bgGraph = (GraphView) view.findViewById(R.id.overview_bggraph);
updateData();
return view; return view;
} }
@ -61,8 +67,39 @@ public class OverviewFragment extends Fragment {
private void updateData() { private void updateData() {
BgReading bgReading = MainApp.getDbHelper().lastBg(); BgReading bgReading = MainApp.getDbHelper().lastBg();
NSProfile profile = MainApp.getNSProfile(); NSProfile profile = MainApp.getNSProfile();
if (profile != null && bgReading != null && bg != null) if (profile != null && bgReading != null && bg != null) {
bg.setText(bgReading.valueToUnitsToString(profile.getUnits())); bg.setText(bgReading.valueToUnitsToString(profile.getUnits()));
BgReading.units = profile.getUnits();
}
// Skip if not initialized yet
if (bgGraph == null)
return;
List<BgReading> bgReadingsArray = MainApp.getDbHelper().get12HoursOfBg();
BgReading[] bgReadings = new BgReading[bgReadingsArray.size()];
bgReadings = bgReadingsArray.toArray(bgReadings);
PointsGraphSeries<BgReading> series = new PointsGraphSeries<BgReading>(bgReadings);
bgGraph.addSeries(series);
series.setShape(PointsGraphSeries.Shape.POINT);
series.setSize(5);
series.setColor(Color.GREEN);
// set date label formatter
bgGraph.getGridLabelRenderer().setLabelFormatter(new DateAsXAxisLabelFormatter(getActivity(), android.text.format.DateFormat.getTimeFormat(getActivity())));
bgGraph.getGridLabelRenderer().setNumVerticalLabels(5); // only 4 because of the space
//bgGraph.getGridLabelRenderer().setNumHorizontalLabels(3); // only 4 because of the space
// set manual x bounds to have nice steps
bgGraph.getViewport().setMaxX(bgReadings[0].timestamp);
bgGraph.getViewport().setMinX(bgReadings[bgReadings.length-1].timestamp);
bgGraph.getViewport().setXAxisBoundsManual(true);
// set manual y bounds to have nice steps
bgGraph.getViewport().setMaxY(20);
bgGraph.getViewport().setMinY(0);
bgGraph.getViewport().setYAxisBoundsManual(true);
} }
@Subscribe @Subscribe

View file

@ -259,7 +259,7 @@ public class TempBasalsFragment extends Fragment {
@Subscribe @Subscribe
public void onStatusEvent(final EventTempBasalChange ev) { public void onStatusEvent(final EventTempBasalChange ev) {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -274,7 +274,7 @@ public class TempBasalsFragment extends Fragment {
@Subscribe @Subscribe
public void onStatusEvent(final EventNewBG ev) { public void onStatusEvent(final EventNewBG ev) {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {

View file

@ -251,7 +251,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
@Subscribe @Subscribe
public void onStatusEvent(final EventTreatmentChange ev) { public void onStatusEvent(final EventTreatmentChange ev) {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
@ -265,7 +265,7 @@ public class TreatmentsFragment extends Fragment implements View.OnClickListener
@Subscribe @Subscribe
public void onStatusEvent(final EventNewBG ev) { public void onStatusEvent(final EventNewBG ev) {
Activity activity = getActivity(); Activity activity = getActivity();
if (activity != null) if (activity != null && recyclerView != null)
activity.runOnUiThread(new Runnable() { activity.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {

View file

@ -5,10 +5,22 @@
tools:context="info.nightscout.androidaps.plugins.Overview.OverviewFragment"> tools:context="info.nightscout.androidaps.plugins.Overview.OverviewFragment">
<TextView <LinearLayout
android:layout_width="wrap_content" android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="match_parent"
android:id="@+id/overview_bg" android:layout_height="match_parent">
android:layout_gravity="left|top"
android:textSize="100dp" /> <TextView
android:id="@+id/overview_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:textSize="100dp" />
<com.jjoe64.graphview.GraphView
android:id="@+id/overview_bggraph"
android:layout_width="match_parent"
android:layout_height="200dip" />
</LinearLayout>
</FrameLayout> </FrameLayout>