当在图例中使用的查找中链接的字段时,Vega-lite 图表失败

Vega-lite chart fails when field linked in lookup used in legend

为什么这个 Vega-lite 代码无法显示图表?它使用转换查找来连接 tables,并且图例中使用了第二个 table 中的字段之一。

Vega Editor Link

{
  "data": {    
          "values": [ 
            {"group": 1, "person": "Alan"},
            {"group": 1, "person": "George"},
            {"group": 1, "person": "Fred"},
            {"group": 2, "person": "Steve"},
            {"group": 2, "person": "Nick"},
            {"group": 2, "person": "Will"},
            {"group": 2, "person": "Cole"},
            {"group": 3, "person": "Rick"},
            {"group": 3, "person": "Tom"}
          ]},
  "transform": [
    {
      "lookup": "person",
      "from": {
        "data": {    
          "values": [
              {"name": "Alan", "_source": { "age": 10, "category": 15}},
              {"name": "Tom", "_source": { "age": 7, "category": 35}},
              {"name": "Fred", "_source": { "age": 17, "category": 75}}
          ]},
        "key": "name",
        "fields": ["_source.age", "_source.category"]
      }
    },
    {"calculate": "datum.person+' '+datum._source.category", "as": "legend"},
    {"aggregate": 
      [{"op": "sum", "field": "_source.age", "as": "totalage"}], 
      "groupby": ["totalage", "legend", "_source.category"]}
  ],
  "mark": "bar",
  "encoding": {
    "x": {"field": "_source.category", "type": "ordinal"},
    "y": {"field": "totalage", "type": "quantitative"},
    "color": {
      "field": "legend",
      "title": "My Legend",
      "legend": {"orient": "top", "columns": 3}
    }
  }
}

但是这个 Vega-lite 代码成功显示了图表?它使用转换查找来连接 tables 和 none 来自第二个 table 的字段用于图例中。

Vega Editor Link

{
  "data": {    
          "values": [ 
            {"group": 1, "person": "Alan"},
            {"group": 1, "person": "George"},
            {"group": 1, "person": "Fred"},
            {"group": 2, "person": "Steve"},
            {"group": 2, "person": "Nick"},
            {"group": 2, "person": "Will"},
            {"group": 2, "person": "Cole"},
            {"group": 3, "person": "Rick"},
            {"group": 3, "person": "Tom"}
          ]},
  "transform": [
    {
      "lookup": "person",
      "from": {
        "data": {    
          "values": [
              {"name": "Alan", "_source": { "age": 10, "category": 15}},
              {"name": "Tom", "_source": { "age": 7, "category": 35}},
              {"name": "Fred", "_source": { "age": 17, "category": 75}}
          ]},
        "key": "name",
        "fields": ["_source.age", "_source.category"]
      }
    },
    {"calculate": "datum.person+' '+datum.group", "as": "legend"},
    {"aggregate": 
      [{"op": "sum", "field": "_source.age", "as": "totalage"}], 
      "groupby": ["totalage", "legend", "_source.category"]}
  ],
  "mark": "bar",
  "encoding": {
    "x": {"field": "_source.category", "type": "ordinal"},
    "y": {"field": "totalage", "type": "quantitative"},
    "color": {
      "field": "legend",
      "title": "My Legend",
      "legend": {"orient": "top", "columns": 3}
    }
  }
}

问题在行

{"calculate": "datum.person+' '+datum._source.category", "as": "legend"}

这表示要查找 datum._sourcecategory 属性。但是您的数据没有名为 "_source" 的列,它有一个名为 "_source.category".

的列

要修复它,您可以像这样引用该列:

{"calculate": "datum.person+' '+datum['_source.category']", "as": "legend"}

Open the Chart in the Vega Editor