2024-10-12 11:03:24 +08:00
|
|
|
import os
|
2025-08-15 16:34:37 +08:00
|
|
|
from pathlib import Path
|
2024-10-12 11:03:24 +08:00
|
|
|
import random
|
|
|
|
|
|
2025-08-18 23:08:22 +08:00
|
|
|
from zhenxun import ui
|
|
|
|
|
from zhenxun.ui.models import BarChartData
|
2024-08-25 00:02:35 +08:00
|
|
|
|
|
|
|
|
from .models import Barh
|
|
|
|
|
|
2025-08-15 16:34:37 +08:00
|
|
|
BACKGROUND_PATH = (
|
|
|
|
|
Path() / "resources" / "themes" / "default" / "assets" / "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
|
2025-08-18 23:08:22 +08:00
|
|
|
async def barh(cls, data: Barh) -> bytes:
|
2024-08-25 00:02:35 +08:00
|
|
|
"""横向统计图"""
|
2025-08-18 23:08:22 +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
|
|
|
)
|
2025-08-18 23:08:22 +08:00
|
|
|
|
|
|
|
|
return await ui.render(chart_component)
|