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

月別ゲーム統計表の生成

Arguments:
  • m (MessageParserProtocol): メッセージデータ