1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2025-01-25 17:17:32 +01:00

Heart rate fragment: line chart gap instead of drawing when no data for longer than max data gap

This commit is contained in:
a0z 2024-12-06 23:40:19 +01:00
parent c01607dc06
commit 1b78ebfad9

View File

@ -20,6 +20,7 @@ import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry; import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData; import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet; import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
@ -208,6 +209,19 @@ public class HeartRateDailyFragment extends AbstractChartFragment<HeartRateDaily
chart.getLegend().setWordWrapEnabled(true); chart.getLegend().setWordWrapEnabled(true);
} }
protected LineDataSet createHeartRateDataSet(final List<Entry> values) {
LineDataSet dataSet = new LineDataSet(values, "Heart Rate");
dataSet.setLineWidth(1.5f);
dataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
dataSet.setCubicIntensity(0.1f);
dataSet.setDrawCircles(false);
dataSet.setDrawValues(true);
dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
dataSet.setColor(HEARTRATE_COLOR);
dataSet.setValueTextColor(CHART_TEXT_COLOR);
return dataSet;
}
@Override @Override
protected void updateChartsnUIThread(HeartRateDailyFragment.HeartRateData data) { protected void updateChartsnUIThread(HeartRateDailyFragment.HeartRateData data) {
Calendar day = Calendar.getInstance(); Calendar day = Calendar.getInstance();
@ -229,33 +243,32 @@ public class HeartRateDailyFragment extends AbstractChartFragment<HeartRateDaily
List<? extends ActivitySample> samples = data.samples; List<? extends ActivitySample> samples = data.samples;
final Accumulator accumulator = new Accumulator(); final Accumulator accumulator = new Accumulator();
int lastHrSampleIndex = -1; final List<ILineDataSet> lineDataSets = new ArrayList<>();
int lastTsShorten = 0;
for (int i =0; i < samples.size(); i++) { for (int i =0; i < samples.size(); i++) {
final ActivitySample sample = samples.get(i); final ActivitySample sample = samples.get(i);
final int ts = tsTranslation.shorten(sample.getTimestamp()); final int tsShorten = tsTranslation.shorten(sample.getTimestamp());
if (!heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) { if (!heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
continue; continue;
} }
if (sample.getKind() != ActivityKind.NOT_WORN) { if (lastTsShorten == 0 || (tsShorten - lastTsShorten) <= 60 * HeartRateUtils.MAX_HR_MEASUREMENTS_GAP_MINUTES) {
if (lastHrSampleIndex > -1 && ts - lastHrSampleIndex > 1800 * HeartRateUtils.MAX_HR_MEASUREMENTS_GAP_MINUTES) { lineEntries.add(new Entry(tsShorten, sample.getHeartRate()));
lineEntries.add(new Entry(lastHrSampleIndex + 1, 0 )); } else {
lineEntries.add(new Entry(ts - 1, 0)); if (!lineEntries.isEmpty()) {
List<Entry> clone = new ArrayList<>(lineEntries.size());
clone.addAll(lineEntries);
lineDataSets.add(createHeartRateDataSet(clone));
lineEntries.clear();
} }
lineEntries.add(new Entry(ts, sample.getHeartRate()));
lastHrSampleIndex = ts;
} }
lastTsShorten = tsShorten;
lineEntries.add(new Entry(tsShorten, sample.getHeartRate()));
accumulator.add(sample.getHeartRate()); accumulator.add(sample.getHeartRate());
} }
LineDataSet dataSet = new LineDataSet(lineEntries, "Heart Rate"); if (!lineEntries.isEmpty()) {
dataSet.setLineWidth(1.5f); lineDataSets.add(createHeartRateDataSet(lineEntries));
dataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER); }
dataSet.setCubicIntensity(0.1f);
dataSet.setDrawCircles(false);
dataSet.setDrawValues(true);
dataSet.setAxisDependency(YAxis.AxisDependency.RIGHT);
dataSet.setColor(HEARTRATE_COLOR);
dataSet.setValueTextColor(CHART_TEXT_COLOR);
final int average = accumulator.getCount() > 0 ? (int) Math.round(accumulator.getAverage()) : -1; final int average = accumulator.getCount() > 0 ? (int) Math.round(accumulator.getAverage()) : -1;
final int minimum = accumulator.getCount() > 0 ? (int) Math.round(accumulator.getMin()) : -1; final int minimum = accumulator.getCount() > 0 ? (int) Math.round(accumulator.getMin()) : -1;
@ -276,7 +289,7 @@ public class HeartRateDailyFragment extends AbstractChartFragment<HeartRateDaily
} }
hrLineChart.getXAxis().setValueFormatter(new SampleXLabelFormatter(tsTranslation, "HH:mm")); hrLineChart.getXAxis().setValueFormatter(new SampleXLabelFormatter(tsTranslation, "HH:mm"));
hrLineChart.setData(new LineData(dataSet)); hrLineChart.setData(new LineData(lineDataSets));
hrLineChart.getAxisLeft().removeAllLimitLines(); hrLineChart.getAxisLeft().removeAllLimitLines();