Search in sources :

Example 26 with PieData

use of com.github.mikephil.charting.data.PieData 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)

Example 27 with PieData

use of com.github.mikephil.charting.data.PieData in project Gadgetbridge by Freeyourgadget.

the class AbstractWeekChartFragment method refreshDayPie.

private DayData refreshDayPie(DBHandler db, Calendar day, GBDevice device) {
    PieData data = new PieData();
    List<PieEntry> entries = new ArrayList<>();
    PieDataSet set = new PieDataSet(entries, "");
    ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
    float[] totalValues = getTotalsForActivityAmounts(amounts);
    String[] pieLabels = getPieLabels();
    float totalValue = 0;
    for (int i = 0; i < totalValues.length; i++) {
        float value = totalValues[i];
        totalValue += value;
        entries.add(new PieEntry(value, pieLabels[i]));
    }
    set.setColors(getColors());
    if (totalValues.length < 2) {
        if (totalValue < mTargetValue) {
            entries.add(new PieEntry((mTargetValue - totalValue)));
            set.addColor(Color.GRAY);
        }
    }
    data.setDataSet(set);
    if (totalValues.length < 2) {
        data.setDrawValues(false);
    } else {
        set.setXValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        set.setYValuePosition(PieDataSet.ValuePosition.OUTSIDE_SLICE);
        set.setValueTextColor(DESCRIPTION_COLOR);
        set.setValueTextSize(13f);
        set.setValueFormatter(getPieValueFormatter());
    }
    return new DayData(data, formatPieValue((long) totalValue));
}
Also used : PieEntry(com.github.mikephil.charting.data.PieEntry) PieDataSet(com.github.mikephil.charting.data.PieDataSet) ActivityAmounts(nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts) ArrayList(java.util.ArrayList) PieData(com.github.mikephil.charting.data.PieData)

Example 28 with PieData

use of com.github.mikephil.charting.data.PieData in project Gadgetbridge by Freeyourgadget.

the class GBDeviceAdapterv2 method setChartsData.

private void setChartsData(PieChart pieChart, float value, float target, String label, String stringValue, Context context) {
    final String CHART_COLOR_START = "#e74c3c";
    final String CHART_COLOR_END = "#2ecc71";
    ArrayList<PieEntry> entries = new ArrayList<>();
    entries.add(new PieEntry((float) value, context.getResources().getDrawable(R.drawable.ic_star_gold)));
    if (value < target) {
        entries.add(new PieEntry((float) (target - value)));
    }
    pieChart.setCenterText(String.format("%s\n%s", stringValue, label));
    float colorValue = Math.max(0, Math.min(1, value / target));
    int chartColor = interpolateColor(Color.parseColor(CHART_COLOR_START), Color.parseColor(CHART_COLOR_END), colorValue);
    PieDataSet dataSet = new PieDataSet(entries, "");
    dataSet.setDrawIcons(false);
    dataSet.setIconsOffset(new MPPointF(0, -66));
    if (colorValue == 1) {
        dataSet.setDrawIcons(true);
    }
    dataSet.setSliceSpace(0f);
    dataSet.setSelectionShift(5f);
    dataSet.setColors(chartColor, Color.LTGRAY);
    PieData data = new PieData(dataSet);
    data.setValueTextSize(0f);
    data.setValueTextColor(Color.WHITE);
    pieChart.setData(data);
    pieChart.invalidate();
}
Also used : PieEntry(com.github.mikephil.charting.data.PieEntry) PieDataSet(com.github.mikephil.charting.data.PieDataSet) MPPointF(com.github.mikephil.charting.utils.MPPointF) ArrayList(java.util.ArrayList) PieData(com.github.mikephil.charting.data.PieData)

Aggregations

PieData (com.github.mikephil.charting.data.PieData)28 PieDataSet (com.github.mikephil.charting.data.PieDataSet)24 PieEntry (com.github.mikephil.charting.data.PieEntry)20 ArrayList (java.util.ArrayList)20 PercentFormatter (com.github.mikephil.charting.formatter.PercentFormatter)7 MPPointF (com.github.mikephil.charting.utils.MPPointF)5 Entry (com.github.mikephil.charting.data.Entry)4 View (android.view.View)3 Intent (android.content.Intent)2 Paint (android.graphics.Paint)2 SpannableString (android.text.SpannableString)2 TextPaint (android.text.TextPaint)2 DisplayMetrics (android.util.DisplayMetrics)2 TextView (android.widget.TextView)2 PieChart (com.github.mikephil.charting.charts.PieChart)2 IPieDataSet (com.github.mikephil.charting.interfaces.datasets.IPieDataSet)2 Context (android.content.Context)1 SharedPreferences (android.content.SharedPreferences)1 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1