use of com.health.openscale.gui.views.MeasurementView 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.MeasurementView 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;
}
use of com.health.openscale.gui.views.MeasurementView in project openScale by oliexdev.
the class StatisticsFragment method updateStatistics.
private void updateStatistics(List<ScaleMeasurement> scaleMeasurementList) {
Calendar histDate = Calendar.getInstance();
Calendar weekPastDate = Calendar.getInstance();
Calendar monthPastDate = Calendar.getInstance();
weekPastDate.setTime(lastScaleMeasurement.getDateTime());
weekPastDate.add(Calendar.DATE, -7);
monthPastDate.setTime(lastScaleMeasurement.getDateTime());
monthPastDate.add(Calendar.DATE, -30);
int weekSize = 0;
int monthSize = 0;
ScaleMeasurement averageWeek = new ScaleMeasurement();
ScaleMeasurement averageMonth = new ScaleMeasurement();
for (ScaleMeasurement measurement : scaleMeasurementList) {
histDate.setTime(measurement.getDateTime());
if (weekPastDate.before(histDate)) {
averageWeek.add(measurement);
weekSize++;
}
if (monthPastDate.before(histDate)) {
averageMonth.add(measurement);
monthSize++;
}
}
if (weekSize > 0) {
averageWeek.divide(weekSize);
}
if (monthSize > 0) {
averageMonth.divide(monthSize);
}
for (MeasurementView measurement : viewMeasurementsListWeek) {
measurement.loadFrom(averageWeek, null);
}
for (MeasurementView measurement : viewMeasurementsListMonth) {
measurement.loadFrom(averageMonth, null);
}
}
use of com.health.openscale.gui.views.MeasurementView in project openScale by oliexdev.
the class TableFragment method onCreateView.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
tableView = inflater.inflate(R.layout.fragment_table, container, false);
subpageView = (LinearLayout) tableView.findViewById(R.id.subpageView);
tableDataView = (ListView) tableView.findViewById(R.id.tableDataView);
tableHeaderView = (LinearLayout) tableView.findViewById(R.id.tableHeaderView);
tableDataView.setAdapter(new ListViewAdapter());
tableDataView.setOnItemClickListener(new onClickListenerRow());
if (savedInstanceState == null) {
selectedSubpageNr = 0;
} else {
selectedSubpageNr = savedInstanceState.getInt(SELECTED_SUBPAGE_NR_KEY);
}
measurementViews = MeasurementView.getMeasurementList(getContext(), MeasurementView.DateTimeOrder.FIRST);
for (MeasurementView measurement : measurementViews) {
measurement.setUpdateViews(false);
}
OpenScale.getInstance(getContext()).registerFragment(this);
return tableView;
}
use of com.health.openscale.gui.views.MeasurementView in project openScale by oliexdev.
the class TableFragment method updateOnView.
@Override
public void updateOnView(List<ScaleMeasurement> scaleMeasurementList) {
final int maxSize = 25;
final int subpageCount = (int) Math.ceil(scaleMeasurementList.size() / (double) maxSize);
if (selectedSubpageNr >= subpageCount) {
selectedSubpageNr = Math.max(0, subpageCount - 1);
}
subpageView.removeAllViews();
Button moveSubpageLeft = new Button(tableView.getContext());
moveSubpageLeft.setText("<");
moveSubpageLeft.setPadding(0, 0, 0, 0);
moveSubpageLeft.setTextColor(Color.WHITE);
moveSubpageLeft.setBackground(ContextCompat.getDrawable(tableView.getContext(), R.drawable.flat_selector));
moveSubpageLeft.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
moveSubpageLeft.getLayoutParams().height = pxImageDp(20);
moveSubpageLeft.getLayoutParams().width = pxImageDp(50);
moveSubpageLeft.setOnClickListener(new onClickListenerMoveSubpageLeft());
moveSubpageLeft.setEnabled(selectedSubpageNr > 0);
subpageView.addView(moveSubpageLeft);
for (int i = 0; i < subpageCount; i++) {
TextView subpageNrView = new TextView(tableView.getContext());
subpageNrView.setOnClickListener(new onClickListenerSubpageSelect());
subpageNrView.setText(Integer.toString(i + 1));
subpageNrView.setTextColor(Color.GRAY);
subpageNrView.setPadding(10, 10, 20, 10);
subpageView.addView(subpageNrView);
}
if (subpageView.getChildCount() > 1) {
TextView selectedSubpageNrView = (TextView) subpageView.getChildAt(selectedSubpageNr + 1);
if (selectedSubpageNrView != null) {
selectedSubpageNrView.setTypeface(null, Typeface.BOLD);
selectedSubpageNrView.setTextColor(Color.WHITE);
}
}
Button moveSubpageRight = new Button(tableView.getContext());
moveSubpageRight.setText(">");
moveSubpageRight.setPadding(0, 0, 0, 0);
moveSubpageRight.setTextColor(Color.WHITE);
moveSubpageRight.setBackground(ContextCompat.getDrawable(tableView.getContext(), R.drawable.flat_selector));
moveSubpageRight.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
moveSubpageRight.getLayoutParams().height = pxImageDp(20);
moveSubpageRight.getLayoutParams().width = pxImageDp(50);
moveSubpageRight.setOnClickListener(new onClickListenerMoveSubpageRight());
moveSubpageRight.setEnabled(selectedSubpageNr + 1 < subpageCount);
subpageView.addView(moveSubpageRight);
subpageView.setVisibility(subpageCount > 1 ? View.VISIBLE : View.GONE);
tableHeaderView.removeAllViews();
ArrayList<MeasurementView> visibleMeasurements = new ArrayList<>();
for (MeasurementView measurement : measurementViews) {
if (measurement.isVisible()) {
ImageView headerIcon = new ImageView(tableView.getContext());
headerIcon.setImageDrawable(measurement.getIcon());
headerIcon.setLayoutParams(new TableRow.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT, 1));
headerIcon.getLayoutParams().width = 0;
headerIcon.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
headerIcon.getLayoutParams().height = pxImageDp(20);
tableHeaderView.addView(headerIcon);
visibleMeasurements.add(measurement);
}
}
ListViewAdapter adapter = (ListViewAdapter) tableDataView.getAdapter();
final int startOffset = maxSize * selectedSubpageNr;
final int endOffset = Math.min(startOffset + maxSize + 1, scaleMeasurementList.size());
adapter.setMeasurements(visibleMeasurements, scaleMeasurementList.subList(startOffset, endOffset), maxSize);
}
Aggregations