-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskStatisticsWindow.xaml.cs
More file actions
318 lines (272 loc) · 12.9 KB
/
TaskStatisticsWindow.xaml.cs
File metadata and controls
318 lines (272 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
namespace TimeTask
{
/// <summary>
/// TaskStatisticsWindow.xaml 的交互逻辑
/// </summary>
public partial class TaskStatisticsWindow : Window
{
private string _currentPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
public TaskStatisticsWindow()
{
InitializeComponent();
LoadStatistics();
}
private void LoadStatistics()
{
try
{
// 获取所有CSV文件中的任务数据
var allTasks = GetAllTasks();
// 更新概览统计
UpdateOverviewStats(allTasks);
// 更新象限分布
UpdateQuadrantDistribution(allTasks);
// 更新最近活动
UpdateRecentActivity(allTasks);
// 更新效率分析
UpdateEfficiencyAnalysis(allTasks);
}
catch (Exception ex)
{
MessageBox.Show($"加载统计数据时发生错误: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private List<ItemGrid> GetAllTasks()
{
var allTasks = new List<ItemGrid>();
string dataPath = Path.Combine(_currentPath, "data");
// 读取所有象限的CSV文件
for (int i = 1; i <= 4; i++)
{
string csvFile = Path.Combine(dataPath, $"{i}.csv");
if (File.Exists(csvFile))
{
var tasks = HelperClass.ReadCsv(csvFile);
if (tasks != null)
{
allTasks.AddRange(tasks);
}
}
}
return allTasks;
}
private void UpdateOverviewStats(List<ItemGrid> allTasks)
{
int totalTasks = allTasks.Count;
int completedTasks = allTasks.Count(t => !t.IsActive);
int activeTasks = allTasks.Count(t => t.IsActive);
double completionRate = totalTasks > 0 ? (double)completedTasks / totalTasks * 100 : 0;
TotalTasksText.Text = totalTasks.ToString();
CompletedTasksText.Text = completedTasks.ToString();
ActiveTasksText.Text = activeTasks.ToString();
CompletionRateText.Text = $"{completionRate:F1}%";
}
private void UpdateQuadrantDistribution(List<ItemGrid> allTasks)
{
// 由于我们无法直接知道每个任务属于哪个象限,我们需要从UI获取信息
// 或者根据重要性和紧急性推断象限
var quadrantCounts = new Dictionary<string, int>
{
{"重要且紧急", 0},
{"重要不紧急", 0},
{"不重要但紧急", 0},
{"不重要不紧急", 0}
};
foreach (var task in allTasks)
{
string quadrant = DetermineQuadrant(task.Importance, task.Urgency);
quadrantCounts[quadrant]++;
}
int totalTasks = allTasks.Count;
double q1Percent = totalTasks > 0 ? (double)quadrantCounts["重要且紧急"] / totalTasks * 100 : 0;
double q2Percent = totalTasks > 0 ? (double)quadrantCounts["重要不紧急"] / totalTasks * 100 : 0;
double q3Percent = totalTasks > 0 ? (double)quadrantCounts["不重要但紧急"] / totalTasks * 100 : 0;
double q4Percent = totalTasks > 0 ? (double)quadrantCounts["不重要不紧急"] / totalTasks * 100 : 0;
Q1Label.Text = $"重要且紧急: {quadrantCounts["重要且紧急"]} ({q1Percent:F1}%)";
Q2Label.Text = $"重要不紧急: {quadrantCounts["重要不紧急"]} ({q2Percent:F1}%)";
Q3Label.Text = $"不重要但紧急: {quadrantCounts["不重要但紧急"]} ({q3Percent:F1}%)";
Q4Label.Text = $"不重要不紧急: {quadrantCounts["不重要不紧急"]} ({q4Percent:F1}%)";
// 设置进度条的宽度(通过绑定到实际宽度)
Q1ProgressBar.Width = Q1ProgressBar.ActualWidth * q1Percent / 100;
Q2ProgressBar.Width = Q2ProgressBar.ActualWidth * q2Percent / 100;
Q3ProgressBar.Width = Q3ProgressBar.ActualWidth * q3Percent / 100;
Q4ProgressBar.Width = Q4ProgressBar.ActualWidth * q4Percent / 100;
}
private string DetermineQuadrant(string importance, string urgency)
{
bool isImportant = importance?.ToLower() == "high" || importance?.ToLower() == "important";
bool isUrgent = urgency?.ToLower() == "high" || urgency?.ToLower() == "urgent";
if (isImportant && isUrgent) return "重要且紧急";
if (isImportant && !isUrgent) return "重要不紧急";
if (!isImportant && isUrgent) return "不重要但紧急";
return "不重要不紧急";
}
private void UpdateRecentActivity(List<ItemGrid> allTasks)
{
RecentActivityListBox.Items.Clear();
// 按最后修改日期排序,取最近的10个任务
var recentTasks = allTasks
.OrderByDescending(t => t.LastModifiedDate)
.Take(10);
foreach (var task in recentTasks)
{
var activity = new
{
Description = task.Task,
Timestamp = task.LastModifiedDate
};
RecentActivityListBox.Items.Add(activity);
}
}
private void UpdateEfficiencyAnalysis(List<ItemGrid> allTasks)
{
// 计算平均完成时间(对于已完成的任务)
var completedTasks = allTasks.Where(t => !t.IsActive && t.CompletionTime.HasValue);
if (completedTasks.Any())
{
var avgCompletionTime = completedTasks.Average(t => (t.CompletionTime.Value - t.CreatedDate).TotalDays);
AvgCompletionTimeText.Text = $"{avgCompletionTime:F1} 天";
}
else
{
AvgCompletionTimeText.Text = "无完成任务";
}
// 找出最高效的时段(基于完成任务的时间段)
var completedHours = completedTasks
.GroupBy(t => t.CompletionTime.Value.Hour)
.OrderByDescending(g => g.Count())
.FirstOrDefault();
if (completedHours != null)
{
MostProductiveTimeText.Text = $"{completedHours.Key} 点 (完成 {completedHours.Count()} 个任务)";
}
else
{
MostProductiveTimeText.Text = "无完成任务";
}
// 计算延期率
int overdueTasks = allTasks.Count(t => t.IsActive && t.ReminderTime.HasValue && t.ReminderTime.Value < DateTime.Now);
double delayRate = allTasks.Count > 0 ? (double)overdueTasks / allTasks.Count * 100 : 0;
DelayRateText.Text = delayRate.ToString("F1") + "%";
// 推荐改进意见
if (delayRate > 30)
{
RecommendationText.Text = "任务延期率较高,建议重新评估任务优先级和时间安排";
}
else if (delayRate > 10)
{
RecommendationText.Text = "任务延期率适中,可适当优化时间管理";
}
else
{
RecommendationText.Text = "任务延期率较低,继续保持良好习惯";
}
// 任务类型分析
UpdateTaskTypeAnalysis(allTasks);
}
private void UpdateTaskTypeAnalysis(List<ItemGrid> allTasks)
{
TaskTypeAnalysisListBox.Items.Clear();
var typeGroups = allTasks
.GroupBy(t => t.Importance ?? "Unknown")
.Select(g => new
{
Category = g.Key,
Count = g.Count(),
Percentage = $"{(double)g.Count() / allTasks.Count * 100:F1}%"
})
.OrderByDescending(g => g.Count);
foreach (var group in typeGroups)
{
TaskTypeAnalysisListBox.Items.Add(group);
}
}
private void RefreshButton_Click(object sender, RoutedEventArgs e)
{
LoadStatistics();
}
private void ExportButton_Click(object sender, RoutedEventArgs e)
{
try
{
var saveFileDialog = new Microsoft.Win32.SaveFileDialog
{
FileName = $"TaskStatistics_{DateTime.Now:yyyyMMdd_HHmmss}.txt",
Filter = "文本文件|*.txt|所有文件|*.*"
};
if (saveFileDialog.ShowDialog() == true)
{
var allTasks = GetAllTasks();
var report = GenerateStatisticsReport(allTasks);
File.WriteAllText(saveFileDialog.FileName, report);
MessageBox.Show("统计报告已成功导出!", "导出成功", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
catch (Exception ex)
{
MessageBox.Show($"导出统计报告时发生错误: {ex.Message}", "导出错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private string GenerateStatisticsReport(List<ItemGrid> allTasks)
{
var report = new System.Text.StringBuilder();
report.AppendLine("任务统计报告");
report.AppendLine($"生成时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
report.AppendLine();
// 概览统计
report.AppendLine("=== 概览统计 ===");
report.AppendLine($"总任务数: {allTasks.Count}");
report.AppendLine($"已完成: {allTasks.Count(t => !t.IsActive)}");
report.AppendLine($"进行中: {allTasks.Count(t => t.IsActive)}");
report.AppendLine("完成率: " + (allTasks.Count > 0 ? ((double)allTasks.Count(t => !t.IsActive) / allTasks.Count * 100).ToString("F1") : 0) + "%");
report.AppendLine();
// 象限分布
report.AppendLine("=== 象限分布 ===");
var quadrantCounts = new Dictionary<string, int>
{
{"重要且紧急", 0},
{"重要不紧急", 0},
{"不重要但紧急", 0},
{"不重要不紧急", 0}
};
foreach (var task in allTasks)
{
string quadrant = DetermineQuadrant(task.Importance, task.Urgency);
quadrantCounts[quadrant]++;
}
int totalTasks = allTasks.Count;
report.AppendLine("重要且紧急: " + quadrantCounts["重要且紧急"] + " (" + (totalTasks > 0 ? ((double)quadrantCounts["重要且紧急"] / totalTasks * 100).ToString("F1") : 0) + "%)");
report.AppendLine("重要不紧急: " + quadrantCounts["重要不紧急"] + " (" + (totalTasks > 0 ? ((double)quadrantCounts["重要不紧急"] / totalTasks * 100).ToString("F1") : 0) + "%)");
report.AppendLine("不重要但紧急: " + quadrantCounts["不重要但紧急"] + " (" + (totalTasks > 0 ? ((double)quadrantCounts["不重要但紧急"] / totalTasks * 100).ToString("F1") : 0) + "%)");
report.AppendLine("不重要不紧急: " + quadrantCounts["不重要不紧急"] + " (" + (totalTasks > 0 ? ((double)quadrantCounts["不重要不紧急"] / totalTasks * 100).ToString("F1") : 0) + "%)");
report.AppendLine();
// 效率分析
report.AppendLine("=== 效率分析 ===");
var completedTasks = allTasks.Where(t => !t.IsActive && t.CompletionTime.HasValue);
if (completedTasks.Any())
{
var avgCompletionTime = completedTasks.Average(t => (t.CompletionTime.Value - t.CreatedDate).TotalDays);
report.AppendLine("平均完成时间: " + avgCompletionTime.ToString("F1") + " 天");
}
else
{
report.AppendLine("平均完成时间: 无完成任务");
}
int overdueTasks = allTasks.Count(t => t.IsActive && t.ReminderTime.HasValue && t.ReminderTime.Value < DateTime.Now);
double delayRate = allTasks.Count > 0 ? (double)overdueTasks / allTasks.Count * 100 : 0;
report.AppendLine("任务延期率: " + delayRate.ToString("F1") + "%");
report.AppendLine();
return report.ToString();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}