graphing.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. """The dashboard page for the template."""
  2. import reflex as rx
  3. from ..states.pie_state import PieChartState
  4. from ..styles import *
  5. data_1 = [
  6. {"name": "Page A", "uv": 4000, "pv": 2400, "amt": 2400},
  7. {"name": "Page B", "uv": 3000, "pv": 1398, "amt": 2210},
  8. {"name": "Page C", "uv": 2000, "pv": 9800, "amt": 2290},
  9. {"name": "Page D", "uv": 2780, "pv": 3908, "amt": 2000},
  10. {"name": "Page E", "uv": 1890, "pv": 4800, "amt": 2181},
  11. {"name": "Page F", "uv": 2390, "pv": 3800, "amt": 2500},
  12. {"name": "Page G", "uv": 3490, "pv": 4300, "amt": 2100},
  13. ]
  14. data_1_show = """[
  15. {"name": "Page A", "uv": 4000, "pv": 2400, "amt": 2400},
  16. {"name": "Page B", "uv": 3000, "pv": 1398, "amt": 2210},
  17. {"name": "Page C", "uv": 2000, "pv": 9800, "amt": 2290},
  18. {"name": "Page D", "uv": 2780, "pv": 3908, "amt": 2000},
  19. {"name": "Page E", "uv": 1890, "pv": 4800, "amt": 2181},
  20. {"name": "Page F", "uv": 2390, "pv": 3800, "amt": 2500},
  21. {"name": "Page G", "uv": 3490, "pv": 4300, "amt": 2100},
  22. ]"""
  23. graph_1_code = """rx.recharts.composed_chart(
  24. rx.recharts.area(
  25. data_key="uv", stroke="#8884d8", fill="#8884d8"
  26. ),
  27. rx.recharts.bar(
  28. data_key="amt", bar_size=20, fill="#413ea0"
  29. ),
  30. rx.recharts.line(
  31. data_key="pv", type_="monotone", stroke="#ff7300"
  32. ),
  33. rx.recharts.x_axis(data_key="name"),
  34. rx.recharts.y_axis(),
  35. rx.recharts.cartesian_grid(stroke_dasharray="3 3"),
  36. rx.recharts.graphing_tooltip(),
  37. data=data,
  38. )"""
  39. graph_2_code = """rx.recharts.pie_chart(
  40. rx.recharts.pie(
  41. data=PieChartState.resources,
  42. data_key="count",
  43. name_key="type_",
  44. cx="50%",
  45. cy="50%",
  46. start_angle=180,
  47. end_angle=0,
  48. fill="#8884d8",
  49. label=True,
  50. ),
  51. rx.recharts.graphing_tooltip(),
  52. ),
  53. rx.vstack(
  54. rx.foreach(
  55. PieChartState.resource_types,
  56. lambda type_, i: rx.hstack(
  57. rx.button(
  58. "-",
  59. on_click=PieChartState.decrement(type_),
  60. ),
  61. rx.text(
  62. type_,
  63. PieChartState.resources[i]["count"],
  64. ),
  65. rx.button(
  66. "+",
  67. on_click=PieChartState.increment(type_),
  68. ),
  69. ),
  70. ),
  71. )"""
  72. graph_2_state = """class PieChartState(rx.State):
  73. resources: list[dict[str, Any]] = [
  74. dict(type_="🏆", count=1),
  75. dict(type_="🪵", count=1),
  76. dict(type_="🥑", count=1),
  77. dict(type_="🧱", count=1),
  78. ]
  79. @rx.cached_var
  80. def resource_types(self) -> list[str]:
  81. return [r["type_"] for r in self.resources]
  82. def increment(self, type_: str):
  83. for resource in self.resources:
  84. if resource["type_"] == type_:
  85. resource["count"] += 1
  86. break
  87. def decrement(self, type_: str):
  88. for resource in self.resources:
  89. if (
  90. resource["type_"] == type_
  91. and resource["count"] > 0
  92. ):
  93. resource["count"] -= 1
  94. break
  95. """
  96. def graphing_page() -> rx.Component:
  97. """The UI for the dashboard page.
  98. Returns:
  99. rx.Component: The UI for the dashboard page.
  100. """
  101. return rx.box(
  102. rx.vstack(
  103. rx.heading(
  104. "Graphing Demo",
  105. font_size="3em",
  106. ),
  107. rx.heading(
  108. "Composed Chart",
  109. font_size="2em",
  110. ),
  111. rx.stack(
  112. rx.recharts.composed_chart(
  113. rx.recharts.area(data_key="uv", stroke="#8884d8", fill="#8884d8"),
  114. rx.recharts.bar(data_key="amt", bar_size=20, fill="#413ea0"),
  115. rx.recharts.line(data_key="pv", type_="monotone", stroke="#ff7300"),
  116. rx.recharts.x_axis(data_key="name"),
  117. rx.recharts.y_axis(),
  118. rx.recharts.cartesian_grid(stroke_dasharray="3 3"),
  119. rx.recharts.graphing_tooltip(),
  120. data=data_1,
  121. # height="15em",
  122. ),
  123. width="100%",
  124. height="20em",
  125. ),
  126. rx.tabs(
  127. rx.tab_list(
  128. rx.tab("Code", style=tab_style),
  129. rx.tab("Data", style=tab_style),
  130. padding_x=0,
  131. ),
  132. rx.tab_panels(
  133. rx.tab_panel(
  134. rx.code_block(
  135. graph_1_code,
  136. language="python",
  137. show_line_numbers=True,
  138. ),
  139. width="100%",
  140. padding_x=0,
  141. padding_y=".25em",
  142. ),
  143. rx.tab_panel(
  144. rx.code_block(
  145. data_1_show,
  146. language="python",
  147. show_line_numbers=True,
  148. ),
  149. width="100%",
  150. padding_x=0,
  151. padding_y=".25em",
  152. ),
  153. width="100%",
  154. ),
  155. variant="unstyled",
  156. color_scheme="purple",
  157. align="end",
  158. width="100%",
  159. padding_top=".5em",
  160. ),
  161. rx.heading("Interactive Example", font_size="2em"),
  162. rx.hstack(
  163. rx.recharts.pie_chart(
  164. rx.recharts.pie(
  165. data=PieChartState.resources,
  166. data_key="count",
  167. name_key="type_",
  168. cx="50%",
  169. cy="50%",
  170. start_angle=180,
  171. end_angle=0,
  172. fill="#8884d8",
  173. label=True,
  174. ),
  175. rx.recharts.graphing_tooltip(),
  176. ),
  177. rx.vstack(
  178. rx.foreach(
  179. PieChartState.resource_types,
  180. lambda type_, i: rx.hstack(
  181. rx.button(
  182. "-",
  183. on_click=PieChartState.decrement(type_),
  184. ),
  185. rx.text(
  186. type_,
  187. PieChartState.resources[i]["count"],
  188. ),
  189. rx.button(
  190. "+",
  191. on_click=PieChartState.increment(type_),
  192. ),
  193. ),
  194. ),
  195. ),
  196. width="100%",
  197. height="15em",
  198. ),
  199. rx.tabs(
  200. rx.tab_list(
  201. rx.tab("Code", style=tab_style),
  202. rx.tab("State", style=tab_style),
  203. padding_x=0,
  204. ),
  205. rx.tab_panels(
  206. rx.tab_panel(
  207. rx.code_block(
  208. graph_2_code,
  209. language="python",
  210. show_line_numbers=True,
  211. ),
  212. width="100%",
  213. padding_x=0,
  214. padding_y=".25em",
  215. ),
  216. rx.tab_panel(
  217. rx.code_block(
  218. graph_2_state,
  219. language="python",
  220. show_line_numbers=True,
  221. ),
  222. width="100%",
  223. padding_x=0,
  224. padding_y=".25em",
  225. ),
  226. width="100%",
  227. ),
  228. variant="unstyled",
  229. color_scheme="purple",
  230. align="end",
  231. width="100%",
  232. padding_top=".5em",
  233. ),
  234. style=template_content_style,
  235. min_h="100vh",
  236. ),
  237. style=template_page_style,
  238. min_h="100vh",
  239. )