Search code examples
vega-lite

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


Why does this Vega-lite code fail to display a chart? It uses a transform lookup to join tables and one of the fields from the second table is used in the legend.

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

But this Vega-lite code succeeds in displaying a chart? It uses a transform lookup to join tables and none of the fields from the second table are used in the legend.

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

Solution

  • The issue is in the line

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

    This says to look for the category attribute of datum._source. But your data does not have a column named "_source", it has a column named "_source.category".

    To fix it, you can reference the column like this:

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

    Open the Chart in the Vega Editor