forked from marimo-team/marimo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunnel_chart.py
More file actions
215 lines (171 loc) · 5.13 KB
/
funnel_chart.py
File metadata and controls
215 lines (171 loc) · 5.13 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# /// script
# requires-python = ">=3.10"
# dependencies = [
# "marimo",
# "pandas==2.3.3",
# "plotly==6.5.1",
# ]
# ///
import marimo
__generated_with = "0.20.2"
app = marimo.App(width="medium")
@app.cell
def _():
import marimo as mo
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
return go, mo, pd, px
@app.cell
def _(mo):
mo.md("""
# Reactive Plotly Funnel Chart Selection
This example demonstrates reactive funnel chart selections with `mo.ui.plotly`.
Funnel charts visualise drop-off at each stage of a sequential process (e.g. a
sales pipeline or marketing funnel). Each segment represents one stage; the bar
width shows the absolute count and the percent metrics show retention relative to
the initial, previous, and overall totals.
Try:
- **Click** any funnel stage to select it — the table below updates instantly
- **Drag a box** (set `dragmode="select"`) over multiple stages to select a range
- Switch between the multi-trace and `FunnelArea` tabs to explore variants
""")
return
@app.cell
def _(pd):
pipeline_data = pd.DataFrame(
{
"stage": [
"Leads",
"Qualified",
"Proposal",
"Negotiation",
"Closed Won",
],
"count": [5000, 2800, 1400, 620, 310],
"region": ["Global"] * 5,
}
)
return (pipeline_data,)
@app.cell
def _(mo, pipeline_data, px):
mo.md("## Single-Trace Funnel (`px.funnel`)")
fig_funnel = px.funnel(
pipeline_data,
x="count",
y="stage",
title="Sales Pipeline — click a stage to inspect it",
labels={"count": "Leads", "stage": "Stage"},
)
fig_funnel.update_layout(
dragmode="select",
margin=dict(l=120),
)
funnel_plot = mo.ui.plotly(fig_funnel)
funnel_plot
return (funnel_plot,)
@app.cell
def _(funnel_plot, mo, pipeline_data):
_pts = funnel_plot.value
if _pts:
_stage = _pts[0].get("y") or _pts[0].get("label")
_row = pipeline_data[pipeline_data["stage"] == _stage]
_pct_initial = _pts[0].get("percentInitial", 1.0)
_summary = (
f"**{_stage}** — "
f"{_row['count'].iloc[0]:,} leads — "
f"{_pct_initial:.0%} retention from start"
)
else:
_summary = "Click any funnel stage to inspect it."
mo.md(f"""
### Selected Stage
{_summary}
**Raw selection:** {funnel_plot.value}
""")
return
@app.cell
def _(mo, pd):
mo.md("## Multi-Trace Funnel — Regional Breakdown")
regional = pd.DataFrame(
{
"stage": [
"Leads", "Qualified", "Proposal", "Negotiation", "Closed Won",
"Leads", "Qualified", "Proposal", "Negotiation", "Closed Won",
],
"count": [3000, 1600, 900, 380, 190, 2000, 1200, 500, 240, 120],
"region": ["North America"] * 5 + ["Europe"] * 5,
}
)
return (regional,)
@app.cell
def _(mo, px, regional):
fig_regional = px.funnel(
regional,
x="count",
y="stage",
color="region",
title="Regional Pipeline — select stages to compare regions",
)
fig_regional.update_layout(dragmode="select")
regional_plot = mo.ui.plotly(fig_regional)
regional_plot
return (regional_plot,)
@app.cell
def _(mo, regional, regional_plot):
_pts = regional_plot.value
if _pts:
_rows = regional[
regional["stage"].isin(
[p.get("y") or p.get("label") for p in _pts if p]
)
]
_table = mo.ui.table(_rows.reset_index(drop=True))
else:
_table = mo.ui.table(regional.iloc[0:0])
mo.vstack([
mo.md(f"**{len(_pts)} stage(s) selected** | Indices: {regional_plot.indices}"),
_table,
])
return
@app.cell
def _(go, mo):
mo.md("## Funnel Area Chart (`go.Funnelarea`)")
fig_area = go.Figure(
go.Funnelarea(
labels=["Awareness", "Interest", "Consideration", "Intent", "Purchase"],
values=[10000, 6000, 3000, 1200, 400],
textinfo="label+percent",
hovertemplate=(
"<b>%{label}</b><br>"
"Count: %{value:,}<br>"
"Retention from start: %{percentInitial:.1%}<br>"
"Retention from previous: %{percentPrevious:.1%}"
"<extra></extra>"
),
)
)
fig_area.update_layout(title="Marketing Funnel Area — click a segment")
area_plot = mo.ui.plotly(fig_area)
area_plot
return (area_plot,)
@app.cell
def _(area_plot, mo):
_pts = area_plot.value
if _pts:
_p = _pts[0]
_detail = (
f"**{_p.get('label')}** — "
f"{_p.get('value', 0):,} users — "
f"{_p.get('percentInitial', 1.0):.1%} of total"
)
else:
_detail = "Click any segment to inspect it."
mo.md(f"""
### Selected Segment
{_detail}
**Raw selection:** {area_plot.value}
""")
return
if __name__ == "__main__":
app.run()