Skip to content

Commit 97cd97e

Browse files
author
tim
committed
init chart component - line chart
1 parent 398147e commit 97cd97e

File tree

11 files changed

+459
-7
lines changed

11 files changed

+459
-7
lines changed

demo/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from httpx import AsyncClient
1212

1313
from .auth import router as auth_router
14+
from .charts import router as charts_router
1415
from .components_list import router as components_router
1516
from .forms import router as forms_router
1617
from .main import router as main_router
@@ -38,6 +39,7 @@ async def lifespan(app_: FastAPI):
3839
app.include_router(table_router, prefix='/api/table')
3940
app.include_router(forms_router, prefix='/api/forms')
4041
app.include_router(auth_router, prefix='/api/auth')
42+
app.include_router(charts_router, prefix='/api/charts')
4143
app.include_router(main_router, prefix='/api')
4244

4345

demo/charts.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from __future__ import annotations as _annotations
2+
3+
from fastapi import APIRouter
4+
from fastui import AnyComponent, FastUI
5+
from fastui import components as c
6+
from fastui.components import RechartsLineChart
7+
from fastui.events import PageEvent
8+
from pydantic import BaseModel
9+
10+
from .shared import demo_page
11+
12+
router = APIRouter()
13+
14+
15+
@router.get('/{kind}', response_model=FastUI, response_model_exclude_none=True)
16+
def charts_view(kind: str) -> list[AnyComponent]:
17+
return demo_page(
18+
c.LinkList(
19+
links=[
20+
c.Link(
21+
components=[c.Text(text='Recharts Line Chart')],
22+
on_click=PageEvent(
23+
name='change-chart',
24+
push_path='/charts/recharts-line-chart',
25+
context={'kind': 'recharts-line-chart'},
26+
),
27+
active='/charts/recharts-line-chart',
28+
),
29+
],
30+
mode='tabs',
31+
class_name='+ mb-4',
32+
),
33+
c.ServerLoad(
34+
path='/charts/content/{kind}',
35+
load_trigger=PageEvent(name='change-chart'),
36+
components=charts_content_view(kind),
37+
),
38+
title='Charts',
39+
)
40+
41+
42+
class Data(BaseModel):
43+
name: str
44+
uv: int
45+
pv: int
46+
amt: int
47+
48+
49+
data_list = [
50+
Data(name='Page A', uv=4000, pv=2400, amt=2400),
51+
Data(name='Page B', uv=3000, pv=1398, amt=2210),
52+
Data(name='Page C', uv=2000, pv=9800, amt=2290),
53+
Data(name='Page D', uv=2780, pv=3908, amt=2000),
54+
Data(name='Page E', uv=1890, pv=4800, amt=2181),
55+
Data(name='Page F', uv=2390, pv=3800, amt=2500),
56+
Data(name='Page G', uv=3490, pv=4300, amt=2100),
57+
]
58+
59+
60+
@router.get('/content/{kind}', response_model=FastUI, response_model_exclude_none=True)
61+
def charts_content_view(kind: str) -> list[AnyComponent]:
62+
match kind:
63+
case 'recharts-line-chart':
64+
return [
65+
c.Heading(text='Line chart', level=2),
66+
c.Paragraph(text='Line chart with Recharts.'),
67+
RechartsLineChart(
68+
title='Recharts Line Chart',
69+
width='100%',
70+
height=300,
71+
data=data_list,
72+
xKey='name',
73+
yKeys=['pv', 'uv', 'amt'],
74+
yKeysNames=['Page Views', 'Unique Views', 'Amount'],
75+
colors=['#8884d8', '#82ca9d', '#ffc658'],
76+
),
77+
]
78+
case _:
79+
return [c.Text(text='Unknown chart kind')]

demo/shared.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ def demo_page(*components: AnyComponent, title: str | None = None) -> list[AnyCo
3232
on_click=GoToEvent(url='/forms/login'),
3333
active='startswith:/forms',
3434
),
35+
c.Link(
36+
components=[c.Text(text='Charts')],
37+
on_click=GoToEvent(url='/charts/recharts-line-chart'),
38+
active='startswith:/charts',
39+
),
3540
],
3641
),
3742
c.Page(

demo/tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_api_root(client: TestClient):
3232
{
3333
'title': 'FastUI Demo',
3434
'titleEvent': {'url': '/', 'type': 'go-to'},
35-
'startLinks': IsList(length=4),
35+
'startLinks': IsList(length=5),
3636
'endLinks': [],
3737
'type': 'Navbar',
3838
},

0 commit comments

Comments
 (0)