Search in sources :

Example 1 with FloatMeasurementView

use of com.health.openscale.gui.views.FloatMeasurementView in project openScale by oliexdev.

the class GraphFragment method generateLineData.

private void generateLineData(int field, List<ScaleMeasurement> scaleMeasurementList) {
    SimpleDateFormat day_date = new SimpleDateFormat("D", Locale.getDefault());
    if (field == Calendar.DAY_OF_MONTH) {
        day_date = new SimpleDateFormat("dd", Locale.getDefault());
        if (prefs.getBoolean("showWeek", false)) {
            field = Calendar.WEEK_OF_MONTH;
            day_date = new SimpleDateFormat("w", Locale.getDefault());
        }
    } else if (field == Calendar.DAY_OF_YEAR) {
        day_date = new SimpleDateFormat("D", Locale.getDefault());
        if (prefs.getBoolean("averageData", true)) {
            field = Calendar.MONTH;
            day_date = new SimpleDateFormat("MMM", Locale.getDefault());
        }
        if (prefs.getBoolean("showWeek", false)) {
            field = Calendar.WEEK_OF_YEAR;
            day_date = new SimpleDateFormat("w", Locale.getDefault());
        }
    }
    Calendar calDays = (Calendar) calLastSelected.clone();
    calDays.setMinimalDaysInFirstWeek(7);
    calDays.set(field, calDays.getMinimum(field));
    int maxDays = calDays.getMaximum(field);
    List<AxisValue> axisValues = new ArrayList<>();
    for (int i = 0; i < calDays.getMaximum(field) + 1; i++) {
        String day_name = day_date.format(calDays.getTime());
        AxisValue xAxisValue = new AxisValue(i + calDays.getActualMinimum(field));
        xAxisValue.setLabel(day_name);
        axisValues.add(xAxisValue);
        calDays.add(field, 1);
    }
    List<Line> diagramLineList = new ArrayList<Line>();
    Calendar calDB = Calendar.getInstance();
    pointIndexScaleMeasurementList = new ArrayList<>();
    floatingActionBar.removeAllViews();
    PolynomialFitter polyFitter = new PolynomialFitter(Integer.parseInt(prefs.getString("regressionLineOrder", "1")));
    for (MeasurementView view : measurementViews) {
        if (view instanceof FloatMeasurementView) {
            FloatMeasurementView measurementView = (FloatMeasurementView) view;
            if (measurementView instanceof BMRMeasurementView) {
                continue;
            }
            Stack<PointValue> valuesStack = new Stack<>();
            ArrayList<Float>[] avgBins = new ArrayList[maxDays + 1];
            ScaleMeasurement[] indexScaleMeasurement = new ScaleMeasurement[maxDays + 1];
            for (ScaleMeasurement measurement : scaleMeasurementList) {
                measurementView.loadFrom(measurement, null);
                calDB.setTime(measurement.getDateTime());
                calDB.setMinimalDaysInFirstWeek(7);
                if (avgBins[calDB.get(field)] == null) {
                    avgBins[calDB.get(field)] = new ArrayList<>();
                }
                if (measurementView.getValue() != 0.0f) {
                    avgBins[calDB.get(field)].add(measurementView.getValue());
                    indexScaleMeasurement[calDB.get(field)] = measurement;
                }
            }
            for (int i = 0; i < maxDays + 1; i++) {
                ArrayList avgBin = avgBins[i];
                if (avgBin == null) {
                    continue;
                }
                float sum = 0.0f;
                for (int j = 0; j < avgBin.size(); j++) {
                    sum += (float) avgBin.get(j);
                }
                PointValue avgValue = new PointValue(i, sum / avgBin.size());
                if (prefs.getBoolean("regressionLine", false) && measurementView instanceof WeightMeasurementView) {
                    polyFitter.addPoint((double) avgValue.getX(), (double) avgValue.getY());
                }
                if (avgBin.size() > 1) {
                    avgValue.setLabel(String.format("Ø %.2f", avgValue.getY()));
                }
                valuesStack.push(avgValue);
                pointIndexScaleMeasurementList.add(indexScaleMeasurement[i]);
            }
            Line diagramLine = new Line(valuesStack).setColor(measurementView.getColor()).setHasLabels(prefs.getBoolean("labelsEnable", true)).setHasPoints(prefs.getBoolean("pointsEnable", true)).setFormatter(new SimpleLineChartValueFormatter(1));
            if (measurementView.isVisible()) {
                addFloatingActionButton(measurementView);
                if (measurementView.getSettings().isInGraph()) {
                    diagramLineList.add(diagramLine);
                }
            }
        }
    }
    LineChartData lineData = new LineChartData(diagramLineList);
    lineData.setAxisXBottom(new Axis(axisValues).setHasLines(true).setTextColor(textColor));
    lineData.setAxisYLeft(new Axis().setHasLines(true).setMaxLabelChars(5).setTextColor(textColor));
    chartBottom.setLineChartData(lineData);
    defaultTopViewport = new Viewport(calDays.getActualMinimum(field), chartBottom.getCurrentViewport().top, calDays.getMaximum(field) + 1, chartBottom.getCurrentViewport().bottom);
    if (prefs.getBoolean("goalLine", true)) {
        Stack<PointValue> valuesGoalLine = new Stack<PointValue>();
        float goalWeight = openScale.getSelectedScaleUser().getGoalWeight();
        valuesGoalLine.push(new PointValue(0, goalWeight));
        valuesGoalLine.push(new PointValue(maxDays, goalWeight));
        Line goalLine = new Line(valuesGoalLine).setHasPoints(false);
        goalLine.setPathEffect(new DashPathEffect(new float[] { 10, 30 }, 0));
        diagramLineList.add(goalLine);
    }
    if (prefs.getBoolean("regressionLine", false)) {
        PolynomialFitter.Polynomial polynom = polyFitter.getBestFit();
        Stack<PointValue> valuesLinearRegression = new Stack<>();
        for (int i = 0; i < maxDays; i++) {
            double y_value = polynom.getY(i);
            valuesLinearRegression.push(new PointValue((float) i, (float) y_value));
        }
        Line linearRegressionLine = new Line(valuesLinearRegression).setColor(ChartUtils.COLOR_VIOLET).setHasPoints(false).setCubic(true);
        linearRegressionLine.setPathEffect(new DashPathEffect(new float[] { 10, 30 }, 0));
        diagramLineList.add(linearRegressionLine);
    }
    chartBottom.setLineChartData(lineData);
    chartBottom.setCurrentViewport(defaultTopViewport);
}
Also used : FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView) MeasurementView(com.health.openscale.gui.views.MeasurementView) BMRMeasurementView(com.health.openscale.gui.views.BMRMeasurementView) WeightMeasurementView(com.health.openscale.gui.views.WeightMeasurementView) ArrayList(java.util.ArrayList) BMRMeasurementView(com.health.openscale.gui.views.BMRMeasurementView) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) LineChartData(lecho.lib.hellocharts.model.LineChartData) Axis(lecho.lib.hellocharts.model.Axis) FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView) PointValue(lecho.lib.hellocharts.model.PointValue) Calendar(java.util.Calendar) Viewport(lecho.lib.hellocharts.model.Viewport) DashPathEffect(android.graphics.DashPathEffect) WeightMeasurementView(com.health.openscale.gui.views.WeightMeasurementView) Stack(java.util.Stack) SimpleLineChartValueFormatter(lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter) Line(lecho.lib.hellocharts.model.Line) AxisValue(lecho.lib.hellocharts.model.AxisValue) PolynomialFitter(com.health.openscale.core.utils.PolynomialFitter) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with FloatMeasurementView

use of com.health.openscale.gui.views.FloatMeasurementView in project openScale by oliexdev.

the class OverviewFragment method updateLastLineChart.

private void updateLastLineChart(List<ScaleMeasurement> scaleMeasurementList) {
    final Calendar now = Calendar.getInstance();
    Calendar histCalendar = Calendar.getInstance();
    scaleMeasurementLastDays = new ArrayList<>();
    List<AxisValue> axisValues = new ArrayList<>();
    int max_i = Math.min(7, scaleMeasurementList.size());
    for (int i = 0; i < max_i; ++i) {
        ScaleMeasurement measurement = scaleMeasurementList.get(max_i - i - 1);
        scaleMeasurementLastDays.add(measurement);
        histCalendar.setTime(measurement.getDateTime());
        int days = DateTimeHelpers.daysBetween(now, histCalendar);
        String label = getResources().getQuantityString(R.plurals.label_days, Math.abs(days), days);
        axisValues.add(new AxisValue(i, label.toCharArray()));
    }
    List<Line> diagramLineList = new ArrayList<>();
    for (MeasurementView view : measurementViews) {
        if (!view.isVisible() || !view.getSettings().isInOverviewGraph() || !(view instanceof FloatMeasurementView)) {
            continue;
        }
        FloatMeasurementView measurementView = (FloatMeasurementView) view;
        Stack<PointValue> valuesStack = new Stack<PointValue>();
        for (int i = 0; i < max_i; ++i) {
            ScaleMeasurement measurement = scaleMeasurementList.get(max_i - i - 1);
            measurementView.loadFrom(measurement, null);
            if (measurementView.getValue() != 0.0f) {
                valuesStack.push(new PointValue(i, measurementView.getValue()));
            }
        }
        diagramLineList.add(new Line(valuesStack).setColor(measurementView.getColor()).setHasLabels(prefs.getBoolean("labelsEnable", true)).setHasPoints(prefs.getBoolean("pointsEnable", true)).setFormatter(new SimpleLineChartValueFormatter(1)));
    }
    LineChartData lineData = new LineChartData(diagramLineList);
    lineData.setAxisXBottom(new Axis(axisValues).setHasLines(true).setTextColor(txtTitleLastMeasurement.getCurrentTextColor()));
    lineData.setAxisYLeft(new Axis().setHasLines(true).setMaxLabelChars(5).setTextColor(txtTitleLastMeasurement.getCurrentTextColor()));
    lineChartLast.setLineChartData(lineData);
    lineChartLast.setViewportCalculationEnabled(true);
    lineChartLast.setZoomEnabled(false);
}
Also used : BMRMeasurementView(com.health.openscale.gui.views.BMRMeasurementView) FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView) MeasurementView(com.health.openscale.gui.views.MeasurementView) PointValue(lecho.lib.hellocharts.model.PointValue) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Stack(java.util.Stack) SimpleLineChartValueFormatter(lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter) Line(lecho.lib.hellocharts.model.Line) AxisValue(lecho.lib.hellocharts.model.AxisValue) ScaleMeasurement(com.health.openscale.core.datatypes.ScaleMeasurement) LineChartData(lecho.lib.hellocharts.model.LineChartData) Axis(lecho.lib.hellocharts.model.Axis) FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView)

Example 3 with FloatMeasurementView

use of com.health.openscale.gui.views.FloatMeasurementView in project openScale by oliexdev.

the class OverviewFragment method updateLastPieChart.

private void updateLastPieChart() {
    List<SliceValue> arcValuesLast = new ArrayList<>();
    for (MeasurementView view : measurementViews) {
        if (view instanceof FloatMeasurementView) {
            FloatMeasurementView measurementView = (FloatMeasurementView) view;
            if (measurementView instanceof BMRMeasurementView) {
                continue;
            }
            measurementView.loadFrom(lastScaleMeasurement, null);
            if (measurementView.getValue() != 0) {
                arcValuesLast.add(new SliceValue(measurementView.getValue(), measurementView.getColor()));
            }
        }
    }
    final Converters.WeightUnit unit = currentScaleUser.getScaleUnit();
    PieChartData pieChartData = new PieChartData(arcValuesLast);
    pieChartData.setHasLabels(false);
    pieChartData.setHasCenterCircle(true);
    pieChartData.setCenterText1(String.format("%.2f %s", lastScaleMeasurement.getConvertedWeight(unit), unit.toString()));
    pieChartData.setCenterText2(DateFormat.getDateInstance(DateFormat.MEDIUM).format(lastScaleMeasurement.getDateTime()));
    pieChartData.setCenterText1Color(txtTitleLastMeasurement.getCurrentTextColor());
    pieChartData.setCenterText2Color(txtTitleLastMeasurement.getCurrentTextColor());
    if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE || (getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_LARGE) {
        pieChartData.setCenterText1FontSize(20);
        pieChartData.setCenterText2FontSize(14);
    } else {
        pieChartData.setCenterText1FontSize(15);
        pieChartData.setCenterText2FontSize(12);
        pieChartData.setValueLabelTextSize(12);
    }
    pieChartLast.setPieChartData(pieChartData);
}
Also used : BMRMeasurementView(com.health.openscale.gui.views.BMRMeasurementView) FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView) MeasurementView(com.health.openscale.gui.views.MeasurementView) BMRMeasurementView(com.health.openscale.gui.views.BMRMeasurementView) ArrayList(java.util.ArrayList) Converters(com.health.openscale.core.utils.Converters) PieChartData(lecho.lib.hellocharts.model.PieChartData) SliceValue(lecho.lib.hellocharts.model.SliceValue) FloatMeasurementView(com.health.openscale.gui.views.FloatMeasurementView)

Aggregations

BMRMeasurementView (com.health.openscale.gui.views.BMRMeasurementView)3 FloatMeasurementView (com.health.openscale.gui.views.FloatMeasurementView)3 MeasurementView (com.health.openscale.gui.views.MeasurementView)3 ArrayList (java.util.ArrayList)3 ScaleMeasurement (com.health.openscale.core.datatypes.ScaleMeasurement)2 Calendar (java.util.Calendar)2 Stack (java.util.Stack)2 SimpleLineChartValueFormatter (lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter)2 Axis (lecho.lib.hellocharts.model.Axis)2 AxisValue (lecho.lib.hellocharts.model.AxisValue)2 Line (lecho.lib.hellocharts.model.Line)2 LineChartData (lecho.lib.hellocharts.model.LineChartData)2 PointValue (lecho.lib.hellocharts.model.PointValue)2 DashPathEffect (android.graphics.DashPathEffect)1 Converters (com.health.openscale.core.utils.Converters)1 PolynomialFitter (com.health.openscale.core.utils.PolynomialFitter)1 WeightMeasurementView (com.health.openscale.gui.views.WeightMeasurementView)1 SimpleDateFormat (java.text.SimpleDateFormat)1 PieChartData (lecho.lib.hellocharts.model.PieChartData)1 SliceValue (lecho.lib.hellocharts.model.SliceValue)1