Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support alt.datum[signal_name] in addition to "datum[signal_name]" #3366

Open
joelostblom opened this issue Mar 15, 2024 · 1 comment
Open

Comments

@joelostblom
Copy link
Contributor

Currently using alt.datum in altair does not support passing a signal/parameter whereas writing the expression string directly as JS-like string does (an example of dynamically passing fields via parameters here https://github.com/altair-viz/altair/pull/3357/files (both the chart title and calculate transform).

Here is another example:

import altair as alt
from vega_datasets import data

dropdown = alt.binding_select(
    options=['Horsepower', 'Displacement', 'Weight_in_lbs', 'Acceleration'],
    name='X-axis column '
)
xcol_param = alt.param(
    value='Horsepower',
    bind=dropdown
)

alt.Chart(data.cars.url).mark_circle().encode(
    x=alt.X('x:Q').title(''),
    y='Miles_per_Gallon:Q',
    color='Origin:N'
).transform_calculate(
    x=f'datum[{xcol_param}]'
    # x=alt.datum[xcol_param.name]
).add_params(
    xcol_param
)

The reason it fails is that alt.datum[xcol_param.name] inserts additional quotation around the parameter name in the VL spec:

"transform": [{"calculate": "datum['param_1']", "as": "x"}],

whereas the VL spec for f'datum[{xcol_param}]' does not:

"transform": [{"calculate": "datum[param_1]", "as": "x"}],

Open the Chart in the Vega Editor

Personally I think the cleanest would be if we could support just using the variable name without the .name suffix when using alt.datum, as in this example from the docs:

param_color_js_expr = alt.param(expr=f"{param_width.name} < 200 ? 'red' : 'black'")
param_color_py_expr = alt.param(expr=alt.expr.if_(param_width < 200, 'red', 'black'))

But even if that doesn't work, just supporting the use of the .name suffix without the quotes inserted would be great. Here are a few examples of how these datums and expressions get translated with differential quoting:

image

@jonmmease
Copy link
Contributor

Thanks for writing this up, I think alt.datum[xcol_param] rather than alt.datum[xcol_param.name] makes good sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants