1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-10-01 18:59:14 +02:00

Add legend ans labels to weekly sleep charts

Also remove the "missing sleep" gray entry to avoid confusion
This commit is contained in:
Andreas Shimokawa 2017-03-31 23:42:25 +02:00
parent 8fccbe3b69
commit 173b4fbbe6
3 changed files with 56 additions and 21 deletions

View File

@ -25,7 +25,6 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import com.github.mikephil.charting.charts.BarChart; import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.charts.PieChart; import com.github.mikephil.charting.charts.PieChart;
import com.github.mikephil.charting.components.LimitLine; import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis; import com.github.mikephil.charting.components.XAxis;
@ -81,13 +80,12 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
protected void updateChartsnUIThread(ChartsData chartsData) { protected void updateChartsnUIThread(ChartsData chartsData) {
MyChartsData mcd = (MyChartsData) chartsData; MyChartsData mcd = (MyChartsData) chartsData;
// setupLegend(mWeekChart); setupLegend(mWeekChart);
mTodayPieChart.setCenterText(mcd.getDayData().centerText); mTodayPieChart.setCenterText(mcd.getDayData().centerText);
mTodayPieChart.setData(mcd.getDayData().data); mTodayPieChart.setData(mcd.getDayData().data);
mWeekChart.setData(null); // workaround for https://github.com/PhilJay/MPAndroidChart/issues/2317 mWeekChart.setData(null); // workaround for https://github.com/PhilJay/MPAndroidChart/issues/2317
mWeekChart.setData(mcd.getWeekBeforeData().getData()); mWeekChart.setData(mcd.getWeekBeforeData().getData());
mWeekChart.getLegend().setEnabled(false);
mWeekChart.getXAxis().setValueFormatter(mcd.getWeekBeforeData().getXValueFormatter()); mWeekChart.getXAxis().setValueFormatter(mcd.getWeekBeforeData().getXValueFormatter());
} }
@ -133,23 +131,30 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
ActivityAmounts amounts = getActivityAmountsForDay(db, day, device); ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
float totalValues[] = getTotalsForActivityAmounts(amounts); float totalValues[] = getTotalsForActivityAmounts(amounts);
String[] pieLabels = getPieLabels();
float totalValue = 0; float totalValue = 0;
for (float value : totalValues) { for (int i = 0; i < totalValues.length; i++) {
float value = totalValues[i];
totalValue += value; totalValue += value;
entries.add(new PieEntry(value)); entries.add(new PieEntry(value, pieLabels[i]));
} }
set.setValueFormatter(getPieValueFormatter()); set.setValueFormatter(getPieValueFormatter());
set.setColors(getColors()); set.setColors(getColors());
if (totalValue < mTargetValue) { //this hides the values (numeric) added to the set. These would be shown aside the strings set with addXValue above
entries.add(new PieEntry((mTargetValue - totalValue))); if (totalValues.length < 2) {
set.addColor(Color.GRAY); 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
data.setDrawValues(false); if (totalValues.length < 2) {
data.setDrawValues(false);
}
return new DayData(data, formatPieValue((int) totalValue)); return new DayData(data, formatPieValue((int) totalValue));
} }
@ -185,7 +190,6 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
// mTodayPieChart.setNoDataTextDescription(""); // mTodayPieChart.setNoDataTextDescription("");
mTodayPieChart.setNoDataText(""); mTodayPieChart.setNoDataText("");
mTodayPieChart.getLegend().setEnabled(false); mTodayPieChart.getLegend().setEnabled(false);
// setupLegend(mTodayPieChart);
} }
private void setupWeekChart() { private void setupWeekChart() {
@ -222,16 +226,6 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
yAxisRight.setTextColor(CHART_TEXT_COLOR); yAxisRight.setTextColor(CHART_TEXT_COLOR);
} }
@Override
protected void setupLegend(Chart chart) {
// List<Integer> legendColors = new ArrayList<>(1);
// List<String> legendLabels = new ArrayList<>(1);
// legendColors.add(akActivity.color);
// legendLabels.add(getContext().getString(R.string.chart_steps));
// chart.getLegend().setCustom(legendColors, legendLabels);
// chart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
}
private List<? extends ActivitySample> getSamplesOfDay(DBHandler db, Calendar day, int offsetHours, GBDevice device) { private List<? extends ActivitySample> getSamplesOfDay(DBHandler db, Calendar day, int offsetHours, GBDevice device) {
int startTs; int startTs;
int endTs; int endTs;
@ -312,6 +306,8 @@ public abstract class AbstractWeekChartFragment extends AbstractChartFragment {
abstract String formatPieValue(int value); abstract String formatPieValue(int value);
abstract String[] getPieLabels();
abstract IValueFormatter getPieValueFormatter(); abstract IValueFormatter getPieValueFormatter();
abstract IValueFormatter getBarValueFormatter(); abstract IValueFormatter getBarValueFormatter();

View File

@ -16,12 +16,16 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts; package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.components.AxisBase; import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.LegendEntry;
import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter; import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter; import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.utils.ViewPortHandler; import com.github.mikephil.charting.utils.ViewPortHandler;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
@ -72,6 +76,11 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
return DateTimeUtils.formatDurationHoursMinutes((long) value, TimeUnit.MINUTES); return DateTimeUtils.formatDurationHoursMinutes((long) value, TimeUnit.MINUTES);
} }
@Override
String[] getPieLabels() {
return new String[]{getString(R.string.abstract_chart_fragment_kind_deep_sleep), getString(R.string.abstract_chart_fragment_kind_light_sleep)};
}
@Override @Override
IValueFormatter getPieValueFormatter() { IValueFormatter getPieValueFormatter() {
return new IValueFormatter() { return new IValueFormatter() {
@ -106,4 +115,22 @@ public class WeekSleepChartFragment extends AbstractWeekChartFragment {
int[] getColors() { int[] getColors() {
return new int[]{akDeepSleep.color, akLightSleep.color}; return new int[]{akDeepSleep.color, akLightSleep.color};
} }
@Override
protected void setupLegend(Chart chart) {
List<LegendEntry> legendEntries = new ArrayList<>(2);
LegendEntry lightSleepEntry = new LegendEntry();
lightSleepEntry.label = akLightSleep.label;
lightSleepEntry.formColor = akLightSleep.color;
legendEntries.add(lightSleepEntry);
LegendEntry deepSleepEntry = new LegendEntry();
deepSleepEntry.label = akDeepSleep.label;
deepSleepEntry.formColor = akDeepSleep.color;
legendEntries.add(deepSleepEntry);
chart.getLegend().setCustom(legendEntries);
chart.getLegend().setTextColor(LEGEND_TEXT_COLOR);
}
} }

View File

@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */ along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.charts; package nodomain.freeyourgadget.gadgetbridge.activities.charts;
import com.github.mikephil.charting.charts.Chart;
import com.github.mikephil.charting.formatter.IAxisValueFormatter; import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter; import com.github.mikephil.charting.formatter.IValueFormatter;
@ -62,6 +63,11 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment {
return String.valueOf(value); return String.valueOf(value);
} }
@Override
String[] getPieLabels() {
return new String[]{""};
}
@Override @Override
IValueFormatter getPieValueFormatter() { IValueFormatter getPieValueFormatter() {
return null; return null;
@ -81,4 +87,10 @@ public class WeekStepsChartFragment extends AbstractWeekChartFragment {
int[] getColors() { int[] getColors() {
return new int[]{akActivity.color}; return new int[]{akActivity.color};
} }
@Override
protected void setupLegend(Chart chart) {
// no legend here, it is all about the steps here
chart.getLegend().setEnabled(false);
}
} }