[英]How to calculate the difference between the dates shown in the piechart with the current date
我有一個 SQLite 數據庫,其中包含許多任務和完成這些任務的日期。 然后我將這些日期添加為餅圖中的 PieEntries。 我想找出圖表上的日期和當前日期之間的差異。
這個想法是根據日期之間的差異為圖表的不同部分設置不同的顏色。
我在這里找到了一些帖子,例如我一直在使用的帖子: 兩個日期之間的 Android 差異。 但到目前為止,似乎沒有任何效果。
下面是我的光標在數據庫中循環並使用它來獲取日期並將它們放入 PieEntry 列表中。 還有我用來計算當前日期的代碼和用於計算填充圖表和當前日期之間差異的代碼。
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();
這個想法是從 currentDate 中減去過去的日期以顯示一個整數值,然后可以使用它來更改餅圖段的顏色。
編輯:
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();
此新代碼不會返回天數差異的整數值,並且所有圖表段當前都顯示為紅色。
這是一個猜測:當你做mPieDataSet.setColor(Color.RED);
,我認為您是在整個餅圖上設置顏色,而不僅僅是在單個切片/餅圖條目上。 這樣,如果您在循環中處理的最后一個條目是 90 天前的,那么您的所有餅圖都會變為紅色。
我無法在您的代碼中重現錯誤的計算,其中天數會超過 90,而本不應該。 如果我將pastDateString
設置為4/1/2019
,我得到 88 天,並且您的if
/ else
構造選擇黃色方式通過。
免責聲明:我不知道 MPAndroidChart。
如果您的數據庫具有date
數據類型,請將其用於日期。 不要將它們存儲為字符串。 其次,考慮不使用SimpleDateFormat
、 Calendar
和Date
。 它們的設計很差,而且已經過時了。 您可以將 ThreeTenABP 添加到您的 Android 項目中,並使用LocalDate
和來自 java.time(現代 Java 日期和時間 API)的其他類。 和它一起工作要好得多。 並為計算日期之間的差異提供更好的支持:
long days = ChronoUnit.DAYS.between(pastLocalDate, LocalDate.now(yourTimeZone));
java.time
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.