or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

advanced-features.mdcharts-visualization.mdevents-interaction.mdindex.mdlayout-navigation.mdtheming-styling.mdui-controls.mdutilities-platform.md

charts-visualization.mddocs/

0

# Charts and Visualization

1

2

This document covers Flet's data visualization capabilities, including built-in charts and third-party integrations for creating interactive and beautiful data visualizations.

3

4

## Import

5

6

```python

7

import flet as ft

8

```

9

10

## Built-in Chart Controls

11

12

### BarChart

13

14

```python { .api }

15

class BarChart(Control):

16

"""Bar chart visualization control."""

17

18

def __init__(

19

self,

20

bar_groups: List[BarChartGroup] = None,

21

baseline: float = None,

22

border_data: ChartBorderData = None,

23

grid_data: ChartGridData = None,

24

left_axis: ChartAxis = None,

25

top_axis: ChartAxis = None,

26

right_axis: ChartAxis = None,

27

bottom_axis: ChartAxis = None,

28

horizontal_grid_lines: ChartGridLines = None,

29

vertical_grid_lines: ChartGridLines = None,

30

bgcolor: str = None,

31

tooltip_bgcolor: str = None,

32

max_y: float = None,

33

min_y: float = None,

34

interactive: bool = True,

35

expand: bool = None,

36

**kwargs

37

)

38

```

39

40

**Parameters:**

41

- `bar_groups` (List[BarChartGroup], optional): Groups of bars to display

42

- `baseline` (float, optional): Y-axis baseline value

43

- `border_data` (ChartBorderData, optional): Chart border styling

44

- `left_axis` (ChartAxis, optional): Left Y-axis configuration

45

- `bottom_axis` (ChartAxis, optional): Bottom X-axis configuration

46

- `max_y` (float, optional): Maximum Y-axis value

47

- `min_y` (float, optional): Minimum Y-axis value

48

- `interactive` (bool, optional): Enable hover and interaction

49

50

**Example:**

51

```python

52

ft.BarChart(

53

bar_groups=[

54

ft.BarChartGroup(

55

x=0,

56

bar_rods=[

57

ft.BarChartRod(

58

from_y=0,

59

to_y=40,

60

width=40,

61

color=ft.colors.AMBER,

62

tooltip="January: 40"

63

)

64

]

65

),

66

ft.BarChartGroup(

67

x=1,

68

bar_rods=[

69

ft.BarChartRod(

70

from_y=0,

71

to_y=100,

72

width=40,

73

color=ft.colors.BLUE,

74

tooltip="February: 100"

75

)

76

]

77

)

78

],

79

left_axis=ft.ChartAxis(

80

labels_size=40,

81

title=ft.Text("Sales"),

82

title_size=40

83

),

84

bottom_axis=ft.ChartAxis(

85

labels=[

86

ft.ChartAxisLabel(value=0, label=ft.Text("Jan")),

87

ft.ChartAxisLabel(value=1, label=ft.Text("Feb"))

88

],

89

labels_size=40

90

),

91

horizontal_grid_lines=ft.ChartGridLines(

92

color=ft.colors.GREY_300,

93

width=1,

94

dash_pattern=[3, 3]

95

)

96

)

97

```

98

99

### BarChartGroup

100

101

```python { .api }

102

class BarChartGroup(Control):

103

"""Group of bars in a bar chart."""

104

105

def __init__(

106

self,

107

x: float,

108

bar_rods: List[BarChartRod] = None,

109

barsSpace: float = None,

110

show_tooltip: bool = True,

111

**kwargs

112

)

113

```

114

115

**Parameters:**

116

- `x` (float): X-axis position for this group

117

- `bar_rods` (List[BarChartRod], optional): Individual bars in this group

118

- `barsSpace` (float, optional): Space between bars in group

119

- `show_tooltip` (bool, optional): Show tooltip on hover

120

121

### BarChartRod

122

123

```python { .api }

124

class BarChartRod(Control):

125

"""Individual bar in a bar chart."""

126

127

def __init__(

128

self,

129

from_y: float,

130

to_y: float,

131

width: float = None,

132

color: str = None,

133

border_radius: BorderRadiusValue = None,

134

border_side: BorderSide = None,

135

gradient: Gradient = None,

136

rod_stack_items: List[BarChartRodStackItem] = None,

137

show_tooltip: bool = True,

138

tooltip: str = None,

139

**kwargs

140

)

141

```

142

143

**Parameters:**

144

- `from_y` (float): Starting Y value (usually 0)

145

- `to_y` (float): Ending Y value (bar height)

146

- `width` (float, optional): Bar width

147

- `color` (str, optional): Bar color

148

- `border_radius` (BorderRadiusValue, optional): Rounded corners

149

- `gradient` (Gradient, optional): Gradient fill

150

- `rod_stack_items` (List[BarChartRodStackItem], optional): Stacked segments

151

- `tooltip` (str, optional): Custom tooltip text

152

153

### LineChart

154

155

```python { .api }

156

class LineChart(Control):

157

"""Line chart visualization control."""

158

159

def __init__(

160

self,

161

data_series: List[LineChartData] = None,

162

border_data: ChartBorderData = None,

163

grid_data: ChartGridData = None,

164

left_axis: ChartAxis = None,

165

top_axis: ChartAxis = None,

166

right_axis: ChartAxis = None,

167

bottom_axis: ChartAxis = None,

168

horizontal_grid_lines: ChartGridLines = None,

169

vertical_grid_lines: ChartGridLines = None,

170

bgcolor: str = None,

171

tooltip_bgcolor: str = None,

172

max_x: float = None,

173

max_y: float = None,

174

min_x: float = None,

175

min_y: float = None,

176

interactive: bool = True,

177

point_line_start: ChartPointLine = None,

178

point_line_end: ChartPointLine = None,

179

expand: bool = None,

180

**kwargs

181

)

182

```

183

184

**Parameters:**

185

- `data_series` (List[LineChartData], optional): Line data series

186

- `max_x` (float, optional): Maximum X-axis value

187

- `max_y` (float, optional): Maximum Y-axis value

188

- `min_x` (float, optional): Minimum X-axis value

189

- `min_y` (float, optional): Minimum Y-axis value

190

- `interactive` (bool, optional): Enable hover and interaction

191

192

**Example:**

193

```python

194

ft.LineChart(

195

data_series=[

196

ft.LineChartData(

197

data_points=[

198

ft.LineChartDataPoint(1, 1),

199

ft.LineChartDataPoint(3, 1.5),

200

ft.LineChartDataPoint(5, 1.4),

201

ft.LineChartDataPoint(7, 3.4),

202

ft.LineChartDataPoint(10, 2),

203

ft.LineChartDataPoint(12, 2.2),

204

ft.LineChartDataPoint(13, 1.8)

205

],

206

stroke_width=8,

207

color=ft.colors.LIGHT_GREEN,

208

curved=True,

209

stroke_cap_round=True

210

)

211

],

212

border_data=ft.ChartBorderData(

213

border=ft.Border(

214

bottom=ft.BorderSide(4, ft.colors.with_opacity(0.5, ft.colors.ON_SURFACE))

215

)

216

),

217

left_axis=ft.ChartAxis(

218

labels=[

219

ft.ChartAxisLabel(

220

value=1,

221

label=ft.Container(ft.Text("1K"), padding=10)

222

),

223

ft.ChartAxisLabel(

224

value=2,

225

label=ft.Container(ft.Text("2K"), padding=10)

226

)

227

],

228

labels_size=40

229

),

230

bottom_axis=ft.ChartAxis(

231

labels=[

232

ft.ChartAxisLabel(

233

value=2,

234

label=ft.Container(ft.Text("MAR"), padding=10)

235

),

236

ft.ChartAxisLabel(

237

value=7,

238

label=ft.Container(ft.Text("JUN"), padding=10)

239

),

240

ft.ChartAxisLabel(

241

value=12,

242

label=ft.Container(ft.Text("SEP"), padding=10)

243

)

244

],

245

labels_size=32

246

),

247

horizontal_grid_lines=ft.ChartGridLines(

248

color=ft.colors.with_opacity(0.2, ft.colors.ON_SURFACE),

249

width=1

250

),

251

tooltip_bgcolor=ft.colors.with_opacity(0.8, ft.colors.BLUE_GREY),

252

max_y=4,

253

min_y=0,

254

interactive=True

255

)

256

```

257

258

### LineChartData

259

260

```python { .api }

261

class LineChartData(Control):

262

"""Line data series for line chart."""

263

264

def __init__(

265

self,

266

data_points: List[LineChartDataPoint] = None,

267

stroke_width: float = 2,

268

color: str = None,

269

gradient: Gradient = None,

270

curved: bool = False,

271

stroke_cap_round: bool = False,

272

prevent_curve_over_shooting: bool = False,

273

is_stroke_cap_round: bool = None,

274

dash_pattern: List[int] = None,

275

shadow: BoxShadow = None,

276

below_line: ChartAreaData = None,

277

above_line: ChartAreaData = None,

278

point: ChartPointShape = None,

279

show_tooltip: bool = True,

280

**kwargs

281

)

282

```

283

284

**Parameters:**

285

- `data_points` (List[LineChartDataPoint], optional): Points on the line

286

- `stroke_width` (float, optional): Line thickness

287

- `color` (str, optional): Line color

288

- `curved` (bool, optional): Smooth curved line

289

- `dash_pattern` (List[int], optional): Dash pattern for dashed lines

290

- `below_line` (ChartAreaData, optional): Fill area below line

291

- `point` (ChartPointShape, optional): Point markers on line

292

293

### LineChartDataPoint

294

295

```python { .api }

296

class LineChartDataPoint(Control):

297

"""Individual data point in line chart."""

298

299

def __init__(

300

self,

301

x: float,

302

y: float,

303

tooltip: str = None,

304

tooltip_style: TextStyle = None,

305

selected_below_line: ChartAreaData = None,

306

selected_point: ChartPointShape = None,

307

show_above_line: bool = True,

308

show_below_line: bool = True,

309

show_tooltip: bool = True,

310

**kwargs

311

)

312

```

313

314

**Parameters:**

315

- `x` (float): X-axis coordinate

316

- `y` (float): Y-axis coordinate

317

- `tooltip` (str, optional): Custom tooltip text

318

- `show_tooltip` (bool, optional): Show tooltip on hover

319

320

### PieChart

321

322

```python { .api }

323

class PieChart(Control):

324

"""Pie chart visualization control."""

325

326

def __init__(

327

self,

328

sections: List[PieChartSection] = None,

329

sections_space: float = 2,

330

start_degree_offset: float = 0,

331

center_space_radius: float = None,

332

center_space_color: str = None,

333

center_space_child: Control = None,

334

expand: bool = None,

335

**kwargs

336

)

337

```

338

339

**Parameters:**

340

- `sections` (List[PieChartSection], optional): Pie chart sections

341

- `sections_space` (float, optional): Space between sections

342

- `start_degree_offset` (float, optional): Starting angle offset

343

- `center_space_radius` (float, optional): Center hole radius (donut chart)

344

- `center_space_child` (Control, optional): Widget in center hole

345

346

**Example:**

347

```python

348

ft.PieChart(

349

sections=[

350

ft.PieChartSection(

351

value=25,

352

color=ft.colors.BLUE,

353

radius=50,

354

title="25%",

355

title_style=ft.TextStyle(

356

size=16, color=ft.colors.WHITE, weight=ft.FontWeight.BOLD

357

)

358

),

359

ft.PieChartSection(

360

value=25,

361

color=ft.colors.YELLOW,

362

radius=45,

363

title="25%"

364

),

365

ft.PieChartSection(

366

value=25,

367

color=ft.colors.PURPLE,

368

radius=45,

369

title="25%"

370

),

371

ft.PieChartSection(

372

value=25,

373

color=ft.colors.GREEN,

374

radius=45,

375

title="25%"

376

)

377

],

378

sections_space=0,

379

center_space_radius=0,

380

expand=True

381

)

382

```

383

384

### PieChartSection

385

386

```python { .api }

387

class PieChartSection(Control):

388

"""Section of a pie chart."""

389

390

def __init__(

391

self,

392

value: float,

393

color: str = None,

394

radius: float = None,

395

title: str = None,

396

title_style: TextStyle = None,

397

title_position_percentage_offset: float = None,

398

badge: Control = None,

399

badge_position_percentage_offset: float = None,

400

gradient: Gradient = None,

401

border_side: BorderSide = None,

402

**kwargs

403

)

404

```

405

406

**Parameters:**

407

- `value` (float): Section value/percentage

408

- `color` (str, optional): Section color

409

- `radius` (float, optional): Section radius

410

- `title` (str, optional): Section title text

411

- `title_style` (TextStyle, optional): Title styling

412

- `badge` (Control, optional): Custom badge widget

413

- `gradient` (Gradient, optional): Gradient fill

414

415

## Chart Components and Styling

416

417

### ChartAxis

418

419

```python { .api }

420

class ChartAxis(Control):

421

"""Chart axis configuration."""

422

423

def __init__(

424

self,

425

labels: List[ChartAxisLabel] = None,

426

labels_size: float = None,

427

labels_interval: float = None,

428

title: Control = None,

429

title_size: float = None,

430

show_labels: bool = True,

431

labels_text_style: TextStyle = None,

432

**kwargs

433

)

434

```

435

436

**Parameters:**

437

- `labels` (List[ChartAxisLabel], optional): Custom axis labels

438

- `labels_size` (float, optional): Reserved space for labels

439

- `labels_interval` (float, optional): Interval between labels

440

- `title` (Control, optional): Axis title widget

441

- `show_labels` (bool, optional): Whether to show labels

442

443

### ChartAxisLabel

444

445

```python { .api }

446

class ChartAxisLabel(Control):

447

"""Individual axis label."""

448

449

def __init__(

450

self,

451

value: float,

452

label: Control,

453

**kwargs

454

)

455

```

456

457

### ChartGridLines

458

459

```python { .api }

460

class ChartGridLines(Control):

461

"""Chart grid lines styling."""

462

463

def __init__(

464

self,

465

color: str = None,

466

width: float = None,

467

dash_pattern: List[int] = None,

468

interval: float = None,

469

**kwargs

470

)

471

```

472

473

**Parameters:**

474

- `color` (str, optional): Grid line color

475

- `width` (float, optional): Grid line width

476

- `dash_pattern` (List[int], optional): Dash pattern for dashed lines

477

- `interval` (float, optional): Interval between grid lines

478

479

### ChartPointShape

480

481

```python { .api }

482

class ChartPointShape(Control):

483

"""Base class for chart point shapes."""

484

pass

485

486

class ChartCirclePoint(ChartPointShape):

487

"""Circular point marker."""

488

489

def __init__(

490

self,

491

color: str = None,

492

radius: float = None,

493

stroke_color: str = None,

494

stroke_width: float = None,

495

**kwargs

496

)

497

498

class ChartSquarePoint(ChartPointShape):

499

"""Square point marker."""

500

501

def __init__(

502

self,

503

color: str = None,

504

size: float = None,

505

stroke_color: str = None,

506

stroke_width: float = None,

507

**kwargs

508

)

509

510

class ChartCrossPoint(ChartPointShape):

511

"""Cross-shaped point marker."""

512

513

def __init__(

514

self,

515

color: str = None,

516

size: float = None,

517

width: float = None,

518

**kwargs

519

)

520

```

521

522

## Third-Party Chart Integration

523

524

### MatplotlibChart

525

526

```python { .api }

527

class MatplotlibChart(Control):

528

"""Matplotlib integration for advanced charting."""

529

530

def __init__(

531

self,

532

figure: matplotlib.figure.Figure = None,

533

original_size: bool = False,

534

isolated: bool = False,

535

**kwargs

536

)

537

```

538

539

**Parameters:**

540

- `figure` (matplotlib.figure.Figure, optional): Matplotlib figure object

541

- `original_size` (bool, optional): Use original figure size

542

- `isolated` (bool, optional): Isolate matplotlib backend

543

544

**Example:**

545

```python

546

import matplotlib.pyplot as plt

547

import matplotlib

548

matplotlib.use("svg") # Use SVG backend for Flet

549

550

fig, ax = plt.subplots()

551

552

fruits = ['apple', 'blueberry', 'cherry', 'orange']

553

counts = [40, 100, 30, 55]

554

bar_colors = ['tab:red', 'tab:blue', 'tab:orange', 'tab:orange']

555

556

ax.bar(fruits, counts, color=bar_colors)

557

ax.set_ylabel('fruit supply')

558

ax.set_title('Fruit supply by kind and color')

559

560

ft.MatplotlibChart(figure=fig, expand=True)

561

```

562

563

### PlotlyChart

564

565

```python { .api }

566

class PlotlyChart(Control):

567

"""Plotly integration for interactive charts."""

568

569

def __init__(

570

self,

571

figure: plotly.graph_objects.Figure = None,

572

original_size: bool = False,

573

isolated: bool = False,

574

**kwargs

575

)

576

```

577

578

**Parameters:**

579

- `figure` (plotly.graph_objects.Figure, optional): Plotly figure object

580

- `original_size` (bool, optional): Use original figure size

581

- `isolated` (bool, optional): Isolate plotly environment

582

583

**Example:**

584

```python

585

import plotly.graph_objects as go

586

587

fig = go.Figure()

588

fig.add_trace(go.Scatter(

589

x=[1, 2, 3, 4],

590

y=[10, 11, 12, 13],

591

mode='markers+lines',

592

name='Trace 1'

593

))

594

fig.add_trace(go.Scatter(

595

x=[1, 2, 3, 4],

596

y=[14, 15, 16, 17],

597

mode='markers+lines',

598

name='Trace 2'

599

))

600

601

ft.PlotlyChart(figure=fig, expand=True)

602

```

603

604

## Chart Examples and Patterns

605

606

### Multi-Series Bar Chart

607

608

```python

609

def create_multi_series_bar_chart():

610

return ft.BarChart(

611

bar_groups=[

612

ft.BarChartGroup(

613

x=0,

614

bar_rods=[

615

ft.BarChartRod(

616

from_y=0, to_y=40, width=20,

617

color=ft.colors.BLUE, tooltip="Q1: 40"

618

),

619

ft.BarChartRod(

620

from_y=0, to_y=60, width=20,

621

color=ft.colors.RED, tooltip="Q2: 60"

622

)

623

],

624

barsSpace=4

625

),

626

ft.BarChartGroup(

627

x=1,

628

bar_rods=[

629

ft.BarChartRod(

630

from_y=0, to_y=50, width=20,

631

color=ft.colors.BLUE, tooltip="Q1: 50"

632

),

633

ft.BarChartRod(

634

from_y=0, to_y=80, width=20,

635

color=ft.colors.RED, tooltip="Q2: 80"

636

)

637

],

638

barsSpace=4

639

)

640

],

641

max_y=100

642

)

643

```

644

645

### Stacked Bar Chart

646

647

```python

648

def create_stacked_bar_chart():

649

return ft.BarChart(

650

bar_groups=[

651

ft.BarChartGroup(

652

x=0,

653

bar_rods=[

654

ft.BarChartRod(

655

from_y=0, to_y=100, width=40,

656

rod_stack_items=[

657

ft.BarChartRodStackItem(0, 30, ft.colors.RED),

658

ft.BarChartRodStackItem(30, 70, ft.colors.GREEN),

659

ft.BarChartRodStackItem(70, 100, ft.colors.BLUE)

660

]

661

)

662

]

663

)

664

]

665

)

666

```

667

668

### Multi-Line Chart

669

670

```python

671

def create_multi_line_chart():

672

return ft.LineChart(

673

data_series=[

674

ft.LineChartData(

675

data_points=[

676

ft.LineChartDataPoint(0, 3),

677

ft.LineChartDataPoint(2.6, 2),

678

ft.LineChartDataPoint(4.9, 5),

679

ft.LineChartDataPoint(6.8, 2.5),

680

ft.LineChartDataPoint(8, 4),

681

ft.LineChartDataPoint(9.5, 3),

682

ft.LineChartDataPoint(11, 4)

683

],

684

stroke_width=3,

685

color=ft.colors.BLUE,

686

curved=True

687

),

688

ft.LineChartData(

689

data_points=[

690

ft.LineChartDataPoint(0, 1),

691

ft.LineChartDataPoint(2.6, 1.5),

692

ft.LineChartDataPoint(4.9, 3),

693

ft.LineChartDataPoint(6.8, 3.1),

694

ft.LineChartDataPoint(8, 2.8),

695

ft.LineChartDataPoint(9.5, 2.5),

696

ft.LineChartDataPoint(11, 2.8)

697

],

698

stroke_width=3,

699

color=ft.colors.RED,

700

curved=True

701

)

702

],

703

max_y=6,

704

min_y=0

705

)

706

```

707

708

### Donut Chart with Center Content

709

710

```python

711

def create_donut_chart():

712

return ft.PieChart(

713

sections=[

714

ft.PieChartSection(40, ft.colors.BLUE, radius=80, title="40%"),

715

ft.PieChartSection(30, ft.colors.RED, radius=80, title="30%"),

716

ft.PieChartSection(20, ft.colors.GREEN, radius=80, title="20%"),

717

ft.PieChartSection(10, ft.colors.YELLOW, radius=80, title="10%")

718

],

719

center_space_radius=40,

720

center_space_child=ft.Container(

721

content=ft.Text("Total\n100%", text_align=ft.TextAlign.CENTER),

722

alignment=ft.alignment.center

723

)

724

)

725

```

726

727

### Interactive Chart with Events

728

729

```python

730

def create_interactive_chart(page):

731

def on_chart_event(e):

732

page.add(ft.Text(f"Chart clicked: {e.data}"))

733

page.update()

734

735

return ft.LineChart(

736

data_series=[

737

ft.LineChartData(

738

data_points=[

739

ft.LineChartDataPoint(1, 1, tooltip="Point 1: (1,1)"),

740

ft.LineChartDataPoint(3, 4, tooltip="Point 2: (3,4)"),

741

ft.LineChartDataPoint(5, 2, tooltip="Point 3: (5,2)")

742

],

743

stroke_width=4,

744

color=ft.colors.CYAN,

745

point=ft.ChartCirclePoint(

746

radius=6,

747

color=ft.colors.CYAN,

748

stroke_color=ft.colors.BLUE,

749

stroke_width=2

750

)

751

)

752

],

753

interactive=True,

754

on_chart_event=on_chart_event

755

)

756

```

757

758

## Best Practices

759

760

### Data Preparation

761

```python

762

# Prepare data for charts

763

def prepare_chart_data(raw_data):

764

# Convert data to chart format

765

points = []

766

for i, value in enumerate(raw_data):

767

points.append(ft.LineChartDataPoint(i, value))

768

return points

769

770

# Dynamic chart updates

771

def update_chart_data(chart, new_data):

772

chart.data_series[0].data_points = prepare_chart_data(new_data)

773

page.update()

774

```

775

776

### Responsive Charts

777

```python

778

# Responsive chart sizing

779

def create_responsive_chart():

780

return ft.Container(

781

content=ft.LineChart(

782

# Chart configuration

783

),

784

width=lambda: min(600, page.window_width * 0.9),

785

height=400

786

)

787

```

788

789

### Chart Theming

790

```python

791

# Consistent chart theming

792

class ChartTheme:

793

PRIMARY_COLOR = ft.colors.BLUE

794

SECONDARY_COLOR = ft.colors.RED

795

GRID_COLOR = ft.colors.GREY_300

796

BACKGROUND_COLOR = ft.colors.WHITE

797

798

@staticmethod

799

def get_default_line_style():

800

return {

801

'stroke_width': 3,

802

'curved': True,

803

'point': ft.ChartCirclePoint(radius=4)

804

}

805

```

806

807

This covers Flet's comprehensive charting and visualization capabilities, enabling you to create professional, interactive data visualizations for any application needs.