general.py 7.9 KB

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