我如何在 Vega-Lite 中格式化货币?

How do I format currency in Vega-Lite?

我正在尝试在 Vega-Lite 编辑器中将值格式化为货币。我正在尝试复制文档,但遇到了一个奇怪的错误。 Y轴是数值。传入格式字符串会给出“预期值”。

这是json:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Protocol Chart",
  "width": 500,
  "height": 225,
  "data": {
        "values": [
        {
            "asset": "eth",
            "time": "2021-06-15T00:00:00Z",
            "ReferenceRateUSD": "2577.04473863238"
        },
        {
            "asset": "eth",
            "time": "2021-06-16T00:00:00Z",
            "ReferenceRateUSD": "2552.74103641146"
        },
        {
            "asset": "eth",
            "time": "2021-06-17T00:00:00Z",
            "ReferenceRateUSD": "2360.99938690824"
        }
    ]
  },
  "config": {
        "view": {
            "stroke": "transparent"
        }
  },
  "mark": "line",
    "encoding": {
        "x": {
            "axis": {
                "domainColor": "#DDD",
                "grid": false, 
                "labelColor": "#AEAEAE", 
                "ticks": false, 
                "labelPadding": 10
            }, 
            "field": "time", 
            "type": "temporal", 
            "title": ""
        },
        "y": {
            "axis": {
                "labelOffset": 2,
                "domainColor": "white",
                "labelColor": "#AEAEAE", 
                "ticks": false, 
                "labelPadding": 10,
                "format": '$.2f'
            }, 
            "field": "ReferenceRateUSD", 
            "type": "quantitative", 
            "title": "", 
            "scale": {
                "zero": false
            }
        },
        "color": {
            "field": "doesntmatter", 
            "type": "nominal", 
            "legend": null,
            "scale": {
                "range": ["#91DB97"]
            }
        }
  }
}

我在这里错过了什么?如何让它接受我的格式字符串?

"$.2f" 看起来是正确的 d3-format string for currency, but note that this is only valid if the associated formatType is "number" (see axis label docs).

由于您没有完整 reproducible example 您遇到的问题,我只能猜测您的数据类型不是数字,这就是格式化失败的原因。如果不是这种情况,我建议编辑您的问题以提供您所看到的错误的完整示例。


编辑:您的完整示例似乎可以与 vega/vega-lite (view in editor) 的当前版本一起正常工作:

也许您需要更新 vega/vega-lite 库?