Skip to content

Commit a5f1339

Browse files
committed
Support only one legend encoding channel
Vega-Lite does not allow for more than one channel when binding a parameter to the legend
1 parent e7f6286 commit a5f1339

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

altair/vegalite/v5/api.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3821,7 +3821,7 @@ def interactive(
38213821
bind_x: bool = True,
38223822
bind_y: bool = True,
38233823
tooltip: bool = True,
3824-
legend: Union[bool, list] = True,
3824+
legend: bool | str = True,
38253825
) -> Self:
38263826
"""
38273827
Add common interactive elements to the chart.
@@ -3830,22 +3830,21 @@ def interactive(
38303830
----------
38313831
name : string
38323832
The parameter name to use for the axes scales. This name should be
3833-
unique among all parameters within the chart.
3833+
unique among all parameters within the chart
38343834
bind_x : boolean, default True
38353835
Bind the interactive scales to the x-axis
38363836
bind_y : boolean, default True
38373837
Bind the interactive scales to the y-axis
38383838
tooltip : boolean, default True,
38393839
Add a tooltip containing the encodings used in the chart
3840-
legend : boolean or list, default True
3841-
Make the legend clickable and control the opacity of the marks.
3842-
Can be set to a list indicating which encodings the legend
3843-
interactivity should include.
3840+
legend : boolean or string, default True
3841+
A single encoding channel to be used to create a clickable legend.
3842+
The deafult is to guess from the spec based on the most commonly used legend encodings.
38443843
38453844
Returns
38463845
-------
38473846
chart :
3848-
copy of self, with interactive axes added
3847+
copy of self, with interactivity added
38493848
38503849
"""
38513850
encodings: list[SingleDefUnitChannel_T] = []
@@ -3866,8 +3865,11 @@ def interactive(
38663865
}
38673866
else:
38683867
interactive_chart.mark.tooltip = tooltip
3868+
38693869
if legend:
3870-
if not isinstance(legend, list):
3870+
if isinstance(legend, str):
3871+
legend_encoding = legend
3872+
else:
38713873
# Set the legend to commonly used encodings by default
38723874
possible_legend_encodings = [
38733875
"color",
@@ -3878,36 +3880,36 @@ def interactive(
38783880
"radius", # TODO Untested
38793881
# "size", # TODO Currently size is not working, renders empty legend
38803882
]
3881-
defined_legend_encodings = [
3882-
enc for enc in possible_legend_encodings
3883-
if not isinstance(interactive_chart.encoding[enc], utils.schemapi.UndefinedType)
3884-
]
3885-
else:
3886-
defined_legend_encodings = legend
3887-
legend_selection = selection_point(
3888-
bind="legend",
3889-
encodings=defined_legend_encodings
3890-
)
3891-
for legend_encoding in defined_legend_encodings:
3892-
if not isinstance(
3883+
legend_encoding = next(
3884+
(
3885+
enc for enc in possible_legend_encodings
3886+
if not isinstance(interactive_chart.encoding[enc], utils.schemapi.UndefinedType)
3887+
),
3888+
None
3889+
)
3890+
3891+
if legend_encoding is not None:
3892+
if isinstance(
38933893
interactive_chart.encoding[legend_encoding]['type'],
38943894
utils.schemapi.UndefinedType
38953895
):
3896-
legend_encoding_type = interactive_chart.encoding[legend_encoding]['type']
3897-
else:
38983896
legend_encoding_type = interactive_chart.encoding[legend_encoding].to_dict(
38993897
context={'data': interactive_chart.data}
39003898
)['type']
3899+
else:
3900+
legend_encoding_type = interactive_chart.encoding[legend_encoding]['type']
39013901
if legend_encoding_type == 'nominal': # TODO Ideally this would work for ordinal data too
3902+
legend_selection = selection_point(
3903+
bind="legend",
3904+
encodings=[legend_encoding]
3905+
)
39023906
initial_computed_domain = param(expr=f"domain('{legend_encoding}')")
39033907
nonreactive_domain = param(react=False, expr=initial_computed_domain.name)
39043908
if isinstance(
39053909
interactive_chart.encoding[legend_encoding]['scale'],
39063910
utils.schemapi.UndefinedType
39073911
):
3908-
interactive_chart.encoding[legend_encoding]['scale'] = {
3909-
'domain': nonreactive_domain
3910-
}
3912+
interactive_chart.encoding[legend_encoding]['scale'] = {'domain': nonreactive_domain}
39113913
else:
39123914
interactive_chart.encoding[legend_encoding]['scale']['domain'] = nonreactive_domain
39133915

0 commit comments

Comments
 (0)