2024-10-12 11:03:24 +08:00
|
|
|
import os
|
|
|
|
|
import random
|
|
|
|
|
|
2024-08-25 00:02:35 +08:00
|
|
|
from nonebot_plugin_htmlrender import template_to_pic
|
|
|
|
|
|
2024-10-12 11:03:24 +08:00
|
|
|
from zhenxun.configs.path_config import TEMPLATE_PATH
|
2024-12-10 19:49:11 +08:00
|
|
|
from zhenxun.utils._build_image import BuildImage
|
2024-08-25 00:02:35 +08:00
|
|
|
|
|
|
|
|
from .models import Barh
|
|
|
|
|
|
2024-10-12 11:03:24 +08:00
|
|
|
BACKGROUND_PATH = TEMPLATE_PATH / "bar_chart" / "background"
|
2024-08-25 00:02:35 +08:00
|
|
|
|
|
|
|
|
|
2024-10-12 11:03:24 +08:00
|
|
|
class ChartUtils:
|
2024-08-25 00:02:35 +08:00
|
|
|
@classmethod
|
|
|
|
|
async def barh(cls, data: Barh) -> BuildImage:
|
|
|
|
|
"""横向统计图"""
|
2025-01-07 14:20:30 +08:00
|
|
|
to_json = data.to_dict()
|
2024-10-12 11:03:24 +08:00
|
|
|
to_json["background_image"] = (
|
|
|
|
|
f"./background/{random.choice(os.listdir(BACKGROUND_PATH))}"
|
|
|
|
|
)
|
2024-08-25 00:02:35 +08:00
|
|
|
pic = await template_to_pic(
|
|
|
|
|
template_path=str((TEMPLATE_PATH / "bar_chart").absolute()),
|
|
|
|
|
template_name="main.html",
|
2024-10-12 11:03:24 +08:00
|
|
|
templates={"data": to_json},
|
2024-08-25 00:02:35 +08:00
|
|
|
pages={
|
2024-10-12 11:03:24 +08:00
|
|
|
"viewport": {"width": 1000, "height": 1000},
|
2024-08-25 00:02:35 +08:00
|
|
|
"base_url": f"file://{TEMPLATE_PATH}",
|
|
|
|
|
},
|
|
|
|
|
wait=2,
|
|
|
|
|
)
|
|
|
|
|
return BuildImage.open(pic)
|