在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": "分组字段"} } } ] }
关键说明
- 过滤生效逻辑:
isValid(datum['Latest plan date'])会自动排除日期为空或格式无效的记录,确保累计计算仅基于有效数据 - 分组累计注意:如果3条折线对应不同数据分组,必须保留
groupby参数,否则所有数据会被合并为一条累计折线 - 折线自定义:可修改每个图层的
stroke颜色,或给特定图层单独添加过滤条件(比如在图层内加transform.filter),实现差异化展示 - 字段替换:将代码中的
分组字段替换为Airtable中实际用于区分折线的字段名称(如"计划类别")
内容的提问来源于stack exchange,提问作者Richard W
相关产品推荐
相关产品推荐

