Skip to content

Commit 617c737

Browse files
committed
TODO: improve callback fn
1 parent 3c4e7e3 commit 617c737

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

chartlets.py/my_extension/my_panel_1.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,11 @@ def make_figure(ctx: Context, selected_dataset: int = 0) -> alt.Chart:
8787

8888

8989
@panel.callback(
90-
Input("plot", property="points"), State("plot", "chart"), Output("plot", "chart")
90+
Input("plot", property="points"),
91+
State("plot", "chart.encoding"),
92+
Output("plot", "chart.encoding.color"),
9193
)
92-
def get_click_event_points(ctx: Context, points, plot) -> alt.Chart:
94+
def get_click_event_points(ctx: Context, points: dict, encoding: dict) -> dict:
9395
"""
9496
This callback function shows how we can use the event handlers output
9597
(property="points") which was defined in the `make_figure` callback
@@ -105,8 +107,8 @@ def get_click_event_points(ctx: Context, points, plot) -> alt.Chart:
105107
conditions = []
106108
for field, values in points.items():
107109
if field != "vlPoint":
110+
field_type = encoding.get(field, {}).get("type", "")
108111
for value in values:
109-
field_type = plot["encoding"].get(field, {}).get("type", "")
110112
if field_type == "nominal":
111113
conditions.append(f"datum.{field} === '{value}'")
112114
else:
@@ -115,13 +117,11 @@ def get_click_event_points(ctx: Context, points, plot) -> alt.Chart:
115117

116118
condition_expr = " && ".join(conditions)
117119

118-
plot["encoding"]["color"] = {
120+
return {
119121
"condition": {
120122
"test": condition_expr,
121123
# Highlight color when the condition is true
122124
"value": "orange",
123125
},
124126
"value": "steelblue", # Default color
125127
}
126-
127-
return alt.Chart.from_dict(plot)

0 commit comments

Comments
 (0)