I have an SQLite database containing a number of tasks and the dates on which these tasks were completed. I am then adding these dates as PieEntries in a piechart. I would like to find the difference between the dates on the chart and the current date.
The idea would be to set different colors to different segments of the chart depending on the difference between the dates.
I've found a few posts on here such as this one that I've been using : Android difference between Two Dates. But so far nothing seems to be working.
Below is my cursor cycling through the database and using that to get the dates and place them in to PieEntry List. There is also the code I'm using to calculate the current date and the code to calculate the difference between those that populate the chart and the current date.
final ArrayList<TaskObject> taskObjects = new ArrayList<>();
if (mCursor != null) {
mCursor.moveToFirst();
do {
TaskObject taskObject = new TaskObject();
taskObject.setTask(mCursor.getString(mCursor.getColumnIndex(TaskEntry.COLUMN_TASK)));
taskObject.setObserver(mCursor.getString(mCursor.getColumnIndex(TaskEntry.COLUMN_OBSERVER)));
taskObject.setDate(mCursor.getString(mCursor.getColumnIndex(TaskEntry.COLUMN_DATE)));
taskObjects.add(taskObject);
} while (mCursor.moveToNext());
float distribution = 100 / taskObjects.size();
List<PieEntry> pieEntries = new ArrayList<>();
for (int i = 0; i < taskObjects.size(); i++) {
pieEntries.add(new PieEntry(distribution, (taskObjects.get(i).getTask()) + "\n" + taskObjects.get(i).getDate()));
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
String pastDateString = taskObjects.get(i).getDate();
Date currentDate = Calendar.getInstance().getTime();
Date pastDate = null;
try {
pastDate = simpleDateFormat.parse(pastDateString);
} catch (ParseException e) {
e.printStackTrace();
}
//Comparing dates
long difference = Math.abs(currentDate.getTime() - pastDate.getTime());
long differenceDates = difference / (24 * 60 * 60 * 1000);
//Convert long to String
String dayDifference = Long.toString(differenceDates);
Log.e("HERE","HERE: " + dayDifference);
}
PieDataSet pieDataSet = new PieDataSet(pieEntries, "Label");
pieDataSet.setColors(ColorTemplate.MATERIAL_COLORS);
PieData pieData = new PieData(pieDataSet);
mPieChart.setData(pieData);
mPieChart.invalidate();
}
mCursor.close();
The idea is to subtract the pastDate from the currentDate to reveal an integer value that could then be used to change the color of the piechart segment.
EDIT:
List<PieEntry> pieEntries = new ArrayList<>();
for (int i = 0; i < taskObjects.size(); i++) {
pieEntries.add(new PieEntry(distribution, (taskObjects.get(i).getTask()) + "\n" + taskObjects.get(i).getDate()));
mPieDataSet = new PieDataSet(pieEntries, "Versatility Matrix & Qualification Status");
String pastDateString = taskObjects.get(i).getDate();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date d = null;
try {
d = simpleDateFormat.parse(pastDateString);//catch exception
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Calendar pastDate = Calendar.getInstance();
pastDate.setTime(d);
Calendar currentDate = Calendar.getInstance();
long diff = currentDate.getTimeInMillis() - pastDate.getTimeInMillis();
long days = diff/(24*60*60*1000);
int daysDifference = (int) days;
Log.d(TAG, "days in difference = " + daysDifference);
if(daysDifference >= 90) {
mPieDataSet.setColor(Color.RED);
} else if (daysDifference < 90 && daysDifference >= 60) {
mPieDataSet.setColor(Color.YELLOW);
} else {
mPieDataSet.setColor(Color.GREEN);
}
}
PieData pieData = new PieData(mPieDataSet);
mPieChart.setData(pieData);
mPieChart.invalidate();
This new code doesn't return an integer value for the difference in days and all chart segments are showing as red currently.