1
0
Эх сурвалжийг харах

feat: 完成数据导出功能

SongZihuan 3 жил өмнө
parent
commit
1722cefbb2

+ 4 - 4
tk_ui/admin_event.py

@@ -586,12 +586,12 @@ class CountThrowTimeEvent(AdminEventBase):
             res[name] = lst
         res['res_type'] = loc_type
 
-        return res
+        return res, loc_list
 
     def __init__(self, gb_station):
         super().__init__(gb_station)
         self.thread = None
-        self._program: Optional[admin_program.StatisticsBaseProgram] = None
+        self._program: Optional[admin_program.StatisticsTimeBaseProgram] = None
 
     def start(self, column: List, get_name: Callable, program):
         self.thread = TkThreading(self.func, column, get_name)
@@ -602,8 +602,8 @@ class CountThrowTimeEvent(AdminEventBase):
         return not self.thread.is_alive()
 
     def done_after_event(self):
-        res: Optional[Dict[str, str]] = self.thread.wait_event()
+        res: Optional[Tuple[Dict[str, str], List]] = self.thread.wait_event()
         if res is None:
             self.station.show_warning("数据分析", "数据获取时发生错误")
         else:
-            self._program.show_result(res)
+            self._program.show_result(res[0], res[1])

+ 1 - 1
tk_ui/admin_menu.py

@@ -208,7 +208,7 @@ class StatisticsMenu(AdminMenu):
     def __init__(self, station, win, color):
         super().__init__(station, win, color, "数据分析")
         self.btn: List[tk.Button] = [tk.Button(self.frame) for _ in range(6)]
-        self.btn_name = ["时段分析", "时间分析", "积分信用分析", "失信用户", "通过率"]
+        self.btn_name = ["时段分析", "积分信用分析", "失信用户", "通过率"]
 
     def conf_gui(self, color: str, n: int = 1):
         super().conf_gui(color, n)

+ 34 - 5
tk_ui/admin_program.py

@@ -1,9 +1,8 @@
 import abc
 import tkinter as tk
 import tkinter.ttk as ttk
-from tkinter.filedialog import askdirectory, askopenfilename
+from tkinter.filedialog import askdirectory, askopenfilename, asksaveasfilename
 
-from matplotlib import font_manager as fm
 from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
 from matplotlib.axes import Axes
 import numpy as np
@@ -1769,6 +1768,7 @@ class StatisticsTimeBaseProgram(AdminProgram):
         self.btn_hide = tk.Button(self.color_frame)
         self.color_show_dict = {}
         self.color_hide_dict = {}
+        self.export_lst = []
 
         self.export_btn = tk.Button(self.frame)
         self.refresh_btn = tk.Button(self.frame)
@@ -1908,8 +1908,15 @@ class StatisticsTimeBaseProgram(AdminProgram):
         self.legend_show[0]['variable'] = self.legend_show[1]
         self.legend_show[0].place(relx=0.21, rely=0.91, relwidth=0.15, relheight=0.08)
 
-    def export(self):
-        ...
+    def export(self, title, func: Callable):
+        path = asksaveasfilename(title='选择CSV文件保存位置', filetypes=[("CSV", ".csv")])
+        if not path.endswith(".csv"):
+            path += ".csv"
+        with open(path, "w") as f:
+            f.write(f"Hour, Count, {title}\n")
+            for i in self.export_lst:
+                f.write(f"{i[0]}, {i[1]}, {func(i)}\n")
+        self.station.show_msg("保存数据", f"数据导出成功\n保存位置:\n  {path}")
 
     def refresh(self):
         self.plt.cla()
@@ -1925,11 +1932,12 @@ class StatisticsTimeBaseProgram(AdminProgram):
         self.color_hide_dict = tmp
         self.update_listbox()
 
-    def show_result(self, res: Dict[str, any]):
+    def show_result(self, res: Dict[str, any], lst: List):
         bottom = np.zeros(24)
         label_num = [i for i in range(24)]
         label_str = [f"{i}" for i in range(24)]
         res_type_lst: List = res['res_type']
+        self.export_lst = lst
         for res_type in res_type_lst:
             res_count: Tuple[str] = res[res_type]
             if len(res_count) != 0:
@@ -1978,6 +1986,9 @@ class StatisticsTimeLocProgram(StatisticsTimeBaseProgram):
         event = tk_event.CountThrowTimeEvent(self.station).start(["Location"], lambda i: i[2], self)
         self.station.push_event(event)
 
+    def export(self, *_, **__):
+        super().export("Location", lambda i: i[2])
+
 
 class StatisticsTimeTypeProgram(StatisticsTimeBaseProgram):
     def __init__(self, station, win, color):
@@ -1993,6 +2004,9 @@ class StatisticsTimeTypeProgram(StatisticsTimeBaseProgram):
         event = tk_event.CountThrowTimeEvent(self.station).start(["GarbageType"], self.get_name, self)
         self.station.push_event(event)
 
+    def export(self, *_, **__):
+        super().export("Type", self.get_name)
+
     @staticmethod
     def get_name(i: Tuple):
         data: bytes = i[2]
@@ -2009,6 +2023,9 @@ class StatisticsTimeTypeLocProgram(StatisticsTimeBaseProgram):
         event = tk_event.CountThrowTimeEvent(self.station).start(["GarbageType", "Location"], self.get_name, self)
         self.station.push_event(event)
 
+    def export(self, *_, **__):
+        super().export("Type-Location", self.get_name)
+
     @staticmethod
     def get_name(i: Tuple):
         data: bytes = i[2]
@@ -2027,6 +2044,9 @@ class StatisticsTimeCheckResultProgram(StatisticsTimeBaseProgram):
         event = tk_event.CountThrowTimeEvent(self.station).start(["CheckResult"], self.get_name, self)
         self.station.push_event(event)
 
+    def export(self, *_, **__):
+        super().export("Result", self.get_name)
+
     @staticmethod
     def get_name(i: Tuple):
         data: bytes = i[2]
@@ -2043,6 +2063,9 @@ class StatisticsTimeCheckResultAndTypeProgram(StatisticsTimeBaseProgram):
         event = tk_event.CountThrowTimeEvent(self.station).start(["CheckResult", "GarbageType"], self.get_name, self)
         self.station.push_event(event)
 
+    def export(self, *_, **__):
+        super().export("Result-Location", self.get_name)
+
     @staticmethod
     def get_name(i: Tuple):
         data_1: bytes = i[2]
@@ -2061,6 +2084,9 @@ class StatisticsTimeCheckResultAndLocProgram(StatisticsTimeBaseProgram):
         event = tk_event.CountThrowTimeEvent(self.station).start(["CheckResult", "Location"], self.get_name, self)
         self.station.push_event(event)
 
+    def export(self, *_, **__):
+        super().export("Result-Type", self.get_name)
+
     @staticmethod
     def get_name(i: Tuple):
         data_1: bytes = i[2]
@@ -2078,6 +2104,9 @@ class StatisticsTimeDetailProgram(StatisticsTimeBaseProgram):
         event.start(["CheckResult", "GarbageType", "Location"], self.get_name, self)
         self.station.push_event(event)
 
+    def export(self, *_, **__):
+        super().export("Detail", self.get_name)
+
     @staticmethod
     def get_name(i: Tuple):
         data_1: bytes = i[2]