mirror of
https://github.com/oliexdev/openScale.git
synced 2025-08-22 08:13:43 +02:00
set the graph viewport dynamically
This commit is contained in:
@@ -159,6 +159,10 @@ public class OpenScale {
|
|||||||
return scaleDB.getAllDBEntriesOfMonth(year, month);
|
return scaleDB.getAllDBEntriesOfMonth(year, month);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float getMaxValueOfDBEntries(int year, int month) {
|
||||||
|
return scaleDB.getMaxValueOfDBEntries(year, month);
|
||||||
|
}
|
||||||
|
|
||||||
public void startBluetoothServer(String deviceName) {
|
public void startBluetoothServer(String deviceName) {
|
||||||
Log.d("OpenScale", "Bluetooth Server started! I am searching for device ...");
|
Log.d("OpenScale", "Bluetooth Server started! I am searching for device ...");
|
||||||
|
|
||||||
|
@@ -146,6 +146,48 @@ public class ScaleDatabase extends SQLiteOpenHelper {
|
|||||||
return numOfMonth;
|
return numOfMonth;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public float getMaxValueOfDBEntries(int year, int month) {
|
||||||
|
SQLiteDatabase db = getReadableDatabase();
|
||||||
|
|
||||||
|
Calendar start_cal = Calendar.getInstance();
|
||||||
|
Calendar end_cal = Calendar.getInstance();
|
||||||
|
|
||||||
|
start_cal.set(year, month, 1, 0, 0, 0);
|
||||||
|
end_cal.set(year, month, 1, 0, 0, 0);
|
||||||
|
end_cal.add(Calendar.MONTH, 1);
|
||||||
|
|
||||||
|
String[] projection = {
|
||||||
|
"MAX(" + COLUMN_NAME_WEIGHT + ")",
|
||||||
|
"MAX(" + COLUMN_NAME_FAT + ")",
|
||||||
|
"MAX(" + COLUMN_NAME_WATER + ")",
|
||||||
|
"MAX(" + COLUMN_NAME_MUSCLE + ")"
|
||||||
|
};
|
||||||
|
|
||||||
|
Cursor cursorScaleDB = db.query(
|
||||||
|
TABLE_NAME, // The table to query
|
||||||
|
projection, // The columns to return
|
||||||
|
COLUMN_NAME_DATE_TIME + " >= ? AND " + COLUMN_NAME_DATE_TIME + " < ? ", // The columns for the WHERE clause
|
||||||
|
new String[]{formatDateTime.format(start_cal.getTime()), formatDateTime.format(end_cal.getTime())}, // The values for the WHERE clause
|
||||||
|
null, // don't group the rows
|
||||||
|
null, // don't filter by row groups
|
||||||
|
null // The sort order
|
||||||
|
);
|
||||||
|
|
||||||
|
cursorScaleDB.moveToFirst();
|
||||||
|
|
||||||
|
float maxValue = -1;
|
||||||
|
|
||||||
|
for (int i=0; i<4; i++)
|
||||||
|
{
|
||||||
|
if (maxValue < cursorScaleDB.getFloat(i))
|
||||||
|
{
|
||||||
|
maxValue = cursorScaleDB.getFloat(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return maxValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public ArrayList<ScaleData> getAllDBEntriesOfMonth(int year, int month) {
|
public ArrayList<ScaleData> getAllDBEntriesOfMonth(int year, int month) {
|
||||||
SQLiteDatabase db = getReadableDatabase();
|
SQLiteDatabase db = getReadableDatabase();
|
||||||
|
@@ -117,15 +117,16 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
private void generateLineData(Calendar cal)
|
private void generateLineData(Calendar cal)
|
||||||
{
|
{
|
||||||
scaleDBEntries = openScale.getAllDataOfMonth(yearCal.get(Calendar.YEAR), cal.get(Calendar.MONTH));
|
scaleDBEntries = openScale.getAllDataOfMonth(yearCal.get(Calendar.YEAR), cal.get(Calendar.MONTH));
|
||||||
|
float maxValue = openScale.getMaxValueOfDBEntries(yearCal.get(Calendar.YEAR), cal.get(Calendar.MONTH));
|
||||||
|
|
||||||
SimpleDateFormat day_date = new SimpleDateFormat("dd", Locale.getDefault());
|
SimpleDateFormat day_date = new SimpleDateFormat("dd", Locale.getDefault());
|
||||||
|
|
||||||
cal.set(Calendar.DAY_OF_MONTH, 1);
|
cal.set(Calendar.DAY_OF_MONTH, 1);
|
||||||
int max_days = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
int maxDays = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
|
||||||
|
|
||||||
List<AxisValue> axisValues = new ArrayList<AxisValue>();
|
List<AxisValue> axisValues = new ArrayList<AxisValue>();
|
||||||
|
|
||||||
for (int i=0; i<max_days; i++) {
|
for (int i=0; i<maxDays; i++) {
|
||||||
String day_name = day_date.format(cal.getTime());
|
String day_name = day_date.format(cal.getTime());
|
||||||
|
|
||||||
axisValues.add(new AxisValue(i, day_name.toCharArray()));
|
axisValues.add(new AxisValue(i, day_name.toCharArray()));
|
||||||
@@ -212,7 +213,13 @@ public class GraphFragment extends Fragment implements FragmentUpdateListener {
|
|||||||
chartTop.setLineChartData(lineData);
|
chartTop.setLineChartData(lineData);
|
||||||
chartTop.setViewportCalculationEnabled(false);
|
chartTop.setViewportCalculationEnabled(false);
|
||||||
|
|
||||||
Viewport v = new Viewport(0, 110, max_days, 0);
|
if (maxValue == 0.0) {
|
||||||
|
maxValue = 100;
|
||||||
|
} else {
|
||||||
|
maxValue += 20;
|
||||||
|
}
|
||||||
|
|
||||||
|
Viewport v = new Viewport(0, (int)maxValue, maxDays, 0);
|
||||||
chartTop.setMaximumViewport(v);
|
chartTop.setMaximumViewport(v);
|
||||||
chartTop.setCurrentViewport(v, true);
|
chartTop.setCurrentViewport(v, true);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user