cartesian.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. """Cartesian 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.vars import Var
  6. from .recharts import Recharts
  7. class Axis(Recharts):
  8. """A base class for axes in Recharts."""
  9. # The key of a group of data which should be unique in an area chart.
  10. data_key: Var[Union[str, int]]
  11. # If set true, the axis do not display in the chart.
  12. hide: Var[bool]
  13. # The orientation of axis 'top' | 'bottom'
  14. orientation: Var[str]
  15. # The type of axis 'number' | 'category'
  16. type_: Var[str]
  17. # Allow the ticks of XAxis to be decimals or not.
  18. allow_decimals: Var[bool]
  19. # When domain of the axis is specified and the type of the axis is 'number', if allowDataOverflow is set to be false, the domain will be adjusted when the minimum value of data is smaller than domain[0] or the maximum value of data is greater than domain[1] so that the axis displays all data values. If set to true, graphic elements (line, area, bars) will be clipped to conform to the specified domain.
  20. allow_data_overflow: Var[bool]
  21. # Allow the axis has duplicated categorys or not when the type of axis is "category".
  22. allow_duplicated_category: Var[bool]
  23. # If set false, no axis line will be drawn. If set a object, the option is the configuration of axis line.
  24. axis_line: Var[bool]
  25. # If set false, no axis tick lines will be drawn. If set a object, the option is the configuration of tick lines.
  26. tick_line: Var[bool]
  27. # If set true, flips ticks around the axis line, displaying the labels inside the chart instead of outside.
  28. mirror: Var[bool]
  29. # Reverse the ticks or not.
  30. reversed: Var[bool]
  31. # If 'auto' set, the scale function is decided by the type of chart, and the props type. 'auto' | 'linear' | 'pow' | 'sqrt' | 'log' | 'identity' | 'time' | 'band' | 'point' | 'ordinal' | 'quantile' | 'quantize' | 'utc' | 'sequential' | 'threshold' | Function
  32. scale: Var[str]
  33. # The unit of data displayed in the axis. This option will be used to represent an index unit in a scatter chart.
  34. unit: Var[Union[str, int]]
  35. # The name of data displayed in the axis. This option will be used to represent an index in a scatter chart.
  36. name: Var[Union[str, int]]
  37. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  38. """Get the event triggers that pass the component's value to the handler.
  39. Returns:
  40. A dict mapping the event trigger to the var that is passed to the handler.
  41. """
  42. return {
  43. EventTriggers.ON_CLICK: lambda: [],
  44. EventTriggers.ON_MOUSE_MOVE: lambda: [],
  45. EventTriggers.ON_MOUSE_OVER: lambda: [],
  46. EventTriggers.ON_MOUSE_OUT: lambda: [],
  47. EventTriggers.ON_MOUSE_ENTER: lambda: [],
  48. EventTriggers.ON_MOUSE_LEAVE: lambda: [],
  49. }
  50. class XAxis(Axis):
  51. """An XAxis component in Recharts."""
  52. tag = "XAxis"
  53. alias = "RechartsXAxis"
  54. class YAxis(Axis):
  55. """A YAxis component in Recharts."""
  56. tag = "YAxis"
  57. alias = "RechartsYAxis"
  58. # The key of data displayed in the axis.
  59. data_key: Var[Union[str, int]]
  60. class ZAxis(Recharts):
  61. """A ZAxis component in Recharts."""
  62. tag = "ZAxis"
  63. alias = "RechartszAxis"
  64. # The key of data displayed in the axis.
  65. data_key: Var[Union[str, int]]
  66. # The range of axis.
  67. range: Var[List[int]]
  68. # The unit of data displayed in the axis. This option will be used to represent an index unit in a scatter chart.
  69. unit: Var[Union[str, int]]
  70. # The name of data displayed in the axis. This option will be used to represent an index in a scatter chart.
  71. name: Var[Union[str, int]]
  72. # If 'auto' set, the scale function is decided by the type of chart, and the props type.
  73. scale: Var[str]
  74. class Brush(Recharts):
  75. """A Brush component in Recharts."""
  76. tag = "Brush"
  77. alias = "RechartsBrush"
  78. # Stroke color
  79. stroke: Var[str]
  80. # The key of data displayed in the axis.
  81. data_key: Var[Union[str, int]]
  82. # The x-coordinate of brush.
  83. x: Var[int]
  84. # The y-coordinate of brush.
  85. y: Var[int]
  86. # The width of brush.
  87. width: Var[int]
  88. # The height of brush.
  89. height: Var[int]
  90. # The data domain of brush, [min, max].
  91. data: Var[List[Any]]
  92. # The width of each traveller.
  93. traveller_width: Var[int]
  94. # The data with gap of refreshing chart. If the option is not set, the chart will be refreshed every time
  95. gap: Var[int]
  96. # The default start index of brush. If the option is not set, the start index will be 0.
  97. start_index: Var[int]
  98. # The default end index of brush. If the option is not set, the end index will be 1.
  99. end_index: Var[int]
  100. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  101. """Get the event triggers that pass the component's value to the handler.
  102. Returns:
  103. A dict mapping the event trigger to the var that is passed to the handler.
  104. """
  105. return {
  106. EventTriggers.ON_CHANGE: lambda: [],
  107. }
  108. class Cartesian(Recharts):
  109. """A base class for cartesian charts in Recharts."""
  110. # The layout of bar in the chart, usually inherited from parent. 'horizontal' | 'vertical'
  111. layout: Var[str]
  112. # The key of a group of data which should be unique in an area chart.
  113. data_key: Var[Union[str, int]]
  114. # The id of x-axis which is corresponding to the data.
  115. x_axis_id: Var[Union[str, int]]
  116. # The id of y-axis which is corresponding to the data.
  117. y_axis_id: Var[Union[str, int]]
  118. # The type of icon in legend. If set to 'none', no legend item will be rendered. 'line' | 'plainline' | 'square' | 'rect'| 'circle' | 'cross' | 'diamond' | 'square' | 'star' | 'triangle' | 'wye' | 'none'optional
  119. legend_type: Var[str]
  120. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  121. """Get the event triggers that pass the component's value to the handler.
  122. Returns:
  123. A dict mapping the event trigger to the var that is passed to the handler.
  124. """
  125. return {
  126. EventTriggers.ON_CLICK: lambda: [],
  127. EventTriggers.ON_MOUSE_MOVE: lambda: [],
  128. EventTriggers.ON_MOUSE_OVER: lambda: [],
  129. EventTriggers.ON_MOUSE_OUT: lambda: [],
  130. EventTriggers.ON_MOUSE_ENTER: lambda: [],
  131. EventTriggers.ON_MOUSE_LEAVE: lambda: [],
  132. }
  133. class Area(Cartesian):
  134. """An Area component in Recharts."""
  135. tag = "Area"
  136. alias = "RechartsArea"
  137. # The color of the line stroke.
  138. stroke: Var[str]
  139. # The width of the line stroke.
  140. stroke_width: Var[int]
  141. # The color of the area fill.
  142. fill: Var[str]
  143. # The interpolation type of area. And customized interpolation function can be set to type. 'basis' | 'basisClosed' | 'basisOpen' | 'bumpX' | 'bumpY' | 'bump' | 'linear' | 'linearClosed' | 'natural' | 'monotoneX' | 'monotoneY' | 'monotone' | 'step' | 'stepBefore' | 'stepAfter' |
  144. type_: Var[str]
  145. # If false set, dots will not be drawn. If true set, dots will be drawn which have the props calculated internally.
  146. dot: Var[bool]
  147. # The dot is shown when user enter an area chart and this chart has tooltip. If false set, no active dot will not be drawn. If true set, active dot will be drawn which have the props calculated internally.
  148. active_dot: Var[bool]
  149. # If false set, labels will not be drawn. If true set, labels will be drawn which have the props calculated internally.
  150. label: Var[bool]
  151. # The stack id of area, when two areas have the same value axis and same stackId, then the two areas area stacked in order.
  152. stack_id: Var[str]
  153. # Valid children components
  154. valid_children: List[str] = ["LabelList"]
  155. class Bar(Cartesian):
  156. """A Bar component in Recharts."""
  157. tag = "Bar"
  158. alias = "RechartsBar"
  159. # The color of the line stroke.
  160. stroke: Var[str]
  161. # The width of the line stroke.
  162. stroke_width: Var[int]
  163. # The width of the line stroke.
  164. fill: Var[str]
  165. # If false set, background of bars will not be drawn. If true set, background of bars will be drawn which have the props calculated internally.
  166. background: Var[bool]
  167. # If false set, labels will not be drawn. If true set, labels will be drawn which have the props calculated internally.
  168. label: Var[bool]
  169. # The stack id of bar, when two areas have the same value axis and same stackId, then the two areas area stacked in order.
  170. stack_id: Var[str]
  171. # Size of the bar
  172. bar_size: Var[int]
  173. # Max size of the bar
  174. max_bar_size: Var[int]
  175. # Valid children components
  176. valid_children: List[str] = ["Cell", "LabelList", "ErrorBar"]
  177. class Line(Cartesian):
  178. """A Line component in Recharts."""
  179. tag = "Line"
  180. alias = "RechartsLine"
  181. # The interpolation type of line. And customized interpolation function can be set to type. It's the same as type in Area.
  182. type_: Var[str]
  183. # The color of the line stroke.
  184. stroke: Var[str]
  185. # The width of the line stroke.
  186. stoke_width: Var[int]
  187. # The dot is shown when mouse enter a line chart and this chart has tooltip. If false set, no active dot will not be drawn. If true set, active dot will be drawn which have the props calculated internally.
  188. dot: Var[bool]
  189. # The dot is shown when user enter an area chart and this chart has tooltip. If false set, no active dot will not be drawn. If true set, active dot will be drawn which have the props calculated internally.
  190. active_dot: Var[bool]
  191. # If false set, labels will not be drawn. If true set, labels will be drawn which have the props calculated internally.
  192. label: Var[bool]
  193. # Hides the line when true, useful when toggling visibility state via legend.
  194. hide: Var[bool]
  195. # Whether to connect a graph line across null points.
  196. connect_nulls: Var[bool]
  197. # Valid children components
  198. valid_children: List[str] = ["LabelList", "ErrorBar"]
  199. class Scatter(Cartesian):
  200. """A Scatter component in Recharts."""
  201. tag = "Scatter"
  202. alias = "RechartsScatter"
  203. # The source data, in which each element is an object.
  204. data: Var[List[Dict[str, Any]]]
  205. # The id of z-axis which is corresponding to the data.
  206. z_axis_id: Var[str]
  207. # If false set, line will not be drawn. If true set, line will be drawn which have the props calculated internally.
  208. line: Var[bool]
  209. # If a string set, specified symbol will be used to show scatter item. 'circle' | 'cross' | 'diamond' | 'square' | 'star' | 'triangle' | 'wye'
  210. shape: Var[str]
  211. # If 'joint' set, line will generated by just jointing all the points. If 'fitting' set, line will be generated by fitting algorithm. 'joint' | 'fitting'
  212. line_type: Var[str]
  213. # The fill
  214. fill: Var[str]
  215. # the name
  216. name: Var[Union[str, int]]
  217. # Valid children components.
  218. valid_children: List[str] = ["LabelList", "ErrorBar"]
  219. class Funnel(Cartesian):
  220. """A Funnel component in Recharts."""
  221. tag = "Funnel"
  222. alias = "RechartsFunnel"
  223. # The source data, in which each element is an object.
  224. data: Var[List[Dict[str, Any]]]
  225. # Specifies when the animation should begin, the unit of this option is ms.
  226. animation_begin: Var[int]
  227. # Specifies the duration of animation, the unit of this option is ms.
  228. animation_duration: Var[int]
  229. # The type of easing function. 'ease' | 'ease-in' | 'ease-out' | 'ease-in-out' | 'linear'
  230. animation_easing: Var[str]
  231. # Valid children components
  232. valid_children: List[str] = ["LabelList", "Cell"]
  233. class ErrorBar(Recharts):
  234. """An ErrorBar component in Recharts."""
  235. tag = "ErrorBar"
  236. alias = "RechartsErrorBar"
  237. # The direction of error bar. 'x' | 'y' | 'both'
  238. direction: Var[str]
  239. # The key of a group of data which should be unique in an area chart.
  240. data_key: Var[Union[str, int]]
  241. # The width of the error bar ends.
  242. width: Var[int]
  243. # The stroke color of error bar.
  244. stroke: Var[str]
  245. # The stroke width of error bar.
  246. stroke_width: Var[int]
  247. class Reference(Recharts):
  248. """A base class for reference components in Reference."""
  249. # The id of x-axis which is corresponding to the data.
  250. x_axis_id: Var[Union[str, int]]
  251. # The id of y-axis which is corresponding to the data.
  252. y_axis_id: Var[Union[str, int]]
  253. # If set a string or a number, a vertical line perpendicular to the x-axis specified by xAxisId will be drawn. If the specified x-axis is a number axis, the type of x must be Number. If the specified x-axis is a category axis, the value of x must be one of the categorys, otherwise no line will be drawn.
  254. x: Var[str]
  255. # If set a string or a number, a horizontal line perpendicular to the y-axis specified by yAxisId will be drawn. If the specified y-axis is a number axis, the type of y must be Number. If the specified y-axis is a category axis, the value of y must be one of the categorys, otherwise no line will be drawn.
  256. y: Var[str]
  257. # Defines how to draw the reference line if it falls partly outside the canvas. If set to 'discard', the reference line will not be drawn at all. If set to 'hidden', the reference line will be clipped to the canvas. If set to 'visible', the reference line will be drawn completely. If set to 'extendDomain', the domain of the overflown axis will be extended such that the reference line fits into the canvas.
  258. if_overflow: Var[str]
  259. # If set true, the line will be rendered in front of bars in BarChart, etc.
  260. is_front: Var[bool]
  261. class ReferenceLine(Reference):
  262. """A ReferenceLine component in Recharts."""
  263. tag = "ReferenceLine"
  264. alias = "RechartsReferenceLine"
  265. # The width of the stroke.
  266. stroke_width: Var[int]
  267. # Valid children components
  268. valid_children: List[str] = ["Label"]
  269. class ReferenceDot(Reference):
  270. """A ReferenceDot component in Recharts."""
  271. tag = "ReferenceDot"
  272. alias = "RechartsReferenceDot"
  273. # Valid children components
  274. valid_children: List[str] = ["Label"]
  275. def get_event_triggers(self) -> dict[str, Union[Var, Any]]:
  276. """Get the event triggers that pass the component's value to the handler.
  277. Returns:
  278. A dict mapping the event trigger to the var that is passed to the handler.
  279. """
  280. return {
  281. EventTriggers.ON_CLICK: lambda: [],
  282. EventTriggers.ON_MOUSE_MOVE: lambda: [],
  283. EventTriggers.ON_MOUSE_OVER: lambda: [],
  284. EventTriggers.ON_MOUSE_OUT: lambda: [],
  285. EventTriggers.ON_MOUSE_ENTER: lambda: [],
  286. EventTriggers.ON_MOUSE_LEAVE: lambda: [],
  287. }
  288. class ReferenceArea(Recharts):
  289. """A ReferenceArea component in Recharts."""
  290. tag = "ReferenceArea"
  291. alias = "RechartsReferenceArea"
  292. # Stroke color
  293. stroke: Var[str]
  294. # Fill color
  295. fill: Var[str]
  296. # The opacity of area.
  297. fill_opacity: Var[float]
  298. # The id of x-axis which is corresponding to the data.
  299. x_axis_id: Var[Union[str, int]]
  300. # The id of y-axis which is corresponding to the data.
  301. y_axis_id: Var[Union[str, int]]
  302. # A boundary value of the area. If the specified x-axis is a number axis, the type of x must be Number. If the specified x-axis is a category axis, the value of x must be one of the categorys. If one of x1 or x2 is invalidate, the area will cover along x-axis.
  303. x1: Var[Union[str, int]]
  304. # A boundary value of the area. If the specified x-axis is a number axis, the type of x must be Number. If the specified x-axis is a category axis, the value of x must be one of the categorys. If one of x1 or x2 is invalidate, the area will cover along x-axis.
  305. x2: Var[Union[str, int]]
  306. # A boundary value of the area. If the specified y-axis is a number axis, the type of y must be Number. If the specified y-axis is a category axis, the value of y must be one of the categorys. If one of y1 or y2 is invalidate, the area will cover along y-axis.
  307. y1: Var[Union[str, int]]
  308. # A boundary value of the area. If the specified y-axis is a number axis, the type of y must be Number. If the specified y-axis is a category axis, the value of y must be one of the categorys. If one of y1 or y2 is invalidate, the area will cover along y-axis.
  309. y2: Var[Union[str, int]]
  310. # Defines how to draw the reference line if it falls partly outside the canvas. If set to 'discard', the reference line will not be drawn at all. If set to 'hidden', the reference line will be clipped to the canvas. If set to 'visible', the reference line will be drawn completely. If set to 'extendDomain', the domain of the overflown axis will be extended such that the reference line fits into the canvas.
  311. if_overflow: Var[str]
  312. # If set true, the line will be rendered in front of bars in BarChart, etc.
  313. is_front: Var[bool]
  314. # Valid children components
  315. valid_children: List[str] = ["Label"]
  316. class Grid(Recharts):
  317. """A base class for grid components in Recharts."""
  318. # The x-coordinate of grid.
  319. x: Var[int]
  320. # The y-coordinate of grid.
  321. y: Var[int]
  322. # The width of grid.
  323. width: Var[int]
  324. # The height of grid.
  325. height: Var[int]
  326. class CartesianGrid(Grid):
  327. """A CartesianGrid component in Recharts."""
  328. tag = "CartesianGrid"
  329. alias = "RechartsCartesianGrid"
  330. # The horizontal line configuration.
  331. horizontal: Var[Dict[str, Any]]
  332. # The vertical line configuration.
  333. vertical: Var[Dict[str, Any]]
  334. # The background of grid.
  335. fill: Var[str]
  336. # The opacity of the background used to fill the space between grid lines
  337. fill_opacity: Var[float]
  338. # The pattern of dashes and gaps used to paint the lines of the grid
  339. stroke_dasharray: Var[str]
  340. class CartesianAxis(Grid):
  341. """A CartesianAxis component in Recharts."""
  342. tag = "CartesianAxis"
  343. alias = "RechartsCartesianAxis"
  344. # The orientation of axis 'top' | 'bottom' | 'left' | 'right'
  345. orientation: Var[str]
  346. # If set false, no axis line will be drawn. If set a object, the option is the configuration of axis line.
  347. axis_line: Var[bool]
  348. # If set false, no axis tick lines will be drawn. If set a object, the option is the configuration of tick lines.
  349. tick_line: Var[bool]
  350. # The length of tick line.
  351. tick_size: Var[int]
  352. # If set 0, all the ticks will be shown. If set preserveStart", "preserveEnd" or "preserveStartEnd", the ticks which is to be shown or hidden will be calculated automatically.
  353. interval: Var[str]
  354. # If set false, no ticks will be drawn.
  355. ticks: Var[bool]
  356. # If set a string or a number, default label will be drawn, and the option is content.
  357. label: Var[str]
  358. # If set true, flips ticks around the axis line, displaying the labels inside the chart instead of outside.
  359. mirror: Var[bool]
  360. # The margin between tick line and tick.
  361. tick_margin: Var[int]