python - 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
解决方案
计算转换由 Vega 渲染器在 Javascript 中评估,因此不能包含在 Python 中定义的自定义功能。计算转换必须是包含明确定义的 javascript 语法子集的字符串,使用https://vega.github.io/vega/docs/expressions/中列出的任何功能
推荐阅读
- kubernetes - 挂载目录的所有权错误
- javascript - 反应前端工作,但对服务器的补丁请求是一个落后的渲染
- mysql - 如果我在 my.cnf 文件中多次写入具有不同值的字段,MySQL 会发生什么?
- node.js - Mongoose - model.find({}) 有效,但 model.findOne({}) 无效
- python - 当列表包含类对象的元素时如何应用地图功能?
- c# - 单独关闭 WPF 窗口以使用 X 按钮从喊叫应用程序创建新窗口
- powershell - powershell 在 azure repo 中迭代文件
- node.js - 如何更新/编辑现有的帖子/交易?
- javascript - 如何从带有 JSON 文件的 python 将此表单提交到指定的网站?
- django - 我想显示目录(图像)中的随机图像并在 Django 中的模板中显示