2025-08-15 16:34:37 +08:00
|
|
|
from typing import Literal
|
2025-08-18 23:08:22 +08:00
|
|
|
import uuid
|
2025-08-15 16:34:37 +08:00
|
|
|
|
2025-08-18 23:08:22 +08:00
|
|
|
from pydantic import BaseModel, Field
|
2025-08-15 16:34:37 +08:00
|
|
|
|
2025-08-18 23:08:22 +08:00
|
|
|
from .core.base import RenderableComponent
|
2025-08-15 16:34:37 +08:00
|
|
|
|
2025-08-18 23:08:22 +08:00
|
|
|
|
|
|
|
|
class BaseChartData(RenderableComponent):
|
2025-08-15 16:34:37 +08:00
|
|
|
"""所有图表数据模型的基类"""
|
|
|
|
|
|
|
|
|
|
style_name: str | None = None
|
|
|
|
|
title: str
|
2025-08-18 23:08:22 +08:00
|
|
|
chart_id: str = Field(default_factory=lambda: f"chart-{uuid.uuid4().hex}")
|
|
|
|
|
|
|
|
|
|
def get_required_scripts(self) -> list[str]:
|
|
|
|
|
"""声明此组件需要 ECharts 库。"""
|
|
|
|
|
return ["js/echarts.min.js"]
|
2025-08-15 16:34:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class BarChartData(BaseChartData):
|
|
|
|
|
"""柱状图(支持横向和竖向)的数据模型"""
|
|
|
|
|
|
|
|
|
|
category_data: list[str]
|
|
|
|
|
data: list[int | float]
|
|
|
|
|
direction: Literal["horizontal", "vertical"] = "horizontal"
|
|
|
|
|
background_image: str | None = None
|
|
|
|
|
|
2025-08-18 23:08:22 +08:00
|
|
|
@property
|
|
|
|
|
def template_name(self) -> str:
|
|
|
|
|
return "components/charts/bar_chart"
|
|
|
|
|
|
2025-08-15 16:34:37 +08:00
|
|
|
|
|
|
|
|
class PieChartDataItem(BaseModel):
|
|
|
|
|
name: str
|
|
|
|
|
value: int | float
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PieChartData(BaseChartData):
|
|
|
|
|
"""饼图的数据模型"""
|
|
|
|
|
|
|
|
|
|
data: list[PieChartDataItem]
|
|
|
|
|
|
2025-08-18 23:08:22 +08:00
|
|
|
@property
|
|
|
|
|
def template_name(self) -> str:
|
|
|
|
|
return "components/charts/pie_chart"
|
|
|
|
|
|
2025-08-15 16:34:37 +08:00
|
|
|
|
|
|
|
|
class LineChartSeries(BaseModel):
|
|
|
|
|
name: str
|
|
|
|
|
data: list[int | float]
|
|
|
|
|
smooth: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class LineChartData(BaseChartData):
|
|
|
|
|
"""折线图的数据模型"""
|
|
|
|
|
|
|
|
|
|
category_data: list[str]
|
|
|
|
|
series: list[LineChartSeries]
|
2025-08-18 23:08:22 +08:00
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def template_name(self) -> str:
|
|
|
|
|
return "components/charts/line_chart"
|