1
votes

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.

1
can you be more specific about what is not working?keepTrackOfYourStack
I'm able to cycle through the database and take the latest date of each type of task that is performed. This task and the last date that it was performed are shown in the chart. The date is stored as a string and I'm trying to subtract this date from the current date to produce an integer result. The idea was to color the parts of the chart depending on the integer value produced by subtracting the dates from one another. I've tried something similar to the top comment here: stackoverflow.com/questions/3838527/…. I'll edit above w/ my progresskells_276
so it must return something. start by logging past date, and current date in one log statement if they are right , get the days, do the math. the math is simple. if they are wrong your color will always be red.keepTrackOfYourStack
@kells_276, was my answer helpful? If you’ve still got doubts, please post a comment. If you’ve got the answer you asked for, please consider accepting the answer. What should I do when someone answers my question?Ole V.V.

1 Answers

1
votes

It’s a guess: When you do mPieDataSet.setColor(Color.RED);, I think you are setting the colour on the entire pie, not just on the individual slice/pie entry. In this way if the last entry you are processing in your loop is 90 days old, all of your pie chart gets the red colour.

I have not been able to reproduce a wrong calculation in your code where the count of days would go over 90 when it shouldn’t. If I set pastDateString to 4/1/2019, I get 88 days, and your if/else construct chooses the YELLOW way through.

Disclaimer: I don’t know MPAndroidChart.

General recommendations

If your database has a date datatype, use it for dates. Don’t store them as strings. Second, consider not using SimpleDateFormat, Calendar and Date. They are poorly designed and long outdated. You may add ThreeTenABP to your Android project and use LocalDate and other classes from java.time, the modern Java date and time API. It is so much nicer to work with. And gives much better support for calculation of differences between dates:

    long days = ChronoUnit.DAYS.between(pastLocalDate, LocalDate.now(yourTimeZone));

Links