如何在一行显示中组合文本和字段值

问题描述

我正在使用 Vega,但我被这个简单的问题困住了。我要展示

The yield is 43.67%

但是使用提供的样本我设法只显示值 43.67

{
  mark:
    {
      type: "text",align: "center",fontSize: 40,fontWeight: "bold"
    },encoding: 
    {
      "text": {"field": "Yield","type": "quantitative",format: ".2f"}
    }
}

是否可以在此值前添加一些文本并在其后添加 % 符号?

解决方法

添加这种复杂注释的最佳方法是使用 calculate transform;例如:

{
  mark:
    {
      type: "text",align: "center",fontSize: 40,fontWeight: "bold"
    },transform:
    [
      {"calculate": "'The yield is ' + datum.Yield + '%'","as": "annotated_yield"}
    ],encoding: 
    {
      "text": {"field": "annotated_yield","type": "nominal"}
    }
}

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...