cartesian.py 21 KB

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