Search in sources :

Example 41 with BarDataSet

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

the class AbstractWeekChartFragment method refreshWeekBeforeData.

private WeekChartsData<BarData> refreshWeekBeforeData(DBHandler db, BarChart barChart, Calendar day, GBDevice device) {
    // do not modify the caller's argument
    day = (Calendar) day.clone();
    day.add(Calendar.DATE, -TOTAL_DAYS);
    List<BarEntry> entries = new ArrayList<>();
    ArrayList<String> labels = new ArrayList<String>();
    long balance = 0;
    long daily_balance = 0;
    TOTAL_DAYS_FOR_AVERAGE = 0;
    for (int counter = 0; counter < TOTAL_DAYS; counter++) {
        ActivityAmounts amounts = getActivityAmountsForDay(db, day, device);
        daily_balance = calculateBalance(amounts);
        if (daily_balance > 0) {
            TOTAL_DAYS_FOR_AVERAGE++;
        }
        balance += daily_balance;
        entries.add(new BarEntry(counter, getTotalsForActivityAmounts(amounts)));
        labels.add(getWeeksChartsLabel(day));
        day.add(Calendar.DATE, 1);
    }
    BarDataSet set = new BarDataSet(entries, "");
    set.setColors(getColors());
    set.setValueFormatter(getBarValueFormatter());
    BarData barData = new BarData(set);
    // prevent tearing other graph elements with the black text. Another approach would be to hide the values cmpletely with data.setDrawValues(false);
    barData.setValueTextColor(Color.GRAY);
    barData.setValueTextSize(10f);
    LimitLine target = new LimitLine(mTargetValue);
    barChart.getAxisLeft().removeAllLimitLines();
    barChart.getAxisLeft().addLimitLine(target);
    float average = 0;
    if (TOTAL_DAYS_FOR_AVERAGE > 0) {
        average = Math.abs(balance / TOTAL_DAYS_FOR_AVERAGE);
    }
    LimitLine average_line = new LimitLine(average);
    average_line.setLabel(getString(R.string.average, getAverage(average)));
    if (average > (mTargetValue)) {
        average_line.setLineColor(Color.GREEN);
        average_line.setTextColor(Color.GREEN);
    } else {
        average_line.setLineColor(Color.RED);
        average_line.setTextColor(Color.RED);
    }
    if (average > 0) {
        if (GBApplication.getPrefs().getBoolean("charts_show_average", true)) {
            barChart.getAxisLeft().addLimitLine(average_line);
        }
    }
    return new WeekChartsData(barData, new PreformattedXIndexLabelFormatter(labels), getBalanceMessage(balance, mTargetValue));
}
Also used : BarDataSet(com.github.mikephil.charting.data.BarDataSet) ArrayList(java.util.ArrayList) LimitLine(com.github.mikephil.charting.components.LimitLine) BarEntry(com.github.mikephil.charting.data.BarEntry) ActivityAmounts(nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts) BarData(com.github.mikephil.charting.data.BarData)

Aggregations

BarDataSet (com.github.mikephil.charting.data.BarDataSet)41 ArrayList (java.util.ArrayList)27 BarEntry (com.github.mikephil.charting.data.BarEntry)25 BarData (com.github.mikephil.charting.data.BarData)24 IBarDataSet (com.github.mikephil.charting.interfaces.datasets.IBarDataSet)22 Intent (android.content.Intent)8 Paint (android.graphics.Paint)5 List (java.util.List)4 SuppressLint (android.annotation.SuppressLint)2 XAxis (com.github.mikephil.charting.components.XAxis)2 IAxisValueFormatter (com.github.mikephil.charting.formatter.IAxisValueFormatter)2 IDataSet (com.github.mikephil.charting.interfaces.datasets.IDataSet)2 Context (android.content.Context)1 Path (android.graphics.Path)1 PointF (android.graphics.PointF)1 RectF (android.graphics.RectF)1 BarChart (com.github.mikephil.charting.charts.BarChart)1 AxisBase (com.github.mikephil.charting.components.AxisBase)1 Legend (com.github.mikephil.charting.components.Legend)1 LimitLine (com.github.mikephil.charting.components.LimitLine)1