BarChart
BarChart
#
Bases: ConstrainedControl
Draws a bar chart.
animate_offset
#
animate_offset: AnimationValue | None = None
animate_opacity
#
animate_opacity: AnimationValue | None = None
animate_position
#
animate_position: AnimationValue | None = None
animate_rotation
#
animate_rotation: AnimationValue | None = None
animate_scale
#
animate_scale: AnimationValue | None = None
animation
#
animation: AnimationValue = field(
default_factory=lambda: Animation(
duration=Duration(milliseconds=150), curve=LINEAR
)
)
Controls chart implicit animation.
bottom
#
bottom: Number | None = None
The distance that the child's bottom edge is inset from the bottom of the stack.
Note
Effective only if this control is a descendant of one of the following:
Stack
control, Page.overlay
list.
bottom_axis
#
The appearance of the bottom axis, its title and labels.
col
#
col: ResponsiveNumber = 12
If a parent of this control is a ResponsiveRow
,
this property is used to determine
how many virtual columns of a screen this control will span.
Can be a number or a dictionary configured to have a different value for specific
breakpoints, for example col={"sm": 6}
.
This control spans the 12 virtual columns by default.
/// details | Dimensions type: info | Breakpoint | Dimension | |---|---| | xs | <576px | | sm | ≥576px | | md | ≥768px | | lg | ≥992px | | xl | ≥1200px | | xxl | ≥1400px | ///
disabled
#
disabled: bool = False
Every control has disabled
property which is False
by default - control and all
its children are enabled.
Note
The value of this property will be propagated down to all children controls recursively.
/// details | Example type: example For example, if you have a form with multiple entry controls you can disable them all together by disabling container:
///
expand
#
expand_loose
#
expand_loose: bool = False
Allows the control to expand along the main axis if space is available, but does not require it to fill all available space.
More information here.
groups
#
groups: list[BarChartGroup] = field(default_factory=list)
The list of BarChartGroup
s to draw.
horizontal_grid_lines
#
horizontal_grid_lines: ChartGridLines | None = None
Controls drawing of chart's horizontal lines.
left
#
left: Number | None = None
The distance that the child's left edge is inset from the left of the stack.
Note
Effective only if this control is a descendant of one of the following:
Stack
control, Page.overlay
list.
left_axis
#
The appearance of the left axis, its title and labels.
offset
#
offset: OffsetValue | None = None
Applies a translation transformation before painting the control.
The translation is expressed as an Offset
scaled to the control's size.
So, Offset(x=0.25, y=0)
, for example, will result in a horizontal translation
of one quarter the width of this control.
/// details | Example
type: example
The following example displays container at 0, 0
top left corner of a stack as
transform applies -1 * 100, -1 * 100
(offset * control's size
) horizontal and
vertical translations to the control:
on_animation_end
#
on_animation_end: (
ControlEventHandler[ConstrainedControl] | None
) = None
Called when animation completes.
Can be used to chain multiple animations.
The data
property of the event handler argument contains the name of the animation.
More information here.
on_event
#
on_event: EventHandler[BarChartEvent] | None = None
Fires when a bar is hovered or clicked.
opacity
#
opacity: Number = 1.0
Defines the transparency of the control.
Value ranges from 0.0
(completely transparent) to 1.0
(completely opaque
without any transparency).
parent
#
parent: BaseControl | None
The direct ancestor(parent) of this control.
It defaults to None
and will only have a value when this control is mounted
(added to the page tree).
The Page
control (which is the root of the tree) is an exception - it always
has parent=None
.
right
#
right: Number | None = None
The distance that the child's right edge is inset from the right of the stack.
Note
Effective only if this control is a descendant of one of the following:
Stack
control, Page.overlay
list.
right_axis
#
The appearance of the right axis, its title and labels.
rotate
#
rotate: RotateValue | None = None
Transforms this control using a rotation around its center.
The value of rotate
property could be one of the following types:
number
- a rotation in clockwise radians. Full circle360°
ismath.pi * 2
radians,90°
ispi / 2
,45°
ispi / 4
, etc.Rotate
- allows to specify rotationangle
as well asalignment
- the location of rotation center.
/// details | Example type: example For example:
///
scale
#
scale: ScaleValue | None = None
Scales this control along the 2D plane. Default scale factor is 1.0
, meaning no-scale.
Setting this property to 0.5
, for example, makes this control twice smaller, while 2.0
makes it twice larger.
Different scale multipliers can be specified for x
and y
axis, by setting
Control.scale
property to an instance of Scale
class.
Either scale
or scale_x
and scale_y
could be specified, but not all of them.
/// details | Example type: example
///
top
#
top: Number | None = None
The distance that the child's top edge is inset from the top of the stack.
Note
Effective only if this control is a descendant of one of the following:
Stack
control, Page.overlay
list.
top_axis
#
The appearance of the top axis, its title and labels.
vertical_grid_lines
#
vertical_grid_lines: ChartGridLines | None = None
Controls drawing of chart's vertical lines.
visible
#
visible: bool = True
Every control has visible
property which is True
by default - control is
rendered on the page. Setting visible
to False
completely prevents control (and
all its children if any) from rendering on a page canvas. Hidden controls cannot be
focused or selected with a keyboard or mouse and they do not emit any events.
build
#
Called once during control initialization to define its child controls. self.page is available in this method.
Examples#
Example 1#
import flet as ft
import flet_charts as fch
def main(page: ft.Page):
page.add(
fch.BarChart(
expand=True,
interactive=True,
max_y=110,
border=ft.Border.all(1, ft.Colors.GREY_400),
horizontal_grid_lines=fch.ChartGridLines(
color=ft.Colors.GREY_300, width=1, dash_pattern=[3, 3]
),
tooltip=fch.BarChartTooltip(
bgcolor=ft.Colors.with_opacity(0.5, ft.Colors.GREY_300),
border_radius=ft.BorderRadius.all(20),
),
left_axis=fch.ChartAxis(
label_size=40, title=ft.Text("Fruit supply"), title_size=40
),
right_axis=fch.ChartAxis(show_labels=False),
bottom_axis=fch.ChartAxis(
label_size=40,
labels=[
fch.ChartAxisLabel(
value=0, label=ft.Container(ft.Text("Apple"), padding=10)
),
fch.ChartAxisLabel(
value=1, label=ft.Container(ft.Text("Blueberry"), padding=10)
),
fch.ChartAxisLabel(
value=2, label=ft.Container(ft.Text("Cherry"), padding=10)
),
fch.ChartAxisLabel(
value=3, label=ft.Container(ft.Text("Orange"), padding=10)
),
],
),
groups=[
fch.BarChartGroup(
x=0,
rods=[
fch.BarChartRod(
from_y=0,
to_y=40,
width=40,
color=ft.Colors.GREEN,
border_radius=0,
),
],
),
fch.BarChartGroup(
x=1,
rods=[
fch.BarChartRod(
from_y=0,
to_y=100,
width=40,
color=ft.Colors.BLUE,
tooltip=fch.BarChartRodTooltip("Blueberry"),
border_radius=0,
),
],
),
fch.BarChartGroup(
x=2,
rods=[
fch.BarChartRod(
from_y=0,
to_y=30,
width=40,
color=ft.Colors.RED,
border_radius=0,
),
],
),
fch.BarChartGroup(
x=3,
rods=[
fch.BarChartRod(
from_y=0,
to_y=60,
width=40,
color=ft.Colors.ORANGE,
tooltip=fch.BarChartRodTooltip("Orange"),
border_radius=0,
),
],
),
],
)
)
ft.run(main)
Example 2#
import flet as ft
import flet_charts as fch
class CustomRod(fch.BarChartRod):
def __init__(self, y: float, hovered: bool = False):
super().__init__()
self.hovered = hovered
self.y = y
self.width = 22
self.color = ft.Colors.WHITE
self.bg_to_y = 20
self.bg_color = ft.Colors.GREEN_300
def before_update(self):
super().before_update()
self.to_y = self.y + 0.5 if self.hovered else self.y
self.color = ft.Colors.YELLOW if self.hovered else ft.Colors.WHITE
self.border_side = (
ft.BorderSide(width=1, color=ft.Colors.RED)
if self.hovered
else ft.BorderSide(width=1, color=ft.Colors.BLUE)
)
def main(page: ft.Page):
def on_chart_event(e: fch.BarChartEvent):
if e.type == fch.ChartEventType.POINTER_HOVER:
for group_index, group in enumerate(chart.groups):
for rod_index, rod in enumerate(group.rods):
rod.hovered = (
e.group_index == group_index and e.rod_index == rod_index
)
chart.update()
chart = fch.BarChart(
on_event=on_chart_event,
interactive=True,
groups=[
fch.BarChartGroup(x=0, rods=[CustomRod(5)]),
fch.BarChartGroup(x=1, rods=[CustomRod(6.5)]),
fch.BarChartGroup(x=2, rods=[CustomRod(15)]),
fch.BarChartGroup(x=3, rods=[CustomRod(7.5)]),
fch.BarChartGroup(x=4, rods=[CustomRod(9)]),
fch.BarChartGroup(x=5, rods=[CustomRod(11.5)]),
fch.BarChartGroup(x=6, rods=[CustomRod(6)]),
],
bottom_axis=fch.ChartAxis(
labels=[
fch.ChartAxisLabel(value=0, label=ft.Text("M", color=ft.Colors.BLUE)),
fch.ChartAxisLabel(value=1, label=ft.Text("T", color=ft.Colors.YELLOW)),
fch.ChartAxisLabel(value=2, label=ft.Text("W", color=ft.Colors.BLUE)),
fch.ChartAxisLabel(value=3, label=ft.Text("T", color=ft.Colors.YELLOW)),
fch.ChartAxisLabel(value=4, label=ft.Text("F", color=ft.Colors.BLUE)),
fch.ChartAxisLabel(value=5, label=ft.Text("S", color=ft.Colors.YELLOW)),
fch.ChartAxisLabel(value=6, label=ft.Text("S", color=ft.Colors.BLUE)),
],
),
)
page.add(
ft.Container(
content=chart,
bgcolor=ft.Colors.GREEN_200,
padding=10,
border_radius=5,
expand=True,
)
)
ft.run(main)