如何在 Vega-Lite 中用特定颜色为线条着色?

Ale*_*hin 3 vega-lite

VegaLite 自动分配颜色。金价是蓝色,银价是橙色,感觉不对。

在此处输入图片说明

我怎么可以指定明确的颜色-#F1C40F黄金和#95A5A6银?

我还想将data.values下面的示例代码中的as保留为一组单独的数组。

代码和游乐场

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "description": "Stock prices of 5 Tech Companies over Time.",
  "data": {
    "values": [
      {
        "dates": ["2000-01", "2000-02", "2000-03"], 
        "gold": [1, 2, 1], 
        "silver": [1.5, 1, 2]
      }
    ]
  },
  "transform": [
    {"flatten": ["dates", "gold", "silver"]},
    {"fold": ["gold", "silver"], "as": ["symbol", "price"]},
    {"calculate": "datetime(datum.dates)", "as": "dates"}
  ],
  "mark": {"type": "line", "point": {"filled": false, "fill": "white"}},
  "encoding": {
    "x": {"field": "dates", "type": "temporal", "timeUnit": "yearmonth"},
    "y": {"field": "price", "type": "quantitative"},
    "color": {"field": "symbol", "type": "nominal"}
  }
}
Run Code Online (Sandbox Code Playgroud)

jak*_*vdp 8

您可以使用和参数设置自定义配色方案scale.domainscale.range

"color": {
  "field": "symbol",
  "type": "nominal",
  "scale": {"domain": ["gold", "silver"], "range": ["#F1C40F", "#95A5A6"]}
}
Run Code Online (Sandbox Code Playgroud)

无论数据源是如何指定的,这都有效。

这是应用于您的图表(Vega Editor)时的结果:

在此处输入图片说明