1
0

general.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. """General components for Recharts."""
  2. from __future__ import annotations
  3. from typing import Any, ClassVar, Sequence, Union
  4. from reflex.components.component import MemoizationLeaf
  5. from reflex.constants.colors import Color
  6. from reflex.event import EventHandler, no_args_event_spec
  7. from reflex.vars.base import LiteralVar, Var
  8. from .recharts import (
  9. LiteralAnimationEasing,
  10. LiteralIconType,
  11. LiteralLayout,
  12. LiteralLegendAlign,
  13. LiteralPosition,
  14. LiteralVerticalAlign,
  15. Recharts,
  16. )
  17. class ResponsiveContainer(Recharts, MemoizationLeaf):
  18. """A base class for responsive containers in Recharts."""
  19. tag = "ResponsiveContainer"
  20. alias = "RechartsResponsiveContainer"
  21. # The aspect ratio of the container. The final aspect ratio of the SVG element will be (width / height) * aspect. Number
  22. aspect: Var[int]
  23. # The width of chart container. Can be a number or string. Default: "100%"
  24. width: Var[int | str]
  25. # The height of chart container. Can be a number or string. Default: "100%"
  26. height: Var[int | str]
  27. # The minimum width of chart container. Number or string.
  28. min_width: Var[int | str]
  29. # The minimum height of chart container. Number or string.
  30. min_height: Var[int | str]
  31. # If specified a positive number, debounced function will be used to handle the resize event. Default: 0
  32. debounce: Var[int]
  33. # If specified provides a callback providing the updated chart width and height values.
  34. on_resize: EventHandler[no_args_event_spec]
  35. # Valid children components
  36. _valid_children: ClassVar[list[str]] = [
  37. "AreaChart",
  38. "BarChart",
  39. "LineChart",
  40. "PieChart",
  41. "RadarChart",
  42. "RadialBarChart",
  43. "ResponsiveContainer",
  44. "ScatterChart",
  45. "Treemap",
  46. "ComposedChart",
  47. "FunnelChart",
  48. ]
  49. class Legend(Recharts):
  50. """A Legend component in Recharts."""
  51. tag = "Legend"
  52. alias = "RechartsLegend"
  53. # The width of legend container. Number
  54. width: Var[int]
  55. # The height of legend container. Number
  56. height: Var[int]
  57. # The layout of legend items. 'horizontal' | 'vertical'. Default: "horizontal"
  58. layout: Var[LiteralLayout]
  59. # The alignment of legend items in 'horizontal' direction, which can be 'left', 'center', 'right'. Default: "center"
  60. align: Var[LiteralLegendAlign]
  61. # The alignment of legend items in 'vertical' direction, which can be 'top', 'middle', 'bottom'. Default: "bottom"
  62. vertical_align: Var[LiteralVerticalAlign]
  63. # The size of icon in each legend item. Default: 14
  64. icon_size: Var[int]
  65. # The type of icon in each legend item. 'line' | 'plainline' | 'square' | 'rect' | 'circle' | 'cross' | 'diamond' | 'star' | 'triangle' | 'wye'
  66. icon_type: Var[LiteralIconType]
  67. # The source data of the content to be displayed in the legend, usually calculated internally. Default: []
  68. payload: Var[Sequence[dict[str, Any]]]
  69. # The width of chart container, usually calculated internally.
  70. chart_width: Var[int]
  71. # The height of chart container, usually calculated internally.
  72. chart_height: Var[int]
  73. # The margin of chart container, usually calculated internally.
  74. margin: Var[dict[str, Any]]
  75. # The customized event handler of click on the items in this group
  76. on_click: EventHandler[no_args_event_spec]
  77. # The customized event handler of mousedown on the items in this group
  78. on_mouse_down: EventHandler[no_args_event_spec]
  79. # The customized event handler of mouseup on the items in this group
  80. on_mouse_up: EventHandler[no_args_event_spec]
  81. # The customized event handler of mousemove on the items in this group
  82. on_mouse_move: EventHandler[no_args_event_spec]
  83. # The customized event handler of mouseover on the items in this group
  84. on_mouse_over: EventHandler[no_args_event_spec]
  85. # The customized event handler of mouseout on the items in this group
  86. on_mouse_out: EventHandler[no_args_event_spec]
  87. # The customized event handler of mouseenter on the items in this group
  88. on_mouse_enter: EventHandler[no_args_event_spec]
  89. # The customized event handler of mouseleave on the items in this group
  90. on_mouse_leave: EventHandler[no_args_event_spec]
  91. class GraphingTooltip(Recharts):
  92. """A Tooltip component in Recharts."""
  93. tag = "Tooltip"
  94. alias = "RechartsTooltip"
  95. # The separator between name and value. Default: ":"
  96. separator: Var[str]
  97. # The offset size of tooltip. Number. Default: 10
  98. offset: Var[int]
  99. # When an item of the payload has value null or undefined, this item won't be displayed. Default: True
  100. filter_null: Var[bool]
  101. # If set false, no cursor will be drawn when tooltip is active. Default: {"strokeWidth": 1, "fill": rx.color("gray", 3)}
  102. cursor: Var[Union[dict[str, Any], bool]] = LiteralVar.create(
  103. {
  104. "strokeWidth": 1,
  105. "fill": Color("gray", 3),
  106. }
  107. )
  108. # The box of viewing area, which has the shape of {x: someVal, y: someVal, width: someVal, height: someVal}, usually calculated internally.
  109. view_box: Var[dict[str, Any]]
  110. # The style of default tooltip content item which is a li element. Default: {"color": rx.color("gray", 12)}
  111. item_style: Var[dict[str, Any]] = LiteralVar.create(
  112. {
  113. "color": Color("gray", 12),
  114. }
  115. )
  116. # The style of tooltip wrapper which is a dom element. Default: {}
  117. wrapper_style: Var[dict[str, Any]]
  118. # The style of tooltip content which is a dom element. Default: {"background": rx.color("gray", 1), "borderColor": rx.color("gray", 4), "borderRadius": "8px"}
  119. content_style: Var[dict[str, Any]] = LiteralVar.create(
  120. {
  121. "background": Color("gray", 1),
  122. "borderColor": Color("gray", 4),
  123. "borderRadius": "8px",
  124. }
  125. )
  126. # The style of default tooltip label which is a p element. Default: {"color": rx.color("gray", 11)}
  127. label_style: Var[dict[str, Any]] = LiteralVar.create({"color": Color("gray", 11)})
  128. # This option allows the tooltip to extend beyond the viewBox of the chart itself. Default: {"x": False, "y": False}
  129. allow_escape_view_box: Var[dict[str, bool]]
  130. # If set true, the tooltip is displayed. If set false, the tooltip is hidden, usually calculated internally. Default: False
  131. active: Var[bool]
  132. # If this field is set, the tooltip position will be fixed and will not move anymore.
  133. position: Var[dict[str, Any]]
  134. # The coordinate of tooltip which is usually calculated internally. Default: {"x": 0, "y": 0}
  135. coordinate: Var[dict[str, Any]]
  136. # If set false, animation of tooltip will be disabled. Default: True
  137. is_animation_active: Var[bool]
  138. # Specifies the duration of animation, the unit of this option is ms. Default: 1500
  139. animation_duration: Var[int]
  140. # The type of easing function. Default: "ease"
  141. animation_easing: Var[LiteralAnimationEasing]
  142. class Label(Recharts):
  143. """A Label component in Recharts."""
  144. tag = "Label"
  145. alias = "RechartsLabel"
  146. # The box of viewing area, which has the shape of {x: someVal, y: someVal, width: someVal, height: someVal}, usually calculated internally.
  147. view_box: Var[dict[str, Any]]
  148. # The value of label, which can be specified by this props or the children of <Label />
  149. value: Var[str]
  150. # The offset of label which can be specified by this props or the children of <Label />
  151. offset: Var[int]
  152. # The position of label which can be specified by this props or the children of <Label />
  153. position: Var[LiteralPosition]
  154. class LabelList(Recharts):
  155. """A LabelList component in Recharts."""
  156. tag = "LabelList"
  157. alias = "RechartsLabelList"
  158. # The key of a group of label values in data.
  159. data_key: Var[str | int]
  160. # The position of each label relative to it view box. "Top" | "left" | "right" | "bottom" | "inside" | "outside" | "insideLeft" | "insideRight" | "insideTop" | "insideBottom" | "insideTopLeft" | "insideBottomLeft" | "insideTopRight" | "insideBottomRight" | "insideStart" | "insideEnd" | "end" | "center"
  161. position: Var[LiteralPosition]
  162. # The offset to the specified "position". Default: 5
  163. offset: Var[int]
  164. # The fill color of each label. Default: rx.color("gray", 10)
  165. fill: Var[str | Color] = LiteralVar.create(Color("gray", 10))
  166. # The stroke color of each label. Default: "none"
  167. stroke: Var[str | Color] = LiteralVar.create("none")
  168. class Cell(Recharts):
  169. """A Cell component in Recharts."""
  170. tag = "Cell"
  171. alias = "RechartsCell"
  172. # The presentation attribute of a rectangle in bar or a sector in pie.
  173. fill: Var[str | Color]
  174. # The presentation attribute of a rectangle in bar or a sector in pie.
  175. stroke: Var[str | Color]
  176. responsive_container = ResponsiveContainer.create
  177. legend = Legend.create
  178. graphing_tooltip = GraphingTooltip.create
  179. label = Label.create
  180. label_list = LabelList.create
  181. cell = Cell.create