有条件的X轴短月份标签(一月,二月等)

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

在 Deneb vega-lite for Power BI 中,我遇到了一个问题,由于该特定数据的性质,我的某段数据未标记为月初(04/01)未显示。数据。

但是,我希望保持刻度线的性质相同(从月初开始),标签出现在月中/月中(15 日)。

我尝试在这个例子中做类似标签表达式的事情:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"date": "2024-04-15T00:00:00Z", "value": 10},
      {"date": "2024-05-15T00:00:00Z", "value": 20},
      {"date": "2024-06-15T00:00:00Z", "value": 15},
      {"date": "2024-07-15T00:00:00Z", "value": 18},
      {"date": "2024-08-15T00:00:00Z", "value": 25},
      {"date": "2024-09-15T00:00:00Z", "value": 22},
      {"date": "2024-10-15T00:00:00Z", "value": 19},
      {"date": "2024-11-15T00:00:00Z", "value": 16},
      {"date": "2024-12-15T00:00:00Z", "value": 23},
      {"date": "2025-01-15T00:00:00Z", "value": 28},
      {"date": "2025-02-15T00:00:00Z", "value": 30},
      {"date": "2025-03-15T00:00:00Z", "value": 25}
    ]
  },
  "width": "container",
  "height": "container",
  "mark": "line",
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "title": "Date",
      "axis": {
        "labelExpr": "if(day(datum.value) === 15, timeFormat(datum.value, '%b'), null)"
      }
    },
    "y": {
      "field": "value",
      "type": "quantitative",
      "title": "Value"
    }
  }
}

但是我无法成功让标签出现。

这是我正在使用的原始数据的屏幕截图:SS

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

这是您要找的吗?

enter image description here

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      {"date": "2024-04-15T00:00:00Z", "value": 10},
      {"date": "2024-05-15T00:00:00Z", "value": 20},
      {"date": "2024-06-15T00:00:00Z", "value": 15},
      {"date": "2024-07-15T00:00:00Z", "value": 18},
      {"date": "2024-08-15T00:00:00Z", "value": 25},
      {"date": "2024-09-15T00:00:00Z", "value": 22},
      {"date": "2024-10-15T00:00:00Z", "value": 19},
      {"date": "2024-11-15T00:00:00Z", "value": 16},
      {"date": "2024-12-15T00:00:00Z", "value": 23},
      {"date": "2025-01-15T00:00:00Z", "value": 28},
      {"date": "2025-02-15T00:00:00Z", "value": 30},
      {"date": "2025-03-15T00:00:00Z", "value": 25}
    ]
  },
  "width": "container",
  "height": "container",
  "mark": "line",
  "encoding": {
    "x": {
      "field": "date",
      "type": "temporal",
      "title": "Date",
      "axis": {
        "tickCount": "day",
        "labelExpr": "date(datum.value) == 15? timeFormat(datum.value, '%b'): null",
        "gridOpacity":{"expr": "date(datum.value) == 15? 1: 0"},
        "tickOpacity":{"expr": "date(datum.value) == 15? 1: 0"}
      }
    },
    "y": {"field": "value", "type": "quantitative", "title": "Value"}
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.