Making a custom stacked bar chart using nested fields with Vega on Kibana

I'm trying to make a stacked bar chart in Vega that uses aggregation on nested fields and I've been able to successfully make an inverted version of it (y axis and color the incorrect way round) but can't get it to work the correct way.

the index data structure is Baz (foo end goal for Y axis) --- One to Many -> qux --- Many to One -> barr (barName end goal for X axis)

The code for the inverted graph that works but isn't the desired result is:

{
  $schema: https://vega.github.io/schema/vega-lite/v5.json
  description: A simple bar chart with embedded data.
  data: {
    name: foo_data
    url: {
      %context%: true
      %timefield%: corge
      index: plurghData
      body: {
        size: 0
        aggs: {
          bar: {
            nested: {
              path: bars
            }
            aggs: {
              bar_barFilter: {
                terms: {
                  field: bars.bar_barFilter
                  size: 10
                  order: {
                    top_reverse_nested.doc_count: desc
                  }
                }
                aggs: {
                  top_reverse_nested: {
                    reverse_nested: {
                    }
                    aggs: {
                      foo_buckets: {
                        terms: {
                          field: foo
                            order: {
                              _key: asc
                            }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
    format: {
      property: aggregations.bars.bar_barFilter.buckets
    }
  }
  transform: [
    {
      flatten: [
        top_reverse_nested.foo_buckets.buckets
      ]
      as: [
        foo
      ]
    }
  ]
  encoding: {
    x: {
      field: foo.doc_count
      title: Number
      type: quantitative
      axis: {
        titlePadding: 20
      }
    }
    y: {
      field: key
      title: Top 10
      type: nominal
      sort: desc
      axis: {
        labelLimit: 300
        titlePadding: 50
      }
    }
    color: {
      field: foo.key
      scale: {
        range: [
          "#209280"
          "#cc5642"
          "#e2634a"
          "#d6bf57"
          "#54b399"
          "#209280"
        ]
        type: ordinal
      }
    }
    tooltip: [
      {
        field: key
        title: Waldo
      }
      {
        field: foo.key
        type: nominal
        title: Fred
      }
      {
        field: foo.doc_count
        type: quantitative
        title: Number
      }
    ]
  }
  layer: [
    {
      mark: {
        type: bar
        height: {
          band: 0.8
        }
      }
      params: [
        {
          name: highlight
          select: {
            on: mouseover
            type: point
            empty: false
          }
        }
      ]
    }
    {
      transform: [
        {
          stack: foo.doc_count
          groupby: [
            key
          ]
          as: [
            lower
            upper
          ]
        }
        {
          calculate: (datum.lower + datum.upper) / 2
          as: midpoint
        }
        {
          joinaggregate:[{
            op: sum
            field: foo.doc_count
            as: total_doc_count
          }]
        }
        {
          filter: datum.upper - datum.lower > datum.total_doc_count * 0.005
        }
      ]
      mark: {
        type: text
        fill: black
        fontSize: 13
      }
      encoding: {
        x: {
          field: midpoint
          type: quantitative
        }
        y: {
          field: key
          type: nominal
        }
        color: {
          field: Measure
          type: nominal
        }
        text: {
          aggregate: sum
          field: foo.doc_count
        }
      }
    }
  ]
}

The attempt at inverting this graph current gives the error:

Cannot read properties of undefined (reading 'doc_count')
And uses this code:

{
  $schema: https://vega.github.io/schema/vega-lite/v5.json
  description: A simple bar chart with embedded data.
  data: {
    name: foo_data
    url: {
      %context%: true
      %timefield%: corge
      index: plurghData
      body: {
        size: 0
        aggs: {
          bars: {
            nested: {
              path: bars
            }
            aggs: {
              bar_barFilter: {
                terms: {
                  field: bars.bar_barFilter
                  size: 10
                  order: {
                    top_reverse_nested.doc_count: desc
                  }
                }
                aggs: {
                  top_reverse_nested: {
                    reverse_nested: {
                    }
                    aggs: {
                      foo_buckets: {
                        terms: {
                          field: foo
                            order: {
                              _key: asc
                            }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
    transform: [
    { 
      flatten: [
        top_reverse_nested.foo_buckets.buckets
      ]
      as: [
        foo
      ]
    }
  ]
  transform: [
      {
      flatten: [
        aggregations.bars.bar_barFilter.buckets
      ]
      as: [
      bFilter_buckets
      ]
    }
  ]

      
  encoding: {
    x: {
      field: foo.doc_count
      title: Number
      type: quantitative
      axis: {
        titlePadding: 20
      }
    }
    y: {
      field: key
      title: Top 10
      type: nominal
      sort: desc
      axis: {
        labelLimit: 300
        titlePadding: 50
      }
    }
    color: {
      field: foo.key
      scale: {
        range: [
          "#209280"
          "#cc5642"
          "#e2634a"
          "#d6bf57"
          "#54b399"
          "#209280"
        ]
        type: ordinal
      }
    }
    tooltip: [
      {
        field: key
        title: Waldo
      }
      {
        field: foo.key
        type: nominal
        title: Fred
      }
      {
        field: foo.doc_count
        type: quantitative
        title: Number
      }
    ]
  }
  layer: [
    {
      mark: {
        type: bar
        height: {
          band: 0.8
        }
      }
      params: [
        {
          name: highlight
          select: {
            on: mouseover
            type: point
            empty: false
          }
        }
      ]
    }
    {
      transform: [
        {
          stack: foo.doc_count
          groupby: [
            key
          ]
          as: [
            lower
            upper
          ]
        }
        {
          calculate: (datum.lower + datum.upper) / 2
          as: midpoint
        }
        {
          joinaggregate:[{
            op: sum
            field: foo.doc_count
            as: total_doc_count
          }]
        }
        {
          filter: datum.upper - datum.lower > datum.total_doc_count * 0.005
        }
      ]
      mark: {
        type: text
        fill: black
        fontSize: 13
      }
      encoding: {
        x: {
          field: midpoint
          type: quantitative
        }
        y: {
          field: key
          type: nominal
        }
        color: {
          field: Measure
          type: nominal
        }
        text: {
          aggregate: sum
          field: foo.doc_count
        }
      }
    }
  ]
}

Hi @Gemma_S, are you comfortable sharing some data from your index?

If so, you can post the spec that Kibana actually sends to Vega by opening the Inspector tab. (It makes it a lot easier to see what is going on using the online editor).

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.