use of com.health.openscale.core.utils.PolynomialFitter 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);
}
Aggregations