You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vega-Lite如何保留最新数据?过滤重复服务环境的旧Build值

解决Vega-Lite同一单元格显示多值问题,保留最后插入的记录

当同一service和env对应多条不同build的记录时,默认会显示所有匹配值。要只保留最后插入的记录,可通过数据转换实现:

实现步骤

通过添加插入顺序索引,筛选出每个service+env组合中索引最大的记录(即最后插入的条目):

修改后的完整代码

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "description": "Domain Breakout Breakout",
  "width": "container",
  "title": {
    "text": ["Build number by service name"],
    "align": "center",
    "dy": -10,
    "fontWeight": "bold",
    "color": "#4f597a",
    "fontSize": 13,
    "font": "Montserrat,sans-serif"
  },
  "config": {"axis": {"grid": true, "tickBand": "extent"}},
  "data": {
    "values": [
      {"service": "service1", "build": 5555, "env": "dev"},
      {"service": "service2", "build": 5555, "env": "test"},
      {"service": "service3", "build": 5555, "env": "staging"},
      {"service": "service4", "build": 5555, "env": "prod"},
      {"service": "service4", "build": 5225, "env": "prod"},
      {"service": "service4", "build": 5558, "env": "prod"}
    ]
  },
  "transform": [
    // 生成插入顺序索引
    {"window": [{"op": "row_number", "as": "insertIndex"}]},
    // 按service和env分组,计算每组最大索引(最后插入记录的索引)
    {"window": [{"op": "max", "field": "insertIndex", "as": "maxIndex"}], "groupby": ["service", "env"]},
    // 只保留每组中索引最大的记录
    {"filter": "datum.insertIndex === datum.maxIndex"},
    // 移除临时字段
    {"drop": ["insertIndex", "maxIndex"]}
  ],
  "layer": [
    {
      "mark": "rect",
      "width": 1000,
      "encoding": {
        "x": {
          "field": "env",
          "type": "ordinal",
          "sort": "descending",
          "axis": {
            "title": null,
            "labelAngle": 0,
            "labelFontWeight": "bold",
            "labelColor": "#4f597a",
            "labelFontSize": 20,
            "labelPadding": 20,
            "orient": "top"
          }
        },
        "y": {
          "field": "service",
          "type": "ordinal",
          "sort": {"field": "service", "order": "descending", "op": "sum"},
          "axis": {
            "title": null,
            "labelAngle": 0,
            "labelFontWeight": "bold",
            "labelColor": "#4f597a",
            "labelFontSize": 10,
            "labelPadding": 5
          }
        },
        "fill": {
          "legend": null,
          "field": "build",
          "type": "quantitative",
          "scale": {"range": ["#ecf9ff", "#c6efff", "#7ad9ff", "#42caff"]}
        }
      }
    },
    {
      "mark": {
        "type": "text",
        "fontSize": 8,
        "font": "Montserrat,sans-serif",
        "align": "center"
      },
      "encoding": {
        "x": {"field": "env", "type": "ordinal"},
        "y": {"field": "service", "type": "ordinal"},
        "text": {"field": "build"}
      }
    }
  ]
}

关键说明

  1. 生成插入索引:row_number函数按数据原始顺序生成递增索引,标记每条记录的插入顺序。
  2. 分组计算最大索引:按service和env分组,获取每组内的最大索引,对应最后插入的记录。
  3. 筛选目标记录:仅保留索引等于组内最大索引的记录,确保每个service+env单元格只显示最后插入的build值。

内容的提问来源于stack exchange,提问作者Ido Segal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 16:48:25