【问题标题】:How to remove an item from the legend in Vega-Lite如何从 Vega-Lite 中的图例中删除项目
【发布时间】:2023-02-17 02:44:24
【问题描述】:

是否可以从图例中删除项目? 图例中的类别数量是动态的,所以我不能为图例使用手动输入的条目列表。

如果我使用

"labelExpr": "datum.label == '_Support' ? null : datum.label",

它将删除标签,但保留其符号。

Before suppresion

After suppression

【问题讨论】:

    标签: vega-lite


    【解决方案1】:

    我在这里隐藏了 x 的符号:

    {
      "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
      "data": {
        "values": [
          {"category": "A", "group": "x", "value": 0.1},
          {"category": "A", "group": "y", "value": 0.6},
          {"category": "A", "group": "z", "value": 0.9},
          {"category": "B", "group": "x", "value": 0.7},
          {"category": "B", "group": "y", "value": 0.2},
          {"category": "B", "group": "z", "value": 1.1},
          {"category": "C", "group": "x", "value": 0.6},
          {"category": "C", "group": "y", "value": 0.1},
          {"category": "C", "group": "z", "value": 0.2}
        ]
      },
      "mark": "bar",
      "encoding": {
        "x": {"field": "category"},
        "y": {"field": "value", "type": "quantitative"},
        "xOffset": {"field": "group"},
        "color": {
          "field": "group",
          "legend": {"symbolSize": {"expr": "datum.label=='x'?0:100"}}
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-05-29
      • 2018-03-12
      • 2021-12-19
      • 2021-03-25
      • 2017-04-16
      • 2021-05-28
      • 1970-01-01
      • 2021-01-23
      • 1970-01-01
      相关资源
      最近更新 更多