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
|
2025-08-28 09:20:15 +08:00
|
|
|
from zhenxun.ui.builders import charts as chart_builders
|
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
|
|
|
|
|
)
|
2025-08-28 09:20:15 +08:00
|
|
|
items = list(zip(data.category_data, data.data))
|
|
|
|
|
builder = chart_builders.bar_chart(
|
|
|
|
|
title=data.title, items=items, direction="horizontal"
|
2024-08-25 00:02:35 +08:00
|
|
|
)
|
2025-08-28 09:20:15 +08:00
|
|
|
if background_image_name:
|
|
|
|
|
builder.set_background_image(background_image_name)
|
2025-08-18 23:08:22 +08:00
|
|
|
|
2025-08-28 09:20:15 +08:00
|
|
|
return await ui.render(builder.build())
|