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

如何在Vega Lite的lookup转换中对二级数据源做前置聚合

解决方案

Vega Lite不支持直接在lookup转换的from.data参数内嵌套写转换规则,你可以通过顶层datasets配置提前完成二级数据源的预处理,具体修改方式如下:

  1. 在JSON配置的根节点新增datasets字段,提前定义并预转换test.csv数据源:按country_txt分组聚合,统计每个国家的出现次数
  2. 原有lookup逻辑直接引用预处理完成的数据集即可

修改后的完整配置示例

{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "title": "OK",
  "width": 1080,
  "height": 1080,
  // 新增datasets节点预定义二级数据源
  "datasets": {
    "aggregated_country_data": {
      "url": "data/test.csv",
      "format": {"type": "csv"},
      "transform": [
        {
          "aggregate": [{"op": "count", "as": "country_appear_count"}],
          "groupby": ["country_txt"]
        }
      ]
    }
  },
  "data": {
    "url": "https://cdn.jsdelivr.net/npm/world-atlas@2/countries-110m.json",
    "format": {
      "type": "topojson",
      "feature": "countries"
    }
  },
  "transform": [
    {
      "lookup": "properties.name",
      "from": {
        // 直接引用预转换完成的数据集
        "data": {"name": "aggregated_country_data"},
        "key": "country_txt",
        // 可以按需取聚合后的统计字段
        "fields": ["country_txt", "country_appear_count"]
      }
    }
  ]
  // 后续补全你的mark、encoding配置即可
}

效果说明

按照你提供的test.csv样例数据,预聚合后的数据集结果为:

country_txtcountry_appear_count
Australia2
England1

之后lookup转换会直接用这个处理后的数据和主TopoJSON数据源关联。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 19:45:00