首页 > 解决方案 > use custom functions in transform_calculate in altair

问题描述

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

标签: pythonaltair

解决方案


计算转换由 Vega 渲染器在 Javascript 中评估,因此不能包含在 Python 中定义的自定义功能。计算转换必须是包含明确定义的 javascript 语法子集的字符串,使用https://vega.github.io/vega/docs/expressions/中列出的任何功能


推荐阅读