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

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

View File

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

View File

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