libs.commands.report.monthly
libs/commands/report/monthly.py
1""" 2libs/commands/report/monthly.py 3""" 4 5import os 6 7import matplotlib.font_manager as fm 8import matplotlib.pyplot as plt 9 10import libs.global_value as g 11from libs.data import loader 12from libs.functions import configuration, message 13 14 15def plot(): 16 """月別ゲーム統計表の生成 17 18 Returns: 19 str: 生成ファイルパス 20 """ 21 22 plt.close() 23 # --- データ収集 24 df = loader.read_data("report/monthly.sql") 25 results = df.transpose().to_dict() 26 27 if len(results) == 0: 28 return False 29 30 # --- グラフフォント設定 31 configuration.graph_setup(plt, fm) 32 plt.rcParams["font.size"] = 6 33 34 # 色彩設定 35 match (plt.rcParams["text.color"], plt.rcParams["figure.facecolor"]): 36 case text_color, bg_color if text_color == "black" and bg_color == "white": 37 line_color1 = "#ffffff" 38 line_color2 = "#dddddd" 39 case text_color, bg_color if text_color == "white" and bg_color == "black": 40 line_color1 = "#000000" 41 line_color2 = "#111111" 42 case _: 43 line_color1 = plt.rcParams["figure.facecolor"] 44 line_color2 = plt.rcParams["figure.facecolor"] 45 46 column_labels = list(results[list(results.keys())[0]].keys()) 47 column_color = ["#000080" for i in column_labels] 48 49 cell_param = [] 50 cell_color = [] 51 line_count = 0 52 for x in results.keys(): 53 line_count += 1 54 cell_param.append([results[x][y] for y in column_labels]) 55 if int(line_count % 2): 56 cell_color.append([line_color1 for i in column_labels]) 57 else: 58 cell_color.append([line_color2 for i in column_labels]) 59 60 report_file_path = os.path.join( 61 g.cfg.setting.work_dir, 62 f"{g.params["filename"]}.png" if g.params.get("filename") else "report.png", 63 ) 64 65 fig = plt.figure( 66 figsize=(6, (len(results) * 0.2) + 0.8), 67 dpi=200, 68 tight_layout=True 69 ) 70 ax_dummy = fig.add_subplot(111) 71 ax_dummy.axis("off") 72 73 plt.title("月別ゲーム統計", fontsize=12) 74 tb = plt.table( 75 colLabels=column_labels, 76 colColours=column_color, 77 cellText=cell_param, 78 cellColours=cell_color, 79 loc="center", 80 ) 81 82 tb.auto_set_font_size(False) 83 for i in range(len(column_labels)): 84 tb[0, i].set_text_props(color="#FFFFFF", weight="bold") 85 for i in range(len(results.keys()) + 1): 86 tb[i, 0].set_text_props(ha="center") 87 88 # 追加テキスト 89 fig.text( 90 0.01, 0.02, # 表示位置(左下0,0 右下0,1) 91 f"[{message.item_search_range().strip()}] [特記:すべてのゲーム結果を含む]", 92 transform=fig.transFigure, 93 fontsize=6, 94 ) 95 96 fig.savefig(report_file_path) 97 plt.close() 98 99 return report_file_path
def
plot():
16def plot(): 17 """月別ゲーム統計表の生成 18 19 Returns: 20 str: 生成ファイルパス 21 """ 22 23 plt.close() 24 # --- データ収集 25 df = loader.read_data("report/monthly.sql") 26 results = df.transpose().to_dict() 27 28 if len(results) == 0: 29 return False 30 31 # --- グラフフォント設定 32 configuration.graph_setup(plt, fm) 33 plt.rcParams["font.size"] = 6 34 35 # 色彩設定 36 match (plt.rcParams["text.color"], plt.rcParams["figure.facecolor"]): 37 case text_color, bg_color if text_color == "black" and bg_color == "white": 38 line_color1 = "#ffffff" 39 line_color2 = "#dddddd" 40 case text_color, bg_color if text_color == "white" and bg_color == "black": 41 line_color1 = "#000000" 42 line_color2 = "#111111" 43 case _: 44 line_color1 = plt.rcParams["figure.facecolor"] 45 line_color2 = plt.rcParams["figure.facecolor"] 46 47 column_labels = list(results[list(results.keys())[0]].keys()) 48 column_color = ["#000080" for i in column_labels] 49 50 cell_param = [] 51 cell_color = [] 52 line_count = 0 53 for x in results.keys(): 54 line_count += 1 55 cell_param.append([results[x][y] for y in column_labels]) 56 if int(line_count % 2): 57 cell_color.append([line_color1 for i in column_labels]) 58 else: 59 cell_color.append([line_color2 for i in column_labels]) 60 61 report_file_path = os.path.join( 62 g.cfg.setting.work_dir, 63 f"{g.params["filename"]}.png" if g.params.get("filename") else "report.png", 64 ) 65 66 fig = plt.figure( 67 figsize=(6, (len(results) * 0.2) + 0.8), 68 dpi=200, 69 tight_layout=True 70 ) 71 ax_dummy = fig.add_subplot(111) 72 ax_dummy.axis("off") 73 74 plt.title("月別ゲーム統計", fontsize=12) 75 tb = plt.table( 76 colLabels=column_labels, 77 colColours=column_color, 78 cellText=cell_param, 79 cellColours=cell_color, 80 loc="center", 81 ) 82 83 tb.auto_set_font_size(False) 84 for i in range(len(column_labels)): 85 tb[0, i].set_text_props(color="#FFFFFF", weight="bold") 86 for i in range(len(results.keys()) + 1): 87 tb[i, 0].set_text_props(ha="center") 88 89 # 追加テキスト 90 fig.text( 91 0.01, 0.02, # 表示位置(左下0,0 右下0,1) 92 f"[{message.item_search_range().strip()}] [特記:すべてのゲーム結果を含む]", 93 transform=fig.transFigure, 94 fontsize=6, 95 ) 96 97 fig.savefig(report_file_path) 98 plt.close() 99 100 return report_file_path
月別ゲーム統計表の生成
Returns:
str: 生成ファイルパス