mirror of
https://github.com/oliexdev/openScale.git
synced 2025-09-03 13:23:24 +02:00
fixed average calculation in graph fragment.
This commit is contained in:
@@ -45,6 +45,7 @@ import com.health.openscale.core.utils.PolynomialFitter;
|
||||
import com.health.openscale.gui.activities.DataEntryActivity;
|
||||
import com.health.openscale.gui.views.FloatMeasurementView;
|
||||
import com.health.openscale.gui.views.MeasurementView;
|
||||
import com.health.openscale.gui.views.WeightMeasurementView;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
@@ -261,34 +262,6 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
floatingActionBar.addView(actionButton);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a point to a point value stack.
|
||||
*
|
||||
* Average y value of point if x value is already on the stack and option "averageData" is enabled.
|
||||
*
|
||||
* @param pointValues stack of point values
|
||||
* @param value_x x value of the point
|
||||
* @param value_y y value of the point
|
||||
* @return true if a new point was added otherwise false if point was average added to an existing point
|
||||
*/
|
||||
private boolean addPointValue(Stack<PointValue> pointValues, float value_x, float value_y) {
|
||||
if (prefs.getBoolean("averageData", true) && !pointValues.isEmpty() && pointValues.peek().getX() == value_x) {
|
||||
PointValue prevValue = pointValues.pop();
|
||||
PointValue newValue = new PointValue(value_x, (prevValue.getY() + value_y) / 2.0f);
|
||||
newValue.setLabel(String.format("Ø %.2f", newValue.getY()));
|
||||
|
||||
pointValues.push(newValue);
|
||||
return true;
|
||||
} else {
|
||||
if (value_y != 0.0f) { // don't show zero values
|
||||
pointValues.push(new PointValue(value_x, value_y));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void generateLineData(int field, List<ScaleMeasurement> scaleMeasurementList)
|
||||
{
|
||||
SimpleDateFormat day_date = new SimpleDateFormat("D", Locale.getDefault());
|
||||
@@ -317,14 +290,14 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
Calendar calDays = (Calendar)calLastSelected.clone();
|
||||
|
||||
calDays.set(field, calDays.getActualMinimum(field));
|
||||
int maxDays = calDays.getActualMaximum(field);
|
||||
int maxDays = calDays.getMaximum(field);
|
||||
|
||||
List<AxisValue> axisValues = new ArrayList<AxisValue>();
|
||||
List<AxisValue> axisValues = new ArrayList<>();
|
||||
|
||||
for (int i=0; i<maxDays+1; i++) {
|
||||
for (int i=0; i<maxDays; i++) {
|
||||
String day_name = day_date.format(calDays.getTime());
|
||||
|
||||
axisValues.add(new AxisValue(i+calDays.getActualMinimum(field), day_name.toCharArray()));
|
||||
axisValues.add(new AxisValue(i, day_name.toCharArray()));
|
||||
|
||||
calDays.add(field, 1);
|
||||
}
|
||||
@@ -337,6 +310,8 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
|
||||
floatingActionBar.removeAllViews();
|
||||
|
||||
PolynomialFitter polyFitter = new PolynomialFitter(Integer.parseInt(prefs.getString("regressionLineOrder", "1")));
|
||||
|
||||
for (MeasurementView view : measurementViews) {
|
||||
if (view instanceof FloatMeasurementView) {
|
||||
FloatMeasurementView measurementView = (FloatMeasurementView) view;
|
||||
@@ -345,16 +320,49 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
continue;
|
||||
}
|
||||
|
||||
Stack<PointValue> valuesStack = new Stack<PointValue>();
|
||||
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());
|
||||
|
||||
if (addPointValue(valuesStack, calDB.get(field), measurementView.getValue())) {
|
||||
pointIndexScaleMeasurementList.add(measurement); // if new point was added, add this point to pointIndexScaleDataList to get the correct point index after selecting an point
|
||||
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).
|
||||
@@ -387,7 +395,7 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
|
||||
chartBottom.setLineChartData(lineData);
|
||||
|
||||
defaultTopViewport = new Viewport(calDays.getActualMinimum(field), chartBottom.getCurrentViewport().top, maxDays+1, chartBottom.getCurrentViewport().bottom);
|
||||
defaultTopViewport = new Viewport(calDays.getMinimum(field), chartBottom.getCurrentViewport().top, maxDays+1, chartBottom.getCurrentViewport().bottom);
|
||||
|
||||
if (prefs.getBoolean("goalLine", true)) {
|
||||
Stack<PointValue> valuesGoalLine = new Stack<PointValue>();
|
||||
@@ -406,36 +414,21 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
}
|
||||
|
||||
if (prefs.getBoolean("regressionLine", false)) {
|
||||
PolynomialFitter polyFitter = new PolynomialFitter(Integer.parseInt(prefs.getString("regressionLineOrder", "1")));
|
||||
|
||||
Stack<PointValue> valuesWeight = new Stack<PointValue>();
|
||||
|
||||
for (ScaleMeasurement measurement : scaleMeasurementList) {
|
||||
calDB.setTime(measurement.getDateTime());
|
||||
|
||||
addPointValue(valuesWeight, calDB.get(field), measurement.getConvertedWeight(openScale.getSelectedScaleUser().getScaleUnit()));
|
||||
}
|
||||
|
||||
for (PointValue value : valuesWeight) {
|
||||
polyFitter.addPoint(value.getX(), value.getY());
|
||||
}
|
||||
|
||||
PolynomialFitter.Polynomial polynom = polyFitter.getBestFit();
|
||||
|
||||
Stack<PointValue> valuesLinearRegression = new Stack<PointValue>();
|
||||
Stack<PointValue> valuesLinearRegression = new Stack<>();
|
||||
|
||||
if (!valuesWeight.isEmpty()) {
|
||||
for (int i = 0; i < maxDays+1; i++) {
|
||||
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));
|
||||
linearRegressionLine.setPathEffect(new DashPathEffect(new float[]{10, 30}, 0));
|
||||
|
||||
diagramLineList.add(linearRegressionLine);
|
||||
}
|
||||
@@ -518,7 +511,7 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
// show only yearly diagram and hide monthly diagram
|
||||
} else {
|
||||
chartTop.setVisibility(View.GONE);
|
||||
chartBottom.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
|
||||
chartBottom.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 0, 0.9f));
|
||||
|
||||
scaleMeasurementList = openScale.getScaleDataOfYear(selectedYear);
|
||||
|
||||
@@ -535,8 +528,7 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
||||
|
||||
calLastSelected = cal;
|
||||
|
||||
List<ScaleMeasurement> scaleMeasurementList =
|
||||
openScale.getScaleDataOfMonth(calYears.get(Calendar.YEAR), calLastSelected.get(Calendar.MONTH));
|
||||
List<ScaleMeasurement> scaleMeasurementList = openScale.getScaleDataOfMonth(calYears.get(Calendar.YEAR), calLastSelected.get(Calendar.MONTH));
|
||||
generateLineData(Calendar.DAY_OF_MONTH, scaleMeasurementList);
|
||||
}
|
||||
|
||||
|
@@ -170,7 +170,6 @@
|
||||
<string name="label_not_found">no trobat</string>
|
||||
<string name="label_ignoreOutOfRange">Ignorar dades fora rang</string>
|
||||
<string name="label_initial_weight">Pes inicial</string>
|
||||
<string name="label_average_data">Calcular mitjana per dia/mes</string>
|
||||
<string name="label_regression_line">Polinomi de regressió del pes</string>
|
||||
<string name="label_regression_line_degree">Grau del polinomi de regressió</string>
|
||||
<string name="label_goal_line">Línia de l\'objectiu</string>
|
||||
|
@@ -141,7 +141,6 @@
|
||||
<string name="label_not_found">nenalezeno</string>
|
||||
<string name="label_ignoreOutOfRange">Ignorovat data která jsou mimo rozsah</string>
|
||||
<string name="label_initial_weight">Počáteční hmotnost</string>
|
||||
<string name="label_average_data">Spočítat průměr za den/měsíc</string>
|
||||
<string name="label_help">Nápověda</string>
|
||||
|
||||
<string name="label_feedback_message_enjoying">Líbí se vám openScale?</string>
|
||||
|
@@ -127,7 +127,6 @@
|
||||
<string name="info_enter_initial_weight">Gebe dein Gewicht in deiner Einheit an</string>
|
||||
<string name="info_enter_goal_weight">Gebe dein Gewicht in deiner Einheit an</string>
|
||||
<string name="error_initial_weight_required">Fehler Anfangsgewicht ist erforderlich</string>
|
||||
<string name="label_average_data">Berechne Durchschnitt pro Tag/Monat</string>
|
||||
<string name="label_bt_device_no_support">Gerät wird nicht unterstützt</string>
|
||||
<string name="label_goal_line">Ziellinie</string>
|
||||
<string name="label_regression_line">Gewichtsregressionsgerade</string>
|
||||
|
@@ -170,7 +170,6 @@
|
||||
<string name="label_not_found">no encontrado</string>
|
||||
<string name="label_ignoreOutOfRange">Ignorar los datos fuera de rango</string>
|
||||
<string name="label_initial_weight">Peso inicial</string>
|
||||
<string name="label_average_data">Calcular media por día/mes</string>
|
||||
<string name="label_regression_line">Línea de peso de regresión</string>
|
||||
<string name="label_regression_line_degree">Línea de grado de polinomio de regresión</string>
|
||||
<string name="label_goal_line">Línea del objetivo</string>
|
||||
|
@@ -172,7 +172,6 @@
|
||||
<string name="label_not_found">niet gevonden</string>
|
||||
<string name="label_ignoreOutOfRange">Negeer gegevens die buiten bereik zijn</string>
|
||||
<string name="label_initial_weight">Begingewicht</string>
|
||||
<string name="label_average_data">Bereken het gemiddelde per dag/maand</string>
|
||||
<string name="label_regression_line">Regressie gewichtslijn</string>
|
||||
<string name="label_regression_line_degree">Regressie polynoomlijn</string>
|
||||
<string name="label_goal_line">Doel lijn</string>
|
||||
|
@@ -150,7 +150,6 @@
|
||||
<string name="label_not_found">nie znaleziono</string>
|
||||
<string name="label_ignoreOutOfRange">Ignoruj dane spoza zakresu</string>
|
||||
<string name="label_initial_weight">Waga początkowa</string>
|
||||
<string name="label_average_data">Oblicz średnią na dzień/miesiąc</string>
|
||||
<string name="label_regression_line">Regression weight line</string>
|
||||
<string name="label_regression_line_degree">Regression polynom degree</string>
|
||||
<string name="label_goal_line">Linia celu</string>
|
||||
|
@@ -58,7 +58,6 @@
|
||||
<string name="info_your_weight">Seu peso foi</string>
|
||||
<string name="label_add">Adicionar</string>
|
||||
<string name="label_add_user">Adicionar usuário</string>
|
||||
<string name="label_average_data">Calcular média</string>
|
||||
<string name="label_backup">Backup</string>
|
||||
<string name="label_birthday">Data de nascimento</string>
|
||||
<string name="label_bluetooth_enable">Procurar por balança ao iniciar</string>
|
||||
|
@@ -131,8 +131,7 @@
|
||||
<string name="label_not_found">nenájdený</string>
|
||||
<string name="label_ignoreOutOfRange">Ignorovať údaje, ktoré sú mimo rozsah</string>
|
||||
<string name="label_initial_weight">Počiatočná hmotnosť</string>
|
||||
<string name="label_average_data">Vypočítať priemer za deň/mesiac</string>
|
||||
<string name="label_regression_line">Vývoj úbytku hmotnosti</string>
|
||||
<string name="label_regression_line">Vývoj úbytku hmotnosti</string>
|
||||
<string name="label_regression_line_degree">Stupeň polynomiálnej regresie</string>
|
||||
<string name="label_goal_line">Cieľová línia</string>
|
||||
<string name="error_max_scale_users">Dosiahli ste maximálny počet viacerých používateľov</string>
|
||||
|
@@ -147,7 +147,6 @@
|
||||
<string name="label_not_found">hittades inte</string>
|
||||
<string name="label_ignoreOutOfRange">Ignorera data som är utanför intervallet</string>
|
||||
<string name="label_initial_weight">Startvikt</string>
|
||||
<string name="label_average_data">Beräkna medelvärde per dag/månad</string>
|
||||
<string name="label_regression_line">Vikt regressionslinje</string>
|
||||
<string name="label_regression_line_degree">Regressions-polynom-gradtal</string>
|
||||
<string name="label_goal_line">Mållinje</string>
|
||||
|
@@ -168,7 +168,6 @@
|
||||
<string name="label_not_found">Bulunamadý</string>
|
||||
<string name="label_ignoreOutOfRange">Mesafenin dýþýndaki verileri yoksay</string>
|
||||
<string name="label_initial_weight">Baþlangýç ??aðýrlýðý</string>
|
||||
<string name="label_average_data">Ortalama hesap Günlük/Aylýk</string>
|
||||
<string name="label_regression_line">Regresyon aðýrlýk çizgisi</string>
|
||||
<string name="label_regression_line_degree">Regresyon polinom derecesi</string>
|
||||
<string name="label_goal_line">Hedef çizgisi</string>
|
||||
|
@@ -185,7 +185,6 @@
|
||||
<string name="label_not_found">not found</string>
|
||||
<string name="label_ignoreOutOfRange">Ignore data that are out of range</string>
|
||||
<string name="label_initial_weight">Initial weight</string>
|
||||
<string name="label_average_data">Calculate average per day/month</string>
|
||||
<string name="label_regression_line">Regression weight line</string>
|
||||
<string name="label_regression_line_degree">Regression polynom degree</string>
|
||||
<string name="label_goal_line">Goal line</string>
|
||||
|
@@ -1,6 +1,5 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
<CheckBoxPreference android:title="@string/label_average_data" android:summaryOn="@string/info_is_enable" android:summaryOff="@string/info_is_not_enable" android:key="averageData" android:defaultValue="true" />
|
||||
<CheckBoxPreference android:title="@string/label_enable_labels" android:summaryOn="@string/info_is_visible" android:summaryOff="@string/info_is_not_visible" android:key="labelsEnable" android:defaultValue="true"/>
|
||||
<CheckBoxPreference android:title="@string/label_enable_points" android:summaryOn="@string/info_is_visible" android:summaryOff="@string/info_is_not_visible" android:key="pointsEnable" android:defaultValue="true"/>
|
||||
<CheckBoxPreference android:title="@string/label_goal_line" android:summaryOn="@string/info_is_visible" android:summaryOff="@string/info_is_not_visible" android:key="goalLine" android:defaultValue="true" />
|
||||
|
Reference in New Issue
Block a user