Charts: In "Sleep a week" chart display light and deep sleep as stacked bars

master
Andreas Shimokawa 2017-02-26 21:41:27 +01:00
parent 8b39ef3a52
commit ac1875eea0
3 changed files with 33 additions and 27 deletions

View File

@ -86,13 +86,13 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
for (int counter = 0; counter < 7; counter++) { for (int counter = 0; counter < 7; counter++) {
ActivityAmounts amounts = getActivityAmountsForDay(db, day, device); ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
entries.add(new BarEntry(counter, getTotalForActivityAmounts(amounts))); entries.add(new BarEntry(counter, getTotalsForActivityAmounts(amounts)));
labels.add(day.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, mLocale)); labels.add(day.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, mLocale));
day.add(Calendar.DATE, 1); day.add(Calendar.DATE, 1);
} }
BarDataSet set = new BarDataSet(entries, ""); BarDataSet set = new BarDataSet(entries, "");
set.setColor(getMainColor()); set.setColors(getColors());
set.setValueFormatter(getFormatter()); set.setValueFormatter(getFormatter());
BarData barData = new BarData(set); BarData barData = new BarData(set);
@ -106,29 +106,32 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
} }
private DayData refreshDayPie(DBHandler db, Calendar day, GBDevice device) { private DayData refreshDayPie(DBHandler db, Calendar day, GBDevice device) {
ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
int totalValue = getTotalForActivityAmounts(amounts);
PieData data = new PieData(); PieData data = new PieData();
List<PieEntry> entries = new ArrayList<>(); List<PieEntry> entries = new ArrayList<>();
List<Integer> colors = new ArrayList<>(); PieDataSet set = new PieDataSet(entries, "");
entries.add(new PieEntry(totalValue, "")); //we don't want labels on the pie chart ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
colors.add(getMainColor()); float totalValues[] = getTotalsForActivityAmounts(amounts);
float totalValue = 0;
if (totalValue < mTargetValue) { for (float value : totalValues) {
entries.add(new PieEntry((mTargetValue - totalValue))); //we don't want labels on the pie chart totalValue += value;
colors.add(Color.GRAY); entries.add(new PieEntry(value));
} }
PieDataSet set = new PieDataSet(entries, "");
set.setValueFormatter(getFormatter()); set.setValueFormatter(getFormatter());
set.setColors(colors); set.setColors(getColors());
if (totalValue < mTargetValue) {
entries.add(new PieEntry((mTargetValue - totalValue)));
set.addColor(Color.GRAY);
}
data.setDataSet(set); data.setDataSet(set);
//this hides the values (numeric) added to the set. These would be shown aside the strings set with addXValue above //this hides the values (numeric) added to the set. These would be shown aside the strings set with addXValue above
data.setDrawValues(false); data.setDrawValues(false);
return new DayData(data, formatPieValue(totalValue)); return new DayData(data, formatPieValue((int) totalValue));
} }
protected abstract String formatPieValue(int value); protected abstract String formatPieValue(int value);
@ -286,9 +289,9 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
abstract int getGoal(); abstract int getGoal();
abstract int getTotalForActivityAmounts(ActivityAmounts activityAmounts); abstract float[] getTotalsForActivityAmounts(ActivityAmounts activityAmounts);
abstract IValueFormatter getFormatter(); abstract IValueFormatter getFormatter();
abstract Integer getMainColor(); abstract int[] getColors();
} }

View File

@ -24,14 +24,17 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
} }
@Override @Override
int getTotalForActivityAmounts(ActivityAmounts activityAmounts) { float[] getTotalsForActivityAmounts(ActivityAmounts activityAmounts) {
long totalSeconds = 0; long totalSecondsDeepSleep = 0;
long totalSecondsLightSleep = 0;
for (ActivityAmount amount : activityAmounts.getAmounts()) { for (ActivityAmount amount : activityAmounts.getAmounts()) {
if ((amount.getActivityKind() & ActivityKind.TYPE_SLEEP) != 0) { if (amount.getActivityKind() == ActivityKind.TYPE_DEEP_SLEEP) {
totalSeconds += amount.getTotalSeconds(); totalSecondsDeepSleep += amount.getTotalSeconds();
} else if (amount.getActivityKind() == ActivityKind.TYPE_LIGHT_SLEEP) {
totalSecondsLightSleep += amount.getTotalSeconds();
} }
} }
return (int) (totalSeconds / 60); return new float[]{(int) (totalSecondsDeepSleep / 60), (int) (totalSecondsLightSleep / 60)};
} }
@Override @Override
@ -50,7 +53,7 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
} }
@Override @Override
Integer getMainColor() { int[] getColors() {
return akLightSleep.color; return new int[]{akDeepSleep.color, akLightSleep.color};
} }
} }

View File

@ -24,13 +24,13 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment {
} }
@Override @Override
int getTotalForActivityAmounts(ActivityAmounts activityAmounts) { float[] getTotalsForActivityAmounts(ActivityAmounts activityAmounts) {
int totalSteps = 0; int totalSteps = 0;
for (ActivityAmount amount : activityAmounts.getAmounts()) { for (ActivityAmount amount : activityAmounts.getAmounts()) {
totalSteps += amount.getTotalSteps(); totalSteps += amount.getTotalSteps();
amount.getTotalSteps(); amount.getTotalSteps();
} }
return totalSteps; return new float[]{totalSteps};
} }
@Override @Override
@ -44,7 +44,7 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment {
} }
@Override @Override
Integer getMainColor() { int[] getColors() {
return akActivity.color; return new int[]{akActivity.color};
} }
} }