有条件去除刻度线?

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

deneb 没有标签的地方可以去掉刻度线吗?

我们还希望保留尽可能多的网格线。

之前
Before

之后
After

我尝试使用文档中的条件标签/,但无法让我的表达式尊重上图中的标签。

json powerbi linechart vega-lite deneb
1个回答
0
投票

这是一个示例,为您展示了可能的解决方案。 我将值除以 10,并将其舍入为小数点后 0。如果它仍然等于自身除以 10,那么我们对勾进行颜色标记,否则我们将其设置为透明。

我们不想更改 tickSize,因为那样的话值将会错位。

enter image description here

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {"url": "data/population.json"},
  "transform": [
    {"filter": "datum.year == 2000"},
    {"calculate": "datum.sex == 2 ? 'Female' : 'Male'", "as": "gender"}
  ],
  "height": {"step": 17},
  "encoding": {
    "y": {
      "field": "age",
      "axis": {
        "tickSize": 8,
        "tickColor": {
          "expr": "round((datum.value/10),0) == (datum.value/10) ? 'black':'transparent'"
        }
      }
    }
  },
  "layer": [
    {
      "mark": "bar",
      "encoding": {
        "x": {
          "aggregate": "sum",
          "field": "people",
          "title": "population",
          "stack": "normalize"
        },
        "color": {"field": "gender", "scale": {"range": ["#675193", "#ca8861"]}}
      }
    },
    {
      "mark": {"type": "text", "opacity": 0.9, "color": "white"},
      "encoding": {
        "x": {
          "aggregate": "sum",
          "field": "people",
          "title": "population",
          "stack": "normalize",
          "bandPosition": 0.5
        },
        "text": {"aggregate": "sum", "field": "people", "title": "population"},
        "detail": {"field": "gender"}
      }
    }
  ]
}
© www.soinside.com 2019 - 2024. All rights reserved.