Hello I would like a suggestion how can I save bar chart (MPChartAndroid library) data in the cloud I used Gson to Json then I write ArrayList to FireBase and to Shared Preference it is the proper way to do it?
The problem is: When i load the data the first two entry`s are set to 0 for example : i have a daily BarChart from 01.07.2016 to 01.07.2018. I register data on 28.06.2017 to 01.07.2017 , but the next day on 02.07.2017 i enter in the app , the Entry on 28.06 and 29.06 are set to 0.
Also i want the if users current day is higher then the last date in the ArrayList of dates to add new set of dates but keep the old one with dataEntry on it .
here is the declaration of the ArrayLists
private ArrayList<BarEntry> barEntryArrayList = new ArrayList<>();
private ArrayList<String> datelist = new ArrayList<>();
Retrieving data from Shared Prefs
SharedPreferences sharedPreferences = getSharedPreferences("SharedPrefs", 0);
SharedPreferences sharedChronoPrefs = getSharedPreferences("ChronometerSample", MODE_PRIVATE);
Type type = new TypeToken<ArrayList<String>>() {}.getType();
Type type2 = new TypeToken<ArrayList<BarEntry>>() {}.getType();
String jsonResp = sharedPreferences.getString("jsonString", null);
if (jsonResp != null) {
datelist = gson.fromJson(jsonResp, type);
}
Log.d(TAG, "loadDataList() called");
String json = sharedPreferences.getString("json", null);
if (json != null) {
barEntryArrayList = gson.fromJson(json, type2);
}
Setting up the Chart
addPreviousData();
addNewEntry();
BarDataSet barDataSet = new BarDataSet(barEntryArrayList, "# of Questions Answered");
LocalDate todayD = new LocalDate();
String tdy = todayD.toString();
BarData barData = new BarData(datelist, barDataSet);
int posX = datelist.indexOf(tdy);
currentPos = barData.getXValCount() - posX;
mChart.setScaleMinima((float) barData.getXValCount() / 5f, 1f);
mChart.zoom(-10f, 0f, 0, 0);
mChart.moveViewToX(barData.getXValCount() - currentPos - 3);
mChart.invalidate();
barDataSet.setValueTextColor(Color.GREEN);
barDataSet.setValueTextSize(14f);
barDataSet.setColor(Color.GREEN);
mChart.setData(barData);
mChart.notifyDataSetChanged();
mChart.animateXY(2000, 1000);
YAxis leftAxis = mChart.getAxisLeft();
leftAxis.setTextColor(Color.WHITE);
leftAxis.setAxisMinValue(0f);
leftAxis.setAxisMaxValue(100f);
Adding the Dates to the ArrayList datelist
private void addPreviousData() {
if (datelist == null || datelist.size() <= 0) {
LocalDate endDate = new LocalDate();
endDate = endDate.plusYears(1);
LocalDate starDate = endDate.minusYears(2);
List<LocalDate> totalDates = new ArrayList<>();
while (!starDate.isAfter(endDate)) {
totalDates.add(starDate);
starDate = starDate.plusDays(1);
}
for (int i = 0; i < totalDates.size(); i++) {
datelist.add(totalDates.get(i).toString());
}
Log.d(TAG, "addPreviousData() called" + totalDates.size());
Log.d(TAG, "addPreviousData :" + datelist.size());
}
}
Adding Entry to the chart
private void addNewEntry() {
if (barEntryArrayList == null || barEntryArrayList.size() <= 0) {
for (int i = 0; i < datelist.size(); i++) {
barEntryArrayList.add(new BarEntry(0f, i));
}
mChart.notifyDataSetChanged();
mChart.invalidate();
Log.d(TAG, "addNewEntry() returned: " + barEntryArrayList.size());
}
}
Updating the current day position :
// int posX = datelist.indexOf(tdy);
// currentPos = barData.getXValCount() - posX;
private void updateEntry(float updateValue) {
int updatePos = datelist.size() - currentPos;
if (isNewDay()) {
updatePos++;
}
BarEntry barEntry = barEntryArrayList.get(updatePos);
Log.d(TAG, "updateEntry() returned: position " + datelist.get(datelist.size() - currentPos));
barEntry.setVal(updateValue);
mChart.notifyDataSetChanged();
mChart.invalidate();
}
Saving the Data
private void saveDataList() {
Gson gson = new Gson();
SharedPreferences sharedPreferences = getSharedPreferences("SharedPrefs", 0);
SharedPreferences.Editor editor = sharedPreferences.edit();
String jsonString = gson.toJson(datelist);
String json = gson.toJson(barEntryArrayList);
editor.putString("jsonString", jsonString).apply();
editor.putString("json", json).apply();
LocalDate tomorrow = new LocalDate().plusDays(1);
editor.putString("LocalTime", tomorrow.toString());
}
Checking if its a new day
private boolean isNewDay() {
SharedPreferences sharedPreferences = getSharedPreferences("SharedPrefs", 0);
boolean isNewDay;
LocalDate todayDate = new LocalDate();
String tomorrow = sharedPreferences.getString("LocalTime", todayDate.plusDays(1).toString());
Log.d(TAG, "checkDates: sharedpRefs Tomorrow : " + sharedPreferences.getString("LocalTime", null));
String today = todayDate.toString();
if (today.equals(tomorrow)) {
tomorrow = todayDate.plusDays(1).toString();
isNewDay = true;
Log.d(TAG, "checkDates: Tomorow at End if/else :value : " + isNewDay + " " + tomorrow);
} else {
isNewDay = false;
Log.d(TAG, "checkDates: Tomorow at End if/else :value : " + isNewDay + " " + tomorrow);
}
return isNewDay;
}
and onCreate()
if (isNewDay() || isFirstRun) {
dailyQuestion = 0;
}