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

时间:2020-04-24 18:16:59

标签: vega-lite

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

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代码成功显示了图表吗?它使用转换查找来联接表,图例中不使用第二个表中的任何字段。

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}
    }
  }
}

1 个答案:

答案 0 :(得分:1)

问题出在那

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

这表示要查找category的{​​{1}}属性。但是您的数据没有名为datum._source的列,而是有名为"_source"的列。

要解决此问题,您可以像这样引用该列:

"_source.category"

Open the Chart in the Vega Editor