【问题标题】:Alternating stripes with vega与 vega 交替条纹
【发布时间】:2021-12-14 15:18:30
【问题描述】:

我们如何使用 vega 添加交替的网格线或条纹?

假设我们从这个例子开始:

https://vega.github.io/vega/examples/grouped-bar-chart/

在编辑器中:

https://vega.github.io/editor/#/examples/vega/grouped-bar-chart

这就是它的样子:

我们如何改变图形,使背景中的 A 和 C 有交替的水平条纹,但 B 没有?

换句话说,如果我们想为“A”和“C”显示灰色背景条纹,而为“B”显示白色条纹。

类似这样的:

【问题讨论】:

    标签: javascript data-visualization vega


    【解决方案1】:

    您可以添加一个标记

    {
      "type": "group",
      "data": [
        {
          "name": "data_category",
          "source": "table",
          "transform": [
            {
              "type": "aggregate",
              "groupby": ["category"],
              "fields": ["category"],
              "ops": ["count"]
            },
            {"type": "identifier", "as": "category_id"}
          ]
        }
      ],
      "marks": [
        {
          "name": "category_background",
          "type": "rect",
          "from": {"data": "data_category"},
          "encode": {
            "enter": {
              "y": {"scale": "yscale", "field": "category"},
              "height": {"scale": "yscale", "band": 1},
              "width": {"signal": "width"},
              "fill": {
                "signal": "datum['category_id'] % 2 == 0 ? '#eee' : '#ccc'"
              },
              "fillOpacity": {"value": 0.6}
            }
          }
        }
      ]
    },
    

    参考文献

    组标记https://vega.github.io/vega/docs/marks/group/

    标识符变换https://vega.github.io/vega/docs/transforms/identifier/

    Open in Vega Editor

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2017-12-13
    • 2020-05-07
    • 2012-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多