use custom functions in transform_calculate in altair

Viewed 1226

i have a bar chart, that i want to add approximation of each bar value, above each itself. that value is a big number, i have a function that formats number e.g.: 10,000$ to 10k$. how can i apply that.

 base = alt.Chart(target_df_hahu).mark_bar().encode(
    alt.X('monthdate(date):O'),
    alt.Y('value'),
    color = 'variable'         
)

i have try blow code .

text = base.mark_text().encode(
    text = 'value:Q'
).transform_calculate(
    value=custom_function(datum.value)
)
base+text
1 Answers

Calculate transforms are evaluated in Javascript by the Vega renderer, and thus cannot contain custom functionality defined in Python. Calculate transforms must be strings containing a well-defined subset of javascript syntax, making use of any of the functionality listed at https://vega.github.io/vega/docs/expressions/

Related