charts.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. """A module that defines the chart components in Recharts."""
  2. from __future__ import annotations
  3. from collections.abc import Sequence
  4. from typing import Any, ClassVar
  5. from reflex.components.component import Component
  6. from reflex.components.recharts.general import ResponsiveContainer
  7. from reflex.constants import EventTriggers
  8. from reflex.constants.colors import Color
  9. from reflex.event import EventHandler, no_args_event_spec
  10. from reflex.vars.base import Var
  11. from .recharts import (
  12. LiteralAnimationEasing,
  13. LiteralComposedChartBaseValue,
  14. LiteralLayout,
  15. LiteralStackOffset,
  16. LiteralSyncMethod,
  17. RechartsCharts,
  18. )
  19. class ChartBase(RechartsCharts):
  20. """A component that wraps a Recharts charts."""
  21. # The width of chart container. String or Integer
  22. width: Var[str | int] = Var.create("100%")
  23. # The height of chart container.
  24. height: Var[str | int] = Var.create("100%")
  25. # The customized event handler of click on the component in this chart
  26. on_click: EventHandler[no_args_event_spec]
  27. # The customized event handler of mouseenter on the component in this chart
  28. on_mouse_enter: EventHandler[no_args_event_spec]
  29. # The customized event handler of mousemove on the component in this chart
  30. on_mouse_move: EventHandler[no_args_event_spec]
  31. # The customized event handler of mouseleave on the component in this chart
  32. on_mouse_leave: EventHandler[no_args_event_spec]
  33. @staticmethod
  34. def _ensure_valid_dimension(name: str, value: Any) -> None:
  35. """Ensure that the value is an int type or str percentage.
  36. Unfortunately str Vars cannot be checked and are implicitly not allowed.
  37. Args:
  38. name: The name of the prop.
  39. value: The value to check.
  40. Raises:
  41. ValueError: If the value is not an int type or str percentage.
  42. """
  43. if value is None:
  44. return
  45. if isinstance(value, int):
  46. return
  47. if isinstance(value, str) and value.endswith("%"):
  48. return
  49. if isinstance(value, Var) and issubclass(value._var_type, int):
  50. return
  51. msg = (
  52. f"Chart {name} must be specified as int pixels or percentage, not {value!r}. "
  53. "CSS unit dimensions are allowed on parent container."
  54. )
  55. raise ValueError(msg)
  56. @classmethod
  57. def create(cls, *children: Any, **props: Any) -> Component:
  58. """Create a chart component.
  59. Args:
  60. *children: The children of the chart component.
  61. **props: The properties of the chart component.
  62. Returns:
  63. The chart component wrapped in a responsive container.
  64. """
  65. width = props.pop("width", None)
  66. height = props.pop("height", None)
  67. cls._ensure_valid_dimension("width", width)
  68. cls._ensure_valid_dimension("height", height)
  69. # Ensure that the min_height and min_width are set to prevent the chart from collapsing.
  70. # We are using small values so that height and width can still be used over min_height and min_width.
  71. # Without this, sometimes the chart will not be visible. Causing confusion to the user.
  72. # With this, the user will see a small chart and can adjust the height and width and can figure out that the issue is with the size.
  73. min_height = props.pop("min_height", 10)
  74. min_width = props.pop("min_width", 10)
  75. return ResponsiveContainer.create(
  76. super().create(*children, **props),
  77. width=width if width is not None else "100%",
  78. height=height if height is not None else "100%",
  79. min_width=min_width,
  80. min_height=min_height,
  81. )
  82. class CategoricalChartBase(ChartBase):
  83. """A component that wraps a Categorical Recharts charts."""
  84. # The source data, in which each element is an object.
  85. data: Var[Sequence[dict[str, Any]]]
  86. # The sizes of whitespace around the chart, i.e. {"top": 50, "right": 30, "left": 20, "bottom": 5}.
  87. margin: Var[dict[str, Any]]
  88. # If any two categorical charts(rx.line_chart, rx.area_chart, rx.bar_chart, rx.composed_chart) have the same sync_id, these two charts can sync the position GraphingTooltip, and the start_index, end_index of Brush.
  89. sync_id: Var[str]
  90. # When sync_id is provided, allows customisation of how the charts will synchronize GraphingTooltips and brushes. Using 'index' (default setting), other charts will reuse current datum's index within the data array. In cases where data does not have the same length, this might yield unexpected results. In that case use 'value' which will try to match other charts values, or a fully custom function which will receive tick, data as argument and should return an index. 'index' | 'value' | function. Default: "index"
  91. sync_method: Var[LiteralSyncMethod]
  92. # The layout of area in the chart. 'horizontal' | 'vertical'. Default: "horizontal"
  93. layout: Var[LiteralLayout]
  94. # The type of offset function used to generate the lower and upper values in the series array. The four types are built-in offsets in d3-shape. 'expand' | 'none' | 'wiggle' | 'silhouette'
  95. stack_offset: Var[LiteralStackOffset]
  96. class AreaChart(CategoricalChartBase):
  97. """An Area chart component in Recharts."""
  98. tag = "AreaChart"
  99. alias = "RechartsAreaChart"
  100. # The base value of area. Number | 'dataMin' | 'dataMax' | 'auto'. Default: "auto"
  101. base_value: Var[int | LiteralComposedChartBaseValue]
  102. # Valid children components
  103. _valid_children: ClassVar[list[str]] = [
  104. "XAxis",
  105. "YAxis",
  106. "ReferenceArea",
  107. "ReferenceDot",
  108. "ReferenceLine",
  109. "Brush",
  110. "CartesianGrid",
  111. "Legend",
  112. "GraphingTooltip",
  113. "Area",
  114. "Defs",
  115. ]
  116. class BarChart(CategoricalChartBase):
  117. """A Bar chart component in Recharts."""
  118. tag = "BarChart"
  119. alias = "RechartsBarChart"
  120. # The gap between two bar categories, which can be a percent value or a fixed value. Percentage | Number. Default: "10%"
  121. bar_category_gap: Var[str | int]
  122. # The gap between two bars in the same category, which can be a percent value or a fixed value. Percentage | Number. Default: 4
  123. bar_gap: Var[str | int]
  124. # The width of all the bars in the chart. Number
  125. bar_size: Var[int]
  126. # The maximum width of all the bars in a horizontal BarChart, or maximum height in a vertical BarChart.
  127. max_bar_size: Var[int]
  128. # The type of offset function used to generate the lower and upper values in the series array. The four types are built-in offsets in d3-shape. Default: "none"
  129. stack_offset: Var[LiteralStackOffset]
  130. # If false set, stacked items will be rendered left to right. If true set, stacked items will be rendered right to left. (Render direction affects SVG layering, not x position.) Default: False
  131. reverse_stack_order: Var[bool]
  132. # Valid children components
  133. _valid_children: ClassVar[list[str]] = [
  134. "XAxis",
  135. "YAxis",
  136. "ReferenceArea",
  137. "ReferenceDot",
  138. "ReferenceLine",
  139. "Brush",
  140. "CartesianGrid",
  141. "Legend",
  142. "GraphingTooltip",
  143. "Bar",
  144. ]
  145. class LineChart(CategoricalChartBase):
  146. """A Line chart component in Recharts."""
  147. tag = "LineChart"
  148. alias = "RechartsLineChart"
  149. # Valid children components
  150. _valid_children: ClassVar[list[str]] = [
  151. "XAxis",
  152. "YAxis",
  153. "ReferenceArea",
  154. "ReferenceDot",
  155. "ReferenceLine",
  156. "Brush",
  157. "CartesianGrid",
  158. "Legend",
  159. "GraphingTooltip",
  160. "Line",
  161. ]
  162. class ComposedChart(CategoricalChartBase):
  163. """A Composed chart component in Recharts."""
  164. tag = "ComposedChart"
  165. alias = "RechartsComposedChart"
  166. # The base value of area. Number | 'dataMin' | 'dataMax' | 'auto'. Default: "auto"
  167. base_value: Var[int | LiteralComposedChartBaseValue]
  168. # The gap between two bar categories, which can be a percent value or a fixed value. Percentage | Number. Default: "10%"
  169. bar_category_gap: Var[str | int]
  170. # The gap between two bars in the same category. Default: 4
  171. bar_gap: Var[int]
  172. # The width or height of each bar. If the barSize is not specified, the size of the bar will be calculated by the barCategoryGap, barGap and the quantity of bar groups.
  173. bar_size: Var[int]
  174. # If false set, stacked items will be rendered left to right. If true set, stacked items will be rendered right to left. (Render direction affects SVG layering, not x position). Default: False
  175. reverse_stack_order: Var[bool]
  176. # Valid children components
  177. _valid_children: ClassVar[list[str]] = [
  178. "XAxis",
  179. "YAxis",
  180. "ReferenceArea",
  181. "ReferenceDot",
  182. "ReferenceLine",
  183. "Brush",
  184. "CartesianGrid",
  185. "Legend",
  186. "GraphingTooltip",
  187. "Area",
  188. "Line",
  189. "Bar",
  190. ]
  191. class PieChart(ChartBase):
  192. """A Pie chart component in Recharts."""
  193. tag = "PieChart"
  194. alias = "RechartsPieChart"
  195. # The sizes of whitespace around the chart, i.e. {"top": 50, "right": 30, "left": 20, "bottom": 5}.
  196. margin: Var[dict[str, Any]]
  197. # Valid children components
  198. _valid_children: ClassVar[list[str]] = [
  199. "PolarAngleAxis",
  200. "PolarRadiusAxis",
  201. "PolarGrid",
  202. "Legend",
  203. "GraphingTooltip",
  204. "Pie",
  205. ]
  206. # The customized event handler of mousedown on the sectors in this group
  207. on_mouse_down: EventHandler[no_args_event_spec]
  208. # The customized event handler of mouseup on the sectors in this group
  209. on_mouse_up: EventHandler[no_args_event_spec]
  210. # The customized event handler of mouseover on the sectors in this group
  211. on_mouse_over: EventHandler[no_args_event_spec]
  212. # The customized event handler of mouseout on the sectors in this group
  213. on_mouse_out: EventHandler[no_args_event_spec]
  214. class RadarChart(ChartBase):
  215. """A Radar chart component in Recharts."""
  216. tag = "RadarChart"
  217. alias = "RechartsRadarChart"
  218. # The source data, in which each element is an object.
  219. data: Var[Sequence[dict[str, Any]]]
  220. # The sizes of whitespace around the chart, i.e. {"top": 50, "right": 30, "left": 20, "bottom": 5}. Default: {"top": 0, "right": 0, "left": 0, "bottom": 0}
  221. margin: Var[dict[str, Any]]
  222. # The The x-coordinate of center. If set a percentage, the final value is obtained by multiplying the percentage of width. Number | Percentage. Default: "50%"
  223. cx: Var[int | str]
  224. # The The y-coordinate of center. If set a percentage, the final value is obtained by multiplying the percentage of height. Number | Percentage. Default: "50%"
  225. cy: Var[int | str]
  226. # The angle of first radial direction line. Default: 90
  227. start_angle: Var[int]
  228. # The angle of last point in the circle which should be startAngle - 360 or startAngle + 360. We'll calculate the direction of chart by 'startAngle' and 'endAngle'. Default: -270
  229. end_angle: Var[int]
  230. # The inner radius of first circle grid. If set a percentage, the final value is obtained by multiplying the percentage of maxRadius which is calculated by the width, height, cx, cy. Number | Percentage. Default: 0
  231. inner_radius: Var[int | str]
  232. # The outer radius of last circle grid. If set a percentage, the final value is obtained by multiplying the percentage of maxRadius which is calculated by the width, height, cx, cy. Number | Percentage. Default: "80%"
  233. outer_radius: Var[int | str]
  234. # Valid children components
  235. _valid_children: ClassVar[list[str]] = [
  236. "PolarAngleAxis",
  237. "PolarRadiusAxis",
  238. "PolarGrid",
  239. "Legend",
  240. "GraphingTooltip",
  241. "Radar",
  242. ]
  243. def get_event_triggers(self) -> dict[str, Var | Any]:
  244. """Get the event triggers that pass the component's value to the handler.
  245. Returns:
  246. A dict mapping the event trigger to the var that is passed to the handler.
  247. """
  248. return {
  249. EventTriggers.ON_CLICK: no_args_event_spec,
  250. EventTriggers.ON_MOUSE_ENTER: no_args_event_spec,
  251. EventTriggers.ON_MOUSE_LEAVE: no_args_event_spec,
  252. }
  253. class RadialBarChart(ChartBase):
  254. """A RadialBar chart component in Recharts."""
  255. tag = "RadialBarChart"
  256. alias = "RechartsRadialBarChart"
  257. # The source data which each element is an object.
  258. data: Var[Sequence[dict[str, Any]]]
  259. # The sizes of whitespace around the chart. Default: {"top": 5, "right": 5, "left": 5 "bottom": 5}
  260. margin: Var[dict[str, Any]]
  261. # The The x-coordinate of center. If set a percentage, the final value is obtained by multiplying the percentage of width. Number | Percentage. Default: "50%"
  262. cx: Var[int | str]
  263. # The The y-coordinate of center. If set a percentage, the final value is obtained by multiplying the percentage of height. Number | Percentage. Default: "50%"
  264. cy: Var[int | str]
  265. # The angle of first radial direction line. Default: 0
  266. start_angle: Var[int]
  267. # The angle of last point in the circle which should be startAngle - 360 or startAngle + 360. We'll calculate the direction of chart by 'startAngle' and 'endAngle'. Default: 360
  268. end_angle: Var[int]
  269. # The inner radius of first circle grid. If set a percentage, the final value is obtained by multiplying the percentage of maxRadius which is calculated by the width, height, cx, cy. Number | Percentage. Default: "30%"
  270. inner_radius: Var[int | str]
  271. # The outer radius of last circle grid. If set a percentage, the final value is obtained by multiplying the percentage of maxRadius which is calculated by the width, height, cx, cy. Number | Percentage. Default: "100%"
  272. outer_radius: Var[int | str]
  273. # The gap between two bar categories, which can be a percent value or a fixed value. Percentage | Number. Default: "10%"
  274. bar_category_gap: Var[int | str]
  275. # The gap between two bars in the same category, which can be a percent value or a fixed value. Percentage | Number. Default: 4
  276. bar_gap: Var[str]
  277. # The size of each bar. If the barSize is not specified, the size of bar will be calculated by the barCategoryGap, barGap and the quantity of bar groups.
  278. bar_size: Var[int]
  279. # Valid children components
  280. _valid_children: ClassVar[list[str]] = [
  281. "PolarAngleAxis",
  282. "PolarRadiusAxis",
  283. "PolarGrid",
  284. "Legend",
  285. "GraphingTooltip",
  286. "RadialBar",
  287. ]
  288. class ScatterChart(ChartBase):
  289. """A Scatter chart component in Recharts."""
  290. tag = "ScatterChart"
  291. alias = "RechartsScatterChart"
  292. # The sizes of whitespace around the chart. Default: {"top": 5, "right": 5, "bottom": 5, "left": 5}
  293. margin: Var[dict[str, Any]]
  294. # Valid children components
  295. _valid_children: ClassVar[list[str]] = [
  296. "XAxis",
  297. "YAxis",
  298. "ZAxis",
  299. "ReferenceArea",
  300. "ReferenceDot",
  301. "ReferenceLine",
  302. "Brush",
  303. "CartesianGrid",
  304. "Legend",
  305. "GraphingTooltip",
  306. "Scatter",
  307. ]
  308. def get_event_triggers(self) -> dict[str, Var | Any]:
  309. """Get the event triggers that pass the component's value to the handler.
  310. Returns:
  311. A dict mapping the event trigger to the var that is passed to the handler.
  312. """
  313. return {
  314. EventTriggers.ON_CLICK: no_args_event_spec,
  315. EventTriggers.ON_MOUSE_DOWN: no_args_event_spec,
  316. EventTriggers.ON_MOUSE_UP: no_args_event_spec,
  317. EventTriggers.ON_MOUSE_MOVE: no_args_event_spec,
  318. EventTriggers.ON_MOUSE_OVER: no_args_event_spec,
  319. EventTriggers.ON_MOUSE_OUT: no_args_event_spec,
  320. EventTriggers.ON_MOUSE_ENTER: no_args_event_spec,
  321. EventTriggers.ON_MOUSE_LEAVE: no_args_event_spec,
  322. }
  323. class FunnelChart(ChartBase):
  324. """A Funnel chart component in Recharts."""
  325. tag = "FunnelChart"
  326. alias = "RechartsFunnelChart"
  327. # The layout of bars in the chart. Default: "centric"
  328. layout: Var[str]
  329. # The sizes of whitespace around the chart. Default: {"top": 5, "right": 5, "bottom": 5, "left": 5}
  330. margin: Var[dict[str, Any]]
  331. # The stroke color of each bar. String | Object
  332. stroke: Var[str | Color]
  333. # Valid children components
  334. _valid_children: ClassVar[list[str]] = ["Legend", "GraphingTooltip", "Funnel"]
  335. class Treemap(RechartsCharts):
  336. """A Treemap chart component in Recharts."""
  337. tag = "Treemap"
  338. alias = "RechartsTreemap"
  339. # The width of chart container. String or Integer. Default: "100%"
  340. width: Var[str | int] = Var.create("100%")
  341. # The height of chart container. String or Integer. Default: "100%"
  342. height: Var[str | int] = Var.create("100%")
  343. # data of treemap. Array
  344. data: Var[Sequence[dict[str, Any]]]
  345. # The key of a group of data which should be unique in a treemap. String | Number. Default: "value"
  346. data_key: Var[str | int]
  347. # The key of each sector's name. String. Default: "name"
  348. name_key: Var[str]
  349. # The treemap will try to keep every single rectangle's aspect ratio near the aspectRatio given. Number
  350. aspect_ratio: Var[int]
  351. # If set false, animation of area will be disabled. Default: True
  352. is_animation_active: Var[bool]
  353. # Specifies when the animation should begin, the unit of this option is ms. Default: 0
  354. animation_begin: Var[int]
  355. # Specifies the duration of animation, the unit of this option is ms. Default: 1500
  356. animation_duration: Var[int]
  357. # The type of easing function. 'ease' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'linear'. Default: "ease"
  358. animation_easing: Var[LiteralAnimationEasing]
  359. # The customized event handler of animation start
  360. on_animation_start: EventHandler[no_args_event_spec]
  361. # The customized event handler of animation end
  362. on_animation_end: EventHandler[no_args_event_spec]
  363. @classmethod
  364. def create(cls, *children, **props) -> Component:
  365. """Create a chart component.
  366. Args:
  367. *children: The children of the chart component.
  368. **props: The properties of the chart component.
  369. Returns:
  370. The Treemap component wrapped in a responsive container.
  371. """
  372. return ResponsiveContainer.create(
  373. super().create(*children, **props),
  374. width=props.pop("width", "100%"),
  375. height=props.pop("height", "100%"),
  376. )
  377. area_chart = AreaChart.create
  378. bar_chart = BarChart.create
  379. line_chart = LineChart.create
  380. composed_chart = ComposedChart.create
  381. pie_chart = PieChart.create
  382. radar_chart = RadarChart.create
  383. radial_bar_chart = RadialBarChart.create
  384. scatter_chart = ScatterChart.create
  385. funnel_chart = FunnelChart.create
  386. treemap = Treemap.create