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

在Vega-Lite(Airtable中)实现带日期过滤的累计求和折线图

解决Airtable中Vega-Lite折线图的过滤与多折线累计求和问题

核心修改要点

  • 添加过滤逻辑:用isValid操作符筛除Latest plan date为空的数据
  • 调整窗口计算:如需多折线分组累计,在window变换中加入groupby指定分组字段
  • 扩展图层配置:复制图层块并修改样式/绑定分组条件,实现多折线叠加

修改后的完整配置代码

{
  "transform": [
    // 过滤空日期数据
    {
      "filter": "isValid(datum['Latest plan date'])"
    },
    // 按日期排序+分组累计求和(替换分组字段为实际业务字段)
    {
      "sort": [{"field": "Latest plan date"}],
      "groupby": ["分组字段"], // 比如"Plan Type",用于区分不同折线
      "window": [{
        "op": "sum",
        "field": "Impact to baseline",
        "as": "planned_cumul"
      }]
    }
  ],
  "layer": [
    // 第一条折线:褐红色
    {
      "mark": {
        "type": "line",
        "stroke": "maroon"
      },
      "encoding": {
        "x": {
          "field": "Latest plan date",
          "type": "temporal",
          "title": ""
        },
        "y": {
          "field": "planned_cumul",
          "type": "quantitative"
        },
        "detail": {"field": "分组字段"} // 绑定分组,确保折线独立计算
      }
    },
    // 第二条折线:蓝色
    {
      "mark": {
        "type": "line",
        "stroke": "blue"
      },
      "encoding": {
        "x": {
          "field": "Latest plan date",
          "type": "temporal",
          "title": ""
        },
        "y": {
          "field": "planned_cumul",
          "type": "quantitative"
        },
        "detail": {"field": "分组字段"}
      }
    },
    // 第三条折线:绿色
    {
      "mark": {
        "type": "line",
        "stroke": "green"
      },
      "encoding": {
        "x": {
          "field": "Latest plan date",
          "type": "temporal",
          "title": ""
        },
        "y": {
          "field": "planned_cumul",
          "type": "quantitative"
        },
        "detail": {"field": "分组字段"}
      }
    }
  ]
}

关键说明

  1. 过滤生效逻辑:isValid(datum['Latest plan date'])会自动排除日期为空或格式无效的记录,确保累计计算仅基于有效数据
  2. 分组累计注意:如果3条折线对应不同数据分组,必须保留groupby参数,否则所有数据会被合并为一条累计折线
  3. 折线自定义:可修改每个图层的stroke颜色,或给特定图层单独添加过滤条件(比如在图层内加transform.filter),实现差异化展示
  4. 字段替换:将代码中的分组字段替换为Airtable中实际用于区分折线的字段名称(如"计划类别")

内容的提问来源于stack exchange,提问作者Richard W

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 23:42:21