Py.Cafe

moksha159/

vizro-waterfall-chart

Interactive Waterfall Chart Using Vizro and Plotly

DocsPricing
  • app.py
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import pandas as pd
import plotly.graph_objects as go
import vizro.models as vm
from vizro import Vizro
from vizro.models.types import capture


@capture("graph")
def waterfall(
    data_frame: pd.DataFrame,
    x: str,
    y: str,
    measure: list[str],
):
    return go.Figure(
        data=go.Waterfall(
            x=data_frame[x], y=data_frame[y], measure=data_frame[measure]
        ),
        layout={"showlegend": False},
    )


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",
        ],
    }
)

fig = waterfall(waterfall_data, x="x", y="y", measure="measure")

page = vm.Page(title="My page", components=[vm.Graph(figure=fig)])
dashboard = vm.Dashboard(pages=[page])
Vizro().build(dashboard).run()