Search in sources :

Example 1 with ValueFormatter

use of com.github.mikephil.charting.formatter.ValueFormatter in project Gadgetbridge by Freeyourgadget.

the class AbstractChartFragment method refresh.

public DefaultChartsData<LineData> refresh(GBDevice gbDevice, List<? extends ActivitySample> samples) {
    // Calendar cal = GregorianCalendar.getInstance();
    // cal.clear();
    TimestampTranslation tsTranslation = new TimestampTranslation();
    // Date date;
    // String dateStringFrom = "";
    // String dateStringTo = "";
    // ArrayList<String> xLabels = null;
    LOG.info("" + getTitle() + ": number of samples:" + samples.size());
    LineData lineData;
    if (samples.size() > 1) {
        boolean annotate = true;
        boolean use_steps_as_movement;
        int last_type = ActivityKind.TYPE_UNKNOWN;
        int numEntries = samples.size();
        List<Entry> activityEntries = new ArrayList<>(numEntries);
        List<Entry> deepSleepEntries = new ArrayList<>(numEntries);
        List<Entry> lightSleepEntries = new ArrayList<>(numEntries);
        List<Entry> notWornEntries = new ArrayList<>(numEntries);
        boolean hr = supportsHeartrate(gbDevice);
        List<Entry> heartrateEntries = hr ? new ArrayList<Entry>(numEntries) : null;
        // this is kinda inefficient...
        List<Integer> colors = new ArrayList<>(numEntries);
        int lastHrSampleIndex = -1;
        HeartRateUtils heartRateUtilsInstance = HeartRateUtils.getInstance();
        for (int i = 0; i < numEntries; i++) {
            ActivitySample sample = samples.get(i);
            int type = sample.getKind();
            int ts = tsTranslation.shorten(sample.getTimestamp());
            // System.out.println(ts);
            // ts = i;
            // determine start and end dates
            // if (i == 0) {
            // cal.setTimeInMillis(ts * 1000L); // make sure it's converted to long
            // date = cal.getTime();
            // dateStringFrom = dateFormat.format(date);
            // } else if (i == samples.size() - 1) {
            // cal.setTimeInMillis(ts * 1000L); // same here
            // date = cal.getTime();
            // dateStringTo = dateFormat.format(date);
            // }
            float movement = sample.getIntensity();
            float value = movement;
            switch(type) {
                case ActivityKind.TYPE_DEEP_SLEEP:
                    if (last_type != type) {
                        // FIXME: this is ugly but it works (repeated in each case)
                        deepSleepEntries.add(createLineEntry(0, ts - 1));
                        lightSleepEntries.add(createLineEntry(0, ts));
                        notWornEntries.add(createLineEntry(0, ts));
                        activityEntries.add(createLineEntry(0, ts));
                    }
                    deepSleepEntries.add(createLineEntry(value + SleepUtils.Y_VALUE_DEEP_SLEEP, ts));
                    break;
                case ActivityKind.TYPE_LIGHT_SLEEP:
                    if (last_type != type) {
                        lightSleepEntries.add(createLineEntry(0, ts - 1));
                        deepSleepEntries.add(createLineEntry(0, ts));
                        notWornEntries.add(createLineEntry(0, ts));
                        activityEntries.add(createLineEntry(0, ts));
                    }
                    lightSleepEntries.add(createLineEntry(value, ts));
                    break;
                case ActivityKind.TYPE_NOT_WORN:
                    if (last_type != type) {
                        notWornEntries.add(createLineEntry(0, ts - 1));
                        lightSleepEntries.add(createLineEntry(0, ts));
                        deepSleepEntries.add(createLineEntry(0, ts));
                        activityEntries.add(createLineEntry(0, ts));
                    }
                    // a small value, just to show something on the graphs
                    notWornEntries.add(createLineEntry(SleepUtils.Y_VALUE_DEEP_SLEEP, ts));
                    break;
                default:
                    // value = ((float) movement) / movement_divisor;
                    if (last_type != type) {
                        activityEntries.add(createLineEntry(0, ts - 1));
                        lightSleepEntries.add(createLineEntry(0, ts));
                        notWornEntries.add(createLineEntry(0, ts));
                        deepSleepEntries.add(createLineEntry(0, ts));
                    }
                    activityEntries.add(createLineEntry(value, ts));
            }
            if (hr && sample.getKind() != ActivityKind.TYPE_NOT_WORN && heartRateUtilsInstance.isValidHeartRateValue(sample.getHeartRate())) {
                if (lastHrSampleIndex > -1 && ts - lastHrSampleIndex > 1800 * HeartRateUtils.MAX_HR_MEASUREMENTS_GAP_MINUTES) {
                    heartrateEntries.add(createLineEntry(0, lastHrSampleIndex + 1));
                    heartrateEntries.add(createLineEntry(0, ts - 1));
                }
                heartrateEntries.add(createLineEntry(sample.getHeartRate(), ts));
                lastHrSampleIndex = ts;
            }
            String xLabel = "";
            if (annotate) {
            // cal.setTimeInMillis((ts + tsOffset) * 1000L);
            // date = cal.getTime();
            // String dateString = annotationDateFormat.format(date);
            // xLabel = dateString;
            // if (last_type != type) {
            // if (isSleep(last_type) && !isSleep(type)) {
            // // woken up
            // LimitLine line = new LimitLine(i, dateString);
            // line.enableDashedLine(8, 8, 0);
            // line.setTextColor(Color.WHITE);
            // line.setTextSize(15);
            // chart.getXAxis().addLimitLine(line);
            // } else if (!isSleep(last_type) && isSleep(type)) {
            // // fallen asleep
            // LimitLine line = new LimitLine(i, dateString);
            // line.enableDashedLine(8, 8, 0);
            // line.setTextSize(15);
            // line.setTextColor(Color.WHITE);
            // chart.getXAxis().addLimitLine(line);
            // }
            // }
            }
            last_type = type;
        }
        List<ILineDataSet> lineDataSets = new ArrayList<>();
        LineDataSet activitySet = createDataSet(activityEntries, akActivity.color, "Activity");
        lineDataSets.add(activitySet);
        LineDataSet deepSleepSet = createDataSet(deepSleepEntries, akDeepSleep.color, "Deep Sleep");
        lineDataSets.add(deepSleepSet);
        LineDataSet lightSleepSet = createDataSet(lightSleepEntries, akLightSleep.color, "Light Sleep");
        lineDataSets.add(lightSleepSet);
        LineDataSet notWornSet = createDataSet(notWornEntries, akNotWorn.color, "Not worn");
        lineDataSets.add(notWornSet);
        if (hr && heartrateEntries.size() > 0) {
            LineDataSet heartrateSet = createHeartrateSet(heartrateEntries, "Heart Rate");
            lineDataSets.add(heartrateSet);
        }
        lineData = new LineData(lineDataSets);
    // chart.setDescription(getString(R.string.sleep_activity_date_range, dateStringFrom, dateStringTo));
    // chart.setDescriptionPosition(?, ?);
    } else {
        lineData = new LineData();
    }
    ValueFormatter xValueFormatter = new SampleXLabelFormatter(tsTranslation);
    return new DefaultChartsData(lineData, xValueFormatter);
}
Also used : AbstractActivitySample(nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample) ActivitySample(nodomain.freeyourgadget.gadgetbridge.model.ActivitySample) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) LineDataSet(com.github.mikephil.charting.data.LineDataSet) HeartRateUtils(nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils) ArrayList(java.util.ArrayList) LineData(com.github.mikephil.charting.data.LineData) Entry(com.github.mikephil.charting.data.Entry) ILineDataSet(com.github.mikephil.charting.interfaces.datasets.ILineDataSet) ValueFormatter(com.github.mikephil.charting.formatter.ValueFormatter)

Example 2 with ValueFormatter

use of com.github.mikephil.charting.formatter.ValueFormatter in project Gadgetbridge by Freeyourgadget.

the class SleepChartFragment method refreshSleepAmounts.

private MySleepChartsData refreshSleepAmounts(GBDevice mGBDevice, List<? extends ActivitySample> samples) {
    SleepAnalysis sleepAnalysis = new SleepAnalysis();
    List<SleepSession> sleepSessions = sleepAnalysis.calculateSleepSessions(samples);
    PieData data = new PieData();
    final long lightSleepDuration = calculateLightSleepDuration(sleepSessions);
    final long deepSleepDuration = calculateDeepSleepDuration(sleepSessions);
    final long totalSeconds = lightSleepDuration + deepSleepDuration;
    final List<PieEntry> entries;
    final List<Integer> colors;
    if (sleepSessions.isEmpty()) {
        entries = Collections.emptyList();
        colors = Collections.emptyList();
    } else {
        entries = Arrays.asList(new PieEntry(lightSleepDuration, getActivity().getString(R.string.abstract_chart_fragment_kind_light_sleep)), new PieEntry(deepSleepDuration, getActivity().getString(R.string.abstract_chart_fragment_kind_deep_sleep)));
        colors = Arrays.asList(getColorFor(ActivityKind.TYPE_LIGHT_SLEEP), getColorFor(ActivityKind.TYPE_DEEP_SLEEP));
    }
    String totalSleep = DateTimeUtils.formatDurationHoursMinutes(totalSeconds, TimeUnit.SECONDS);
    PieDataSet set = new PieDataSet(entries, "");
    set.setValueFormatter(new ValueFormatter() {

        @Override
        public String getFormattedValue(float value) {
            return DateTimeUtils.formatDurationHoursMinutes((long) value, TimeUnit.SECONDS);
        }
    });
    set.setColors(colors);
    set.setValueTextColor(DESCRIPTION_COLOR);
    set.setValueTextSize(13f);
    set.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
    set.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
    data.setDataSet(set);
    // setupLegend(pieChart);
    return new MySleepChartsData(totalSleep, data, sleepSessions);
}
Also used : PieEntry(com.github.mikephil.charting.data.PieEntry) SleepSession(nodomain.freeyourgadget.gadgetbridge.activities.charts.SleepAnalysis.SleepSession) PieDataSet(com.github.mikephil.charting.data.PieDataSet) PieData(com.github.mikephil.charting.data.PieData) ValueFormatter(com.github.mikephil.charting.formatter.ValueFormatter)

Aggregations

ValueFormatter (com.github.mikephil.charting.formatter.ValueFormatter)2 Entry (com.github.mikephil.charting.data.Entry)1 LineData (com.github.mikephil.charting.data.LineData)1 LineDataSet (com.github.mikephil.charting.data.LineDataSet)1 PieData (com.github.mikephil.charting.data.PieData)1 PieDataSet (com.github.mikephil.charting.data.PieDataSet)1 PieEntry (com.github.mikephil.charting.data.PieEntry)1 ILineDataSet (com.github.mikephil.charting.interfaces.datasets.ILineDataSet)1 ArrayList (java.util.ArrayList)1 HeartRateUtils (nodomain.freeyourgadget.gadgetbridge.activities.HeartRateUtils)1 SleepSession (nodomain.freeyourgadget.gadgetbridge.activities.charts.SleepAnalysis.SleepSession)1 AbstractActivitySample (nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample)1 ActivitySample (nodomain.freeyourgadget.gadgetbridge.model.ActivitySample)1