feat: retention funnel chart tab with treatment line depth (Task C.1)
This commit is contained in:
@@ -1069,6 +1069,76 @@ def get_trust_durations(
|
||||
# --- Directorate/indication summary for Trust Comparison landing page ---
|
||||
|
||||
|
||||
def get_retention_funnel(
|
||||
db_path: Path,
|
||||
date_filter_id: str,
|
||||
chart_type: str,
|
||||
directory: Optional[str] = None,
|
||||
trust: Optional[str] = None,
|
||||
) -> list[dict]:
|
||||
"""Aggregate patient counts by treatment line depth for a retention funnel.
|
||||
|
||||
Level 3 = 1st drug, Level 4 = 2-drug pathway, Level 5 = 3-drug pathway, etc.
|
||||
Returns list of dicts sorted by depth ascending:
|
||||
[{depth: 1, label: "1 drug", patients: N, pct: 100.0}, ...]
|
||||
"""
|
||||
conn = sqlite3.connect(str(db_path))
|
||||
conn.row_factory = sqlite3.Row
|
||||
try:
|
||||
where = ["date_filter_id = ?", "chart_type = ?", "level >= 3"]
|
||||
params: list = [date_filter_id, chart_type]
|
||||
|
||||
if directory:
|
||||
where.append("directory = ?")
|
||||
params.append(directory)
|
||||
if trust:
|
||||
where.append("trust_name = ?")
|
||||
params.append(trust)
|
||||
|
||||
query = f"""
|
||||
SELECT level, SUM(value) AS patients
|
||||
FROM pathway_nodes
|
||||
WHERE {' AND '.join(where)}
|
||||
GROUP BY level
|
||||
ORDER BY level
|
||||
"""
|
||||
rows = conn.execute(query, params).fetchall()
|
||||
|
||||
if not rows:
|
||||
return []
|
||||
|
||||
result = []
|
||||
base_patients = 0
|
||||
for r in rows:
|
||||
level = r["level"]
|
||||
patients = r["patients"] or 0
|
||||
depth = level - 2 # level 3 → depth 1, level 4 → depth 2, etc.
|
||||
|
||||
if depth == 1:
|
||||
base_patients = patients
|
||||
|
||||
ordinal_labels = {
|
||||
1: "1st drug",
|
||||
2: "2nd drug",
|
||||
3: "3rd drug",
|
||||
}
|
||||
label = ordinal_labels.get(depth, f"{depth}th drug")
|
||||
|
||||
pct = round(patients / base_patients * 100, 1) if base_patients else 0
|
||||
result.append({
|
||||
"depth": depth,
|
||||
"label": label,
|
||||
"patients": patients,
|
||||
"pct": pct,
|
||||
})
|
||||
|
||||
return result
|
||||
except sqlite3.Error:
|
||||
return []
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
|
||||
def get_directorate_summary(
|
||||
db_path: Path,
|
||||
date_filter_id: str,
|
||||
|
||||
@@ -1754,3 +1754,69 @@ def create_trust_duration_figure(
|
||||
fig.update_layout(**layout)
|
||||
|
||||
return fig
|
||||
|
||||
|
||||
def create_retention_funnel_figure(
|
||||
data: list[dict],
|
||||
title: str = "",
|
||||
) -> go.Figure:
|
||||
"""Create a retention funnel showing patient drop-off by treatment line depth.
|
||||
|
||||
Args:
|
||||
data: List of dicts with keys: depth, label, patients, pct
|
||||
title: Chart title from filter state.
|
||||
|
||||
Returns:
|
||||
Plotly Figure with go.Funnel trace.
|
||||
"""
|
||||
if not data:
|
||||
return go.Figure()
|
||||
|
||||
display_title = f"Treatment Retention — {title}" if title else "Treatment Retention"
|
||||
|
||||
labels = [d["label"] for d in data]
|
||||
patients = [d["patients"] for d in data]
|
||||
pcts = [d["pct"] for d in data]
|
||||
|
||||
# NHS blue gradient: darkest at top (most patients) → lightest at bottom
|
||||
funnel_colors = [
|
||||
"#003087", # NHS Heritage Blue (1st drug)
|
||||
"#005EB8", # NHS Blue
|
||||
"#1E88E5", # Mid blue
|
||||
"#42A5F5", # Light blue
|
||||
"#90CAF9", # Pale blue
|
||||
]
|
||||
colors = funnel_colors[: len(data)]
|
||||
if len(colors) < len(data):
|
||||
colors.extend(["#E3F2FD"] * (len(data) - len(colors)))
|
||||
|
||||
text_values = [
|
||||
f"{p:,} patients ({pct}%)" for p, pct in zip(patients, pcts)
|
||||
]
|
||||
|
||||
fig = go.Figure(
|
||||
go.Funnel(
|
||||
y=labels,
|
||||
x=patients,
|
||||
text=text_values,
|
||||
textposition="inside",
|
||||
textfont=dict(family=CHART_FONT_FAMILY, size=14, color="white"),
|
||||
marker=dict(color=colors),
|
||||
connector=dict(line=dict(color=GRID_COLOR, width=1)),
|
||||
hovertemplate=(
|
||||
"<b>%{y}</b><br>"
|
||||
"Patients: %{x:,}<br>"
|
||||
"%{text}<extra></extra>"
|
||||
),
|
||||
)
|
||||
)
|
||||
|
||||
layout = _base_layout(display_title)
|
||||
layout.update(
|
||||
margin=dict(t=60, l=8, r=8, b=40),
|
||||
yaxis=dict(automargin=True),
|
||||
height=max(300, len(data) * 80 + 120),
|
||||
)
|
||||
fig.update_layout(**layout)
|
||||
|
||||
return fig
|
||||
|
||||
Reference in New Issue
Block a user