zhenxun_bot/zhenxun/utils/echart_utils/__init__.py

33 lines
818 B
Python
Raw Normal View History

import os
from pathlib import Path
import random
from zhenxun import ui
from zhenxun.ui.models import BarChartData
2024-08-25 00:02:35 +08:00
from .models import Barh
BACKGROUND_PATH = (
Path() / "resources" / "themes" / "default" / "assets" / "bar_chart" / "background"
)
2024-08-25 00:02:35 +08:00
class ChartUtils:
2024-08-25 00:02:35 +08:00
@classmethod
async def barh(cls, data: Barh) -> bytes:
2024-08-25 00:02:35 +08:00
"""横向统计图"""
background_image_name = (
random.choice(os.listdir(BACKGROUND_PATH))
if BACKGROUND_PATH.exists()
else None
)
chart_component = BarChartData(
title=data.title,
category_data=data.category_data,
data=data.data,
background_image=background_image_name,
direction="horizontal",
2024-08-25 00:02:35 +08:00
)
return await ui.render(chart_component)