general.py 8.8 KB

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