libs.commands.report.monthly
libs/commands/report/monthly.py
1""" 2libs/commands/report/monthly.py 3""" 4 5from typing import TYPE_CHECKING 6 7import matplotlib.pyplot as plt 8 9import libs.global_value as g 10from libs.functions import message 11from libs.functions.compose import text_item 12from libs.types import StyleOptions 13from libs.utils import graphutil, textutil 14 15if TYPE_CHECKING: 16 from integrations.protocols import MessageParserProtocol 17 18 19def plot(m: "MessageParserProtocol") -> None: 20 """ 21 月別ゲーム統計表の生成 22 23 Args: 24 m (MessageParserProtocol): メッセージデータ 25 26 """ 27 # --- データ収集 28 title: str = "月別ゲーム統計" 29 df = g.params.read_data("REPORT_MONTHLY") 30 results = df.transpose().to_dict() 31 32 if len(results) == 0: 33 m.set_headline(message.random_reply(m, "no_hits"), StyleOptions(title=title)) 34 m.status.result = False 35 return 36 37 # --- グラフ設定 38 report_file_path = textutil.save_file_path("report.png") 39 match g.adapter.conf.plotting_backend: 40 case "plotly": 41 pass 42 case _: 43 graphutil.setup() 44 45 # グラフフォント設定 46 plt.rcParams["font.size"] = 6 47 48 # 色彩設定 49 match (plt.rcParams["text.color"], plt.rcParams["figure.facecolor"]): 50 case text_color, bg_color if text_color == "black" and bg_color == "white": 51 line_color1 = "#ffffff" 52 line_color2 = "#dddddd" 53 case text_color, bg_color if text_color == "white" and bg_color == "black": 54 line_color1 = "#000000" 55 line_color2 = "#111111" 56 case _: 57 line_color1 = plt.rcParams["figure.facecolor"] 58 line_color2 = plt.rcParams["figure.facecolor"] 59 60 column_labels = list(results[list(results.keys())[0]].keys()) 61 column_color = ["#000080" for i in column_labels] 62 63 cell_param = [] 64 cell_color = [] 65 line_count = 0 66 for x in results.keys(): 67 line_count += 1 68 cell_param.append([results[x][y] for y in column_labels]) 69 if int(line_count % 2): 70 cell_color.append([line_color1 for i in column_labels]) 71 else: 72 cell_color.append([line_color2 for i in column_labels]) 73 74 fig = plt.figure(figsize=(6, (len(results) * 0.2) + 0.8), dpi=200, tight_layout=True) 75 ax_dummy = fig.add_subplot(111) 76 ax_dummy.axis("off") 77 78 plt.title(title, fontsize=12) 79 tb = plt.table( 80 colLabels=column_labels, 81 colColours=column_color, 82 cellText=cell_param, 83 cellColours=cell_color, 84 loc="center", 85 ) 86 87 tb.auto_set_font_size(False) 88 for i in range(len(column_labels)): 89 tb[0, i].set_text_props(color="#FFFFFF", weight="bold") 90 for i in range(len(results.keys()) + 1): 91 tb[i, 0].set_text_props(ha="center") 92 93 # 追加テキスト 94 fig.text( 95 0.01, 96 0.02, # 表示位置(左下0,0 右下0,1) 97 f"[検索範囲:{text_item.search_range()}] [特記:すべてのゲーム結果を含む]", 98 transform=fig.transFigure, 99 fontsize=6, 100 ) 101 102 fig.savefig(report_file_path) 103 104 match g.adapter.interface_type: 105 case "slack" | "discord": 106 m.set_message(report_file_path, StyleOptions(title=title, use_comment=True, header_hidden=True)) 107 case "web": 108 m.set_message(df, StyleOptions(title=title)) 109 case _: 110 m.set_message(df, StyleOptions(title=title))
20def plot(m: "MessageParserProtocol") -> None: 21 """ 22 月別ゲーム統計表の生成 23 24 Args: 25 m (MessageParserProtocol): メッセージデータ 26 27 """ 28 # --- データ収集 29 title: str = "月別ゲーム統計" 30 df = g.params.read_data("REPORT_MONTHLY") 31 results = df.transpose().to_dict() 32 33 if len(results) == 0: 34 m.set_headline(message.random_reply(m, "no_hits"), StyleOptions(title=title)) 35 m.status.result = False 36 return 37 38 # --- グラフ設定 39 report_file_path = textutil.save_file_path("report.png") 40 match g.adapter.conf.plotting_backend: 41 case "plotly": 42 pass 43 case _: 44 graphutil.setup() 45 46 # グラフフォント設定 47 plt.rcParams["font.size"] = 6 48 49 # 色彩設定 50 match (plt.rcParams["text.color"], plt.rcParams["figure.facecolor"]): 51 case text_color, bg_color if text_color == "black" and bg_color == "white": 52 line_color1 = "#ffffff" 53 line_color2 = "#dddddd" 54 case text_color, bg_color if text_color == "white" and bg_color == "black": 55 line_color1 = "#000000" 56 line_color2 = "#111111" 57 case _: 58 line_color1 = plt.rcParams["figure.facecolor"] 59 line_color2 = plt.rcParams["figure.facecolor"] 60 61 column_labels = list(results[list(results.keys())[0]].keys()) 62 column_color = ["#000080" for i in column_labels] 63 64 cell_param = [] 65 cell_color = [] 66 line_count = 0 67 for x in results.keys(): 68 line_count += 1 69 cell_param.append([results[x][y] for y in column_labels]) 70 if int(line_count % 2): 71 cell_color.append([line_color1 for i in column_labels]) 72 else: 73 cell_color.append([line_color2 for i in column_labels]) 74 75 fig = plt.figure(figsize=(6, (len(results) * 0.2) + 0.8), dpi=200, tight_layout=True) 76 ax_dummy = fig.add_subplot(111) 77 ax_dummy.axis("off") 78 79 plt.title(title, fontsize=12) 80 tb = plt.table( 81 colLabels=column_labels, 82 colColours=column_color, 83 cellText=cell_param, 84 cellColours=cell_color, 85 loc="center", 86 ) 87 88 tb.auto_set_font_size(False) 89 for i in range(len(column_labels)): 90 tb[0, i].set_text_props(color="#FFFFFF", weight="bold") 91 for i in range(len(results.keys()) + 1): 92 tb[i, 0].set_text_props(ha="center") 93 94 # 追加テキスト 95 fig.text( 96 0.01, 97 0.02, # 表示位置(左下0,0 右下0,1) 98 f"[検索範囲:{text_item.search_range()}] [特記:すべてのゲーム結果を含む]", 99 transform=fig.transFigure, 100 fontsize=6, 101 ) 102 103 fig.savefig(report_file_path) 104 105 match g.adapter.interface_type: 106 case "slack" | "discord": 107 m.set_message(report_file_path, StyleOptions(title=title, use_comment=True, header_hidden=True)) 108 case "web": 109 m.set_message(df, StyleOptions(title=title)) 110 case _: 111 m.set_message(df, StyleOptions(title=title))
月別ゲーム統計表の生成
Arguments:
- m (MessageParserProtocol): メッセージデータ