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
|
2026-02-06 20:13:40 +08:00
|
|
|
from zhenxun.ui.models.charts import EChartsData
|
2024-08-25 00:02:35 +08:00
|
|
|
|
|
|
|
|
from .models import Barh
|
|
|
|
|
|
2025-08-15 16:34:37 +08:00
|
|
|
BACKGROUND_PATH = (
|
2025-08-28 09:20:15 +08:00
|
|
|
Path() / "resources" / "themes" / "default" / "assets" / "ui" / "background"
|
2025-08-15 16:34:37 +08:00
|
|
|
)
|
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
|
|
|
|
|
)
|
2026-02-06 20:13:40 +08:00
|
|
|
|
2025-08-28 09:20:15 +08:00
|
|
|
items = list(zip(data.category_data, data.data))
|
2026-02-06 20:13:40 +08:00
|
|
|
|
|
|
|
|
chart_data = EChartsData.bar_chart(
|
|
|
|
|
title=data.title,
|
|
|
|
|
items=items, # type: ignore
|
|
|
|
|
direction="horizontal",
|
|
|
|
|
background_image=background_image_name,
|
2024-08-25 00:02:35 +08:00
|
|
|
)
|
2025-08-18 23:08:22 +08:00
|
|
|
|
2026-02-06 20:13:40 +08:00
|
|
|
return await ui.render(chart_data)
|