You need to enable JavaScript to run this app. Vega Lite仪表盘跨图表筛选:实现多维度AND联动过滤问题
优惠活动
大模型
产品
解决方案
定价
更多

Vega Lite仪表盘跨图表筛选:实现多维度AND联动过滤问题

Vega Lite 数据仪表盘联动筛选逻辑异常问题

我在使用Vega Lite开发数据仪表盘时,遇到联动筛选逻辑不符合预期的问题。当前仪表盘包含三类图表:

  • 第一幅为按日期维度统计的每日累计购买金额时间序列柱状图
  • 第二幅为按支付类型聚合统计的总购买金额分类型柱状图
  • 第三幅为按历史购买客群维度聚合统计的总购买金额分群柱状图

目前已实现基础联动交互:点击下方两个维度图表中的任意柱形,顶部的时间序列图表会根据选中的维度值完成数据筛选,效果可参考对应截图。但当前交互存在核心问题:当同时从「支付类型」维度选择一个分类、从「历史购买客群」维度选择另一个分类时,顶部图表的筛选逻辑为两个选中值的OR逻辑(即满足任意一个选中条件即纳入统计),预期需要实现的是AND筛选逻辑(即同时满足两个维度的选中条件才纳入统计)。

以下为可直接在Vega Editor中运行的复现代码,需要提供可实现多维度AND筛选逻辑的调整方案:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v4.json",
  "data": {
    "values": [
      {"date": "2020-10-01","purchase_amount": 30,
        "purchase_type": "cash","previous_purchaser": "no"},
      {"date": "2020-10-01","purchase_amount": 60,
        "purchase_type": "cash","previous_purchaser": "yes"},
      {"date": "2020-10-01","purchase_amount": 40,
        "purchase_type": "credit","previous_purchaser": "yes"},
      {"date": "2020-10-01","purchase_amount": 40,
        "purchase_type": "credit","previous_purchaser": "no"},
      {"date": "2020-10-02","purchase_amount": 60,
        "purchase_type": "cash","previous_purchaser": "no"},
      {"date": "2020-10-02","purchase_amount": 90,
        "purchase_type": "cash","previous_purchaser": "yes"},
      {"date": "2020-10-02","purchase_amount": 110,
        "purchase_type": "credit","previous_purchaser": "yes"},
      {"date": "2020-10-02","purchase_amount": 80,
        "purchase_type": "credit","previous_purchaser": "no"},
      {"date": "2020-10-03","purchase_amount": 80,
        "purchase_type": "cash","previous_purchaser": "no"},
      {"date": "2020-10-03","purchase_amount": 60,
        "purchase_type": "cash","previous_purchaser": "yes"},
      {"date": "2020-10-03","purchase_amount": 100,
        "purchase_type": "credit","previous_purchaser": "yes"},
      {"date": "2020-10-03","purchase_amount": 100,
        "purchase_type": "credit","previous_purchaser": "yes"}]},
  "vconcat": [
    {
      "width": 350,
      "height": 80,
      "transform": [{"filter": {"selection": "click"}}],
      "mark": {"type": "bar"},
      "encoding": {
        "x": {"field": "date"},
        "y": {"aggregate": "sum", "field": "purchase_amount"}
      },
      "selection": {"brush": {"encodings": ["x"], "type": "interval"}}
    },
    {
      "vconcat": [
        {
          "width": 325,
          "transform": [
            {"filter": {"selection": "brush"}},
            {"calculate": "datum.purchase_amount", "as": "purchase_amt"}
          ],
          "mark": {"type": "bar"},
          "encoding": {
            "x": {"field": "purchase_amt", "aggregate": "sum"},
            "y": {"field": "purchase_type", "title": "Purchase Type"},
            "color": {
              "condition": {
                "selection": "click",
                "field": "purchase_type",
                "legend": null
              },
              "value": "lightgrey"
            }
          },
          "selection": {"click": {"encodings": ["color"], "type": "multi"}}
        },
        {
          "width": 325,
          "transform": [
            {"filter": {"selection": "brush"}},
            {"calculate": "datum.purchase_amount", "as": "purchase_amt"}
          ],
          "mark": {"type": "bar"},
          "encoding": {
            "x": {"field": "purchase_amt", "aggregate": "sum"},
            "y": {
              "field": "previous_purchaser",
              "type": "nominal",
              "title": "Previous Purchaser"
            },
            "color": {
              "condition": {
                "selection": "click",
                "field": "previous_purchaser"
              },
              "value": "lightgrey"
            }
          },
          "selection": {"click": {"encodings": ["color"], "type": "multi"}}
        }
      ]
    }
  ]
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.16 19:45:08