# Vizro is an open-source toolkit for creating modular data visualization applications.
# check out https://github.com/mckinsey/vizro for more info about Vizro
# and checkout https://vizro.readthedocs.io/en/stable/ for documentation.
import pandas as pd
import plotly.graph_objects as go
import vizro.models as vm
import vizro.plotly.express as px
from vizro import Vizro
from vizro.figures import kpi_card, kpi_card_reference
from vizro.models._components.form._text_area import TextArea
from vizro.models._components.form._user_input import UserInput
from vizro.models.types import capture
from vizro.tables import dash_ag_grid
iris = px.data.iris()
tips = px.data.tips()
stocks = px.data.stocks(datetimes=True)
gapminder_2007 = px.data.gapminder().query("year == 2007")
df_kpi = pd.DataFrame({"Actual": [100, 200, 700], "Reference": [100, 300, 500], "Category": ["A", "B", "C"]})
waterfall_data = pd.DataFrame(
{
"x": [
"Sales",
"Consulting",
"Net revenue",
"Purchases",
"Other expenses",
"Profit before tax",
],
"y": [60, 80, 0, -40, -20, 0],
"measure": [
"relative",
"relative",
"total",
"relative",
"relative",
"total",
],
}
)
@capture("graph")
def waterfall(data_frame: pd.DataFrame, x: str, y: str, measure: list[str]):
"""Custom waterfall chart."""
fig = go.Figure(
data=go.Waterfall(
x=data_frame[x],
y=data_frame[y],
measure=data_frame[measure],
showlegend=False,
),
)
fig.update_layout(showlegend=False)
return fig
@capture("graph")
def sankey(data_frame: pd.DataFrame, source: str, target: str, value: str):
"""Custom Sankey diagram."""
fig = go.Figure(
data=[
go.Sankey(
node={"label": ["A1", "A2", "B1", "B2", "C1", "C2"]},
link={
"source": [0, 1, 0, 2, 3, 3], # indices correspond to labels, eg A1, A2, A1, B1, ...
"target": [2, 3, 3, 4, 4, 5],
"value": [8, 4, 2, 8, 4, 2],
},
)
]
)
return fig
graphs = vm.Page(
title="Graphs",
layout=vm.Grid(grid=[[0, 1, 2], [3, 4, 5], [6, 7, 8]]),
components=[
vm.Graph(
figure=px.scatter(
gapminder_2007,
x="gdpPercap",
y="lifeExp",
size="pop",
size_max=60,
color="continent",
)
),
vm.Graph(figure=px.box(tips, y="total_bill", x="day", color="day")),
vm.Graph(figure=px.histogram(tips, x="total_bill")),
vm.Graph(
figure=px.histogram(
tips,
y="day",
x="total_bill",
color="sex",
barmode="group",
orientation="h",
)
),
vm.Graph(figure=px.density_heatmap(tips, x="day", y="size", z="tip", histfunc="avg", text_auto="$.2f")),
vm.Graph(figure=px.histogram(tips, x="sex", y="total_bill", color="day")),
vm.Graph(figure=waterfall(waterfall_data, x="x", y="y", measure="measure")),
vm.Graph(figure=px.pie(tips, values="tip", names="day", hole=0.85)),
vm.Graph(
figure=px.bar(
gapminder_2007.query("country.isin(['United States', 'Pakistan', 'India', 'China', 'Indonesia'])"),
x="pop",
y="country",
orientation="h",
)
),
],
)
sankey_page = vm.Page(
title="Sankey",
components=[
vm.Graph(
title="Energy flow",
figure=sankey(pd.DataFrame(), source="source", target="target", value="value"),
footer="Sample energy flow from sources to sectors.",
),
],
)
# DASHBOARD -------------------------------------------------------------------
dashboard = vm.Dashboard(
pages=[
graphs,
],
)
Vizro().build(dashboard).run()