use of com.health.openscale.gui.views.WeightMeasurementView 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);
}
use of com.health.openscale.gui.views.WeightMeasurementView in project openScale by oliexdev.
the class StatisticsFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
statisticsView = inflater.inflate(R.layout.fragment_statistics, container, false);
// Set android:tint="?attr/editTextColor" programmatically as setting it in xml layout
// throws an exception on API 19.
int color = new EditText(getContext()).getCurrentTextColor();
for (int id : new int[] { R.id.imageGoalWeight, R.id.imageGoalDiff, R.id.imageDayLeft }) {
ImageView image = statisticsView.findViewById(id);
image.setColorFilter(color);
}
txtGoalWeight = (TextView) statisticsView.findViewById(R.id.txtGoalWeight);
txtGoalDiff = (TextView) statisticsView.findViewById(R.id.txtGoalDiff);
txtGoalDayLeft = (TextView) statisticsView.findViewById(R.id.txtGoalDayLeft);
txtLabelGoalWeight = (TextView) statisticsView.findViewById(R.id.txtLabelGoalWeight);
txtLabelGoalDiff = (TextView) statisticsView.findViewById(R.id.txtLabelGoalDiff);
txtLabelDayLeft = (TextView) statisticsView.findViewById(R.id.txtLabelDayLeft);
tableWeekAveragesLayoutColumnA = (TableLayout) statisticsView.findViewById(R.id.tableWeekAveragesLayoutColumnA);
tableWeekAveragesLayoutColumnB = (TableLayout) statisticsView.findViewById(R.id.tableWeekAveragesLayoutColumnB);
tableMonthAveragesLayoutColumnA = (TableLayout) statisticsView.findViewById(R.id.tableMonthAveragesLayoutColumnA);
tableMonthAveragesLayoutColumnB = (TableLayout) statisticsView.findViewById(R.id.tableMonthAveragesLayoutColumnB);
viewMeasurementsListWeek = new ArrayList<>();
viewMeasurementsListWeek.add(new WeightMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new WaterMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new MuscleMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new LBWMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new FatMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new BoneMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new WaistMeasurementView(statisticsView.getContext()));
viewMeasurementsListWeek.add(new HipMeasurementView(statisticsView.getContext()));
final int paddingBottom = 10;
int i = 0;
for (MeasurementView measurement : viewMeasurementsListWeek) {
measurement.setEditMode(STATISTIC);
if (measurement.getSettings().isEnabled()) {
measurement.setVisible(true);
measurement.setPadding(-1, -1, -1, paddingBottom);
if ((i % 2) == 0) {
tableWeekAveragesLayoutColumnA.addView(measurement);
} else {
tableWeekAveragesLayoutColumnB.addView(measurement);
}
i++;
}
}
viewMeasurementsListMonth = new ArrayList<>();
viewMeasurementsListMonth.add(new WeightMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new WaterMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new MuscleMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new LBWMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new FatMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new BoneMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new WaistMeasurementView(statisticsView.getContext()));
viewMeasurementsListMonth.add(new HipMeasurementView(statisticsView.getContext()));
i = 0;
for (MeasurementView measurement : viewMeasurementsListMonth) {
measurement.setEditMode(STATISTIC);
if (measurement.getSettings().isEnabled()) {
measurement.setVisible(true);
measurement.setPadding(-1, -1, -1, paddingBottom);
if ((i % 2) == 0) {
tableMonthAveragesLayoutColumnA.addView(measurement);
} else {
tableMonthAveragesLayoutColumnB.addView(measurement);
}
i++;
}
}
OpenScale.getInstance(getContext()).registerFragment(this);
return statisticsView;
}
Aggregations