1
0

polar.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. """Polar charts in Recharts."""
  2. from __future__ import annotations
  3. from typing import Any, Dict, List, Union
  4. from reflex.constants import EventTriggers
  5. from reflex.constants.colors import Color
  6. from reflex.event import EventHandler
  7. from reflex.vars.base import LiteralVar, Var
  8. from .recharts import (
  9. LiteralAnimationEasing,
  10. LiteralGridType,
  11. LiteralLegendType,
  12. LiteralPolarRadiusType,
  13. LiteralScale,
  14. Recharts,
  15. )
  16. class Pie(Recharts):
  17. """A Pie chart component in Recharts."""
  18. tag = "Pie"
  19. alias = "RechartsPie"
  20. # data
  21. data: Var[List[Dict[str, Any]]]
  22. # The key of each sector's value.
  23. data_key: Var[Union[str, int]]
  24. # The x-coordinate of center. If set a percentage, the final value is obtained by multiplying the percentage of container width.
  25. cx: Var[Union[int, str]]
  26. # The y-coordinate of center. If set a percentage, the final value is obtained by multiplying the percentage of container height.
  27. cy: Var[Union[int, str]]
  28. # The inner radius of pie, which can be set to a percent value.
  29. inner_radius: Var[Union[int, str]]
  30. # The outer radius of pie, which can be set to a percent value.
  31. outer_radius: Var[Union[int, str]]
  32. # The angle of first sector.
  33. start_angle: Var[int]
  34. # The direction of sectors. 1 means clockwise and -1 means anticlockwise.
  35. end_angle: Var[int]
  36. # The minimum angle of each unzero data.
  37. min_angle: Var[int]
  38. # The angle between two sectors.
  39. padding_angle: Var[int]
  40. # The key of each sector's name.
  41. name_key: Var[str]
  42. # The type of icon in legend. If set to 'none', no legend item will be rendered.
  43. legend_type: Var[LiteralLegendType]
  44. # If false set, labels will not be drawn.
  45. label: Var[bool] = False # type: ignore
  46. # If false set, label lines will not be drawn.
  47. label_line: Var[bool]
  48. # Valid children components
  49. _valid_children: List[str] = ["Cell", "LabelList"]
  50. # Stoke color
  51. stroke: Var[Union[str, Color]] = LiteralVar.create(Color("accent", 9))
  52. # Fill color
  53. fill: Var[Union[str, Color]] = LiteralVar.create(Color("accent", 3))
  54. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  55. """Get the event triggers that pass the component's value to the handler.
  56. Returns:
  57. A dict mapping the event trigger to the var that is passed to the handler.
  58. """
  59. return {
  60. EventTriggers.ON_CLICK: lambda: [],
  61. EventTriggers.ON_MOUSE_MOVE: lambda: [],
  62. EventTriggers.ON_MOUSE_OVER: lambda: [],
  63. EventTriggers.ON_MOUSE_OUT: lambda: [],
  64. EventTriggers.ON_MOUSE_ENTER: lambda: [],
  65. EventTriggers.ON_MOUSE_LEAVE: lambda: [],
  66. }
  67. class Radar(Recharts):
  68. """A Radar chart component in Recharts."""
  69. tag = "Radar"
  70. alias = "RechartsRadar"
  71. # The key of a group of data which should be unique in a radar chart.
  72. data_key: Var[Union[str, int]]
  73. # The coordinates of all the vertexes of the radar shape, like [{ x, y }].
  74. points: Var[List[Dict[str, Any]]]
  75. # If false set, dots will not be drawn. Default: True
  76. dot: Var[bool]
  77. # Stoke color. Default: rx.color("accent", 9)
  78. stroke: Var[Union[str, Color]] = LiteralVar.create(Color("accent", 9))
  79. # Fill color. Default: rx.color("accent", 3)
  80. fill: Var[str] = LiteralVar.create(Color("accent", 3))
  81. # opacity. Default: 0.6
  82. fill_opacity: Var[float] = LiteralVar.create(0.6)
  83. # The type of icon in legend. If set to 'none', no legend item will be rendered. Default: "rect"
  84. legend_type: Var[LiteralLegendType]
  85. # If false set, labels will not be drawn. Default: True
  86. label: Var[bool]
  87. # If set false, animation of polygon will be disabled. Default: True in CSR, and False in SSR
  88. is_animation_active: Var[bool]
  89. # Specifies when the animation should begin, the unit of this option is ms. Default: 0
  90. animation_begin: Var[int]
  91. # Specifies the duration of animation, the unit of this option is ms. Default: 1500
  92. animation_duration: Var[int]
  93. # The type of easing function. 'ease' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'linear'. Default: "ease"
  94. animation_easing: Var[LiteralAnimationEasing]
  95. # Valid children components
  96. _valid_children: List[str] = ["LabelList"]
  97. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  98. """Get the event triggers that pass the component's value to the handler.
  99. Returns:
  100. A dict mapping the event trigger to the var that is passed to the handler.
  101. """
  102. return {
  103. EventTriggers.ON_ANIMATION_START: lambda: [],
  104. EventTriggers.ON_ANIMATION_END: lambda: [],
  105. }
  106. class RadialBar(Recharts):
  107. """A RadialBar chart component in Recharts."""
  108. tag = "RadialBar"
  109. alias = "RechartsRadialBar"
  110. # The key of a group of data which should be unique to show the meaning of angle axis.
  111. data_key: Var[Union[str, int]]
  112. # Min angle of each bar. A positive value between 0 and 360.
  113. min_angle: Var[int]
  114. # Type of legend
  115. legend_type: Var[str]
  116. # If false set, labels will not be drawn.
  117. label: Var[Union[bool, Dict[str, Any]]]
  118. # If false set, background sector will not be drawn.
  119. background: Var[Union[bool, Dict[str, Any]]]
  120. # If set false, animation of radial bars will be disabled. By default true in CSR, and false in SSR
  121. is_animation_active: Var[bool]
  122. # Specifies when the animation should begin, the unit of this option is ms. By default 0
  123. animation_begin: Var[int]
  124. # Specifies the duration of animation, the unit of this option is ms. By default 1500
  125. animation_duration: Var[int]
  126. # The type of easing function. 'ease' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'linear'. By default 'ease'
  127. animation_easing: Var[LiteralAnimationEasing]
  128. # Valid children components
  129. _valid_children: List[str] = ["Cell", "LabelList"]
  130. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  131. """Get the event triggers that pass the component's value to the handler.
  132. Returns:
  133. A dict mapping the event trigger to the var that is passed to the handler.
  134. """
  135. return {
  136. EventTriggers.ON_CLICK: lambda: [],
  137. EventTriggers.ON_MOUSE_MOVE: lambda: [],
  138. EventTriggers.ON_MOUSE_OVER: lambda: [],
  139. EventTriggers.ON_MOUSE_OUT: lambda: [],
  140. EventTriggers.ON_MOUSE_ENTER: lambda: [],
  141. EventTriggers.ON_MOUSE_LEAVE: lambda: [],
  142. EventTriggers.ON_ANIMATION_START: lambda: [],
  143. EventTriggers.ON_ANIMATION_END: lambda: [],
  144. }
  145. class PolarAngleAxis(Recharts):
  146. """A PolarAngleAxis component in Recharts."""
  147. tag = "PolarAngleAxis"
  148. alias = "RechartsPolarAngleAxis"
  149. # The key of a group of data which should be unique to show the meaning of angle axis.
  150. data_key: Var[Union[str, int]]
  151. # The x-coordinate of center. If set a percentage, the final value is obtained by multiplying the percentage of container width.
  152. cx: Var[Union[int, str]]
  153. # The y-coordinate of center. If set a percentage, the final value is obtained by multiplying the percentage of container height.
  154. cy: Var[Union[int, str]]
  155. # The outer radius of 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.
  156. radius: Var[Union[int, str]]
  157. # If false set, axis line will not be drawn. If true set, axis line will be drawn which have the props calculated internally. If object set, axis line will be drawn which have the props mergered by the internal calculated props and the option. Default: True
  158. axis_line: Var[Union[bool, Dict[str, Any]]]
  159. # The type of axis line. Default: "polygon"
  160. axis_line_type: Var[LiteralGridType]
  161. # If false set, tick lines will not be drawn. If true set, tick lines will be drawn which have the props calculated internally. If object set, tick lines will be drawn which have the props mergered by the internal calculated props and the option. Default: False
  162. tick_line: Var[Union[bool, Dict[str, Any]]] = LiteralVar.create(False)
  163. # If false set, ticks will not be drawn. If true set, ticks will be drawn which have the props calculated internally. If object set, ticks will be drawn which have the props mergered by the internal calculated props and the option. Default: True
  164. tick: Var[Union[bool, Dict[str, Any]]]
  165. # The array of every tick's value and angle.
  166. ticks: Var[List[Dict[str, Any]]]
  167. # The orientation of axis text. Default: "outer"
  168. orientation: Var[str]
  169. # The stroke color of axis. Default: rx.color("gray", 10)
  170. stroke: Var[Union[str, Color]] = LiteralVar.create(Color("gray", 10))
  171. # Allow the axis has duplicated categorys or not when the type of axis is "category". Default: True
  172. allow_duplicated_category: Var[bool]
  173. # Valid children components.
  174. _valid_children: List[str] = ["Label"]
  175. # The customized event handler of click on the ticks of this axis.
  176. on_click: EventHandler[lambda: []]
  177. # The customized event handler of mousedown on the the ticks of this axis.
  178. on_mouse_down: EventHandler[lambda: []]
  179. # The customized event handler of mouseup on the ticks of this axis.
  180. on_mouse_up: EventHandler[lambda: []]
  181. # The customized event handler of mousemove on the ticks of this axis.
  182. on_mouse_move: EventHandler[lambda: []]
  183. # The customized event handler of mouseover on the ticks of this axis.
  184. on_mouse_over: EventHandler[lambda: []]
  185. # The customized event handler of mouseout on the ticks of this axis.
  186. on_mouse_out: EventHandler[lambda: []]
  187. # The customized event handler of moustenter on the ticks of this axis.
  188. on_mouse_enter: EventHandler[lambda: []]
  189. # The customized event handler of mouseleave on the ticks of this axis.
  190. on_mouse_leave: EventHandler[lambda: []]
  191. class PolarGrid(Recharts):
  192. """A PolarGrid component in Recharts."""
  193. tag = "PolarGrid"
  194. alias = "RechartsPolarGrid"
  195. # The x-coordinate of center.
  196. cx: Var[int]
  197. # The y-coordinate of center.
  198. cy: Var[int]
  199. # The radius of the inner polar grid.
  200. inner_radius: Var[int]
  201. # The radius of the outer polar grid.
  202. outer_radius: Var[int]
  203. # The array of every line grid's angle.
  204. polar_angles: Var[List[int]]
  205. # The array of every line grid's radius.
  206. polar_radius: Var[List[int]]
  207. # The type of polar grids. 'polygon' | 'circle'. Default: "polygon"
  208. grid_type: Var[LiteralGridType]
  209. # The stroke color of grid. Default: rx.color("gray", 10)
  210. stroke: Var[Union[str, Color]] = LiteralVar.create(Color("gray", 10))
  211. # Valid children components
  212. _valid_children: List[str] = ["RadarChart", "RadiarBarChart"]
  213. class PolarRadiusAxis(Recharts):
  214. """A PolarRadiusAxis component in Recharts."""
  215. tag = "PolarRadiusAxis"
  216. alias = "RechartsPolarRadiusAxis"
  217. # The angle of radial direction line to display axis text.
  218. angle: Var[int]
  219. # The type of axis line. 'number' | 'category'
  220. type_: Var[LiteralPolarRadiusType]
  221. # Allow the axis has duplicated categorys or not when the type of axis is "category".
  222. allow_duplicated_category: Var[bool]
  223. # The x-coordinate of center.
  224. cx: Var[Union[int, str]]
  225. # The y-coordinate of center.
  226. cy: Var[Union[int, str]]
  227. # If set to true, the ticks of this axis are reversed.
  228. reversed: Var[bool]
  229. # The orientation of axis text.
  230. orientation: Var[str]
  231. # If false set, axis line will not be drawn. If true set, axis line will be drawn which have the props calculated internally. If object set, axis line will be drawn which have the props mergered by the internal calculated props and the option.
  232. axis_line: Var[Union[bool, Dict[str, Any]]]
  233. # The width or height of tick.
  234. tick: Var[Union[int, str]]
  235. # The count of ticks.
  236. tick_count: Var[int]
  237. # If 'auto' set, the scale funtion is linear scale. 'auto' | 'linear' | 'pow' | 'sqrt' | 'log' | 'identity' | 'time' | 'band' | 'point' | 'ordinal' | 'quantile' | 'quantize' | 'utc' | 'sequential' | 'threshold'
  238. scale: Var[LiteralScale]
  239. # Valid children components
  240. _valid_children: List[str] = ["Label"]
  241. # The domain of the polar radius axis, specifying the minimum and maximum values.
  242. domain: Var[List[int]] = LiteralVar.create([0, 250])
  243. # The stroke color of axis
  244. stroke: Var[Union[str, Color]] = LiteralVar.create(Color("gray", 10))
  245. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  246. """Get the event triggers that pass the component's value to the handler.
  247. Returns:
  248. A dict mapping the event trigger to the var that is passed to the handler.
  249. """
  250. return {
  251. EventTriggers.ON_CLICK: lambda: [],
  252. EventTriggers.ON_MOUSE_MOVE: lambda: [],
  253. EventTriggers.ON_MOUSE_OVER: lambda: [],
  254. EventTriggers.ON_MOUSE_OUT: lambda: [],
  255. EventTriggers.ON_MOUSE_ENTER: lambda: [],
  256. EventTriggers.ON_MOUSE_LEAVE: lambda: [],
  257. }
  258. pie = Pie.create
  259. radar = Radar.create
  260. radial_bar = RadialBar.create
  261. polar_angle_axis = PolarAngleAxis.create
  262. polar_grid = PolarGrid.create
  263. polar_radius_axis = PolarRadiusAxis.create