如何显示y轴标称标签,无论是否有数据

问题描述 投票:0回答:1

我有一些简单的条形图,有时某些 y 轴标称类别没有任何与之关联的数据。但是,出于一致性目的,我仍然想在图表上显示这些值。这是一个简化的示例:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Bar chart with text labels. Set domain to make the frame cover the labels.",
  "data": {
    "values": [
      {"a": "A", "b": null},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43},
      {"a": "D", "b": null},
      {"a": "E", "b": 20}
    ]
  },
  "encoding": {
    "y": {"field": "a", "type": "nominal"},
    "x": {"field": "b", "type": "quantitative"}
  },
  "layer": [{
    "mark": "point"
  }]
}

它返回给我这个:

enter image description here

但我想看到以下内容:

enter image description here

我设法通过将它们设为 0 并添加以下条件来做到这一点:

"opacity": {
   "condition":{"test": "datum['b'] == 0", "value": 0}, "value": 1
}

但想知道是否有更优雅的方法,特别是因为我的用例更复杂并且取决于日期,而不是数字

在 Vega 编辑器中打开图表

json charts visualization vega-lite
1个回答
0
投票

问题是 VL 在底层 Vega 中生成一个 isValid 过滤器。你的做法很好。这是一个替代方案:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Bar chart with text labels. Set domain to make the frame cover the labels.",
  "data": {
    "values": [
      {"a": "A", "b": -1},
      {"a": "B", "b": 55},
      {"a": "C", "b": 43},
      {"a": "D", "b": -1},
      {"a": "E", "b": 20}
    ]
  },
  "encoding": {
    "y": {"field": "a", "type": "nominal"},
    "x": {"field": "b", "type": "quantitative", "scale":{"domainMin":0}}
  },
  "layer": [{ "mark":{"type": "point", "clip":true}  }]
}
© www.soinside.com 2019 - 2024. All rights reserved.