Skip to content

Commit 4342c44

Browse files
committed
Add rules with tooltip values for deciles chart
* Add a separate Chart with mark_rule to make it clear which month you're looking at * use selection_point(nearest=True) so that the user doesn't have to hit the exact x coordinate to view data * Use the new Chart to aggregate the deciles into one big tooltip
1 parent 139dd3b commit 4342c44

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

openprescribing/web/charts.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,45 @@ def build_org_chart_spec(analysis):
1919
stroke_width = (
2020
alt.when(alt.datum.centile == 50).then(alt.value(3)).otherwise(alt.value(1))
2121
)
22-
chart_spec = (
22+
23+
nearest = alt.selection_point(
24+
nearest=True, on="pointerover", fields=["month"], empty=False
25+
)
26+
27+
# If we use columns on its own, it seems that Javascript automagically orders the
28+
# list numerically. So do a bit of a dance creating tooltip_columns so that we
29+
# can retain the ordering with p90 at the top of the list (to match the p90 line
30+
# at the top of the chart).
31+
columns = [str(n) for n in range(10, 100, 10)]
32+
tooltip_columns = [f"p{c}" for c in columns]
33+
34+
# Draw a rule at the location of the selection
35+
deciles_rules = (
36+
alt.Chart(alt.NamedData("deciles"))
37+
.transform_pivot("centile", value="value", groupby=["month"])
38+
# rename the centiles keys so that we can reverse the order later
39+
.transform_calculate(**{f"p{c}": f"datum['{c}']" for c in columns})
40+
.mark_rule(color="gray")
41+
.encode(
42+
x="month:T",
43+
opacity=alt.when(nearest).then(alt.value(0.3)).otherwise(alt.value(0)),
44+
tooltip=[
45+
alt.Tooltip("month:T", title="Month", format="%Y %b"),
46+
*[
47+
alt.Tooltip(c, type="quantitative")
48+
for c in reversed(tooltip_columns)
49+
],
50+
],
51+
)
52+
.add_params(nearest)
53+
)
54+
deciles_chart = (
2355
alt.Chart(alt.NamedData("deciles"))
2456
.mark_line(color="#3182BD")
2557
.encode(x=x, y=y, detail="centile:O", strokeWidth=stroke_width)
2658
)
59+
chart_spec = alt.layer(deciles_chart, deciles_rules)
60+
2761
all_orgs_line_chart = (
2862
alt.Chart(alt.NamedData("all_orgs_line"))
2963
.mark_line(color="grey", opacity=0.2)

0 commit comments

Comments
 (0)