Trouble showing data in Vega-lite

Hello Everyone,

I'm trying to plot a vertical bar using Vega-lite getting data from an Index. But the data does not show up in the view, only the axes.

This is the script I'm using:

{
 $schema: "https://vega.github.io/schema/vega-lite/v2.4.0.json"
 mark: bar
 data: {
   url: {
     %context%: true
     %timefield%:_source.data_registrado_contrato
     index: trabalhista,
     body: {
       _source: ["diretoria", "data_registrado_contrato", "id_contrato"]
     }
   }
   format: { property: "hits.hits" }
 }
 transform: [
   {
     aggregate: [{
     op: count
     field: _source.id_contrato
     as: qtd
     }]
     groupby: ["_source.diretoria"]
   }
 ]
 encoding: {

   y:{
     field: qtd
     type: quantitative

   }
   x:{
     field: _source.diretoria
     type: nominal
   }
 }
}

And the mapping of my data:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1499,
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "AcwtemoBAxKStAaIEnOV",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 8.0,
          "diretoria" : "Outras",
          "data_registrado_contrato" : "2016-01-24T00:00:00"
        }
      },
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "BMwtemoBAxKStAaIEnOV",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 11.0,
          "diretoria" : "Comercial",
          "data_registrado_contrato" : "2014-08-01T00:00:00"
        }
      },
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "CcwtemoBAxKStAaIEnOV",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 16.0,
          "diretoria" : "Industrial",
          "data_registrado_contrato" : "2018-09-10T00:00:00"
        }
      },
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "DMwtemoBAxKStAaIEnOW",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 19.0,
          "diretoria" : "Provisão",
          "data_registrado_contrato" : "2015-12-08T00:00:00"
        }
      },
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "FcwtemoBAxKStAaIEnOW",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 28.0,
          "diretoria" : "Demanda",
          "data_registrado_contrato" : "2015-02-09T00:00:00"
        }
      },
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "IMwtemoBAxKStAaIEnOW",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 39.0,
          "diretoria" : "Terceiros",
          "data_registrado_contrato" : "2016-08-29T00:00:00"
        }
      },
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "I8wtemoBAxKStAaIEnOW",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 42.0,
          "diretoria" : "Outras",
          "data_registrado_contrato" : "2016-07-12T00:00:00"
        }
      },
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "JMwtemoBAxKStAaIEnOW",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 43.0,
          "diretoria" : "Industrial",
          "data_registrado_contrato" : "2019-03-05T00:00:00"
        }
      },
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "KswtemoBAxKStAaIEnOW",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 49.0,
          "diretoria" : "Provisão",
          "data_registrado_contrato" : "2015-08-24T00:00:00"
        }
      },
      {
        "_index" : "trabalhista",
        "_type" : "data",
        "_id" : "L8wtemoBAxKStAaIEnOW",
        "_score" : 1.0,
        "_source" : {
          "id_contrato" : 54.0,
          "diretoria" : "Provisão",
          "data_registrado_contrato" : "2014-03-14T00:00:00"
        }
      }
    ]
  }
}

This is running on:
Kibana 6.6.2

Why are you doing the aggregation on the client side in vega? Your query to elasticsearch should preform the aggregation.

Hi Nathan, I'm kind new to Elastic...

Can you tell me how to do this? It's my first time using script to build this plot. Normally, I use the Kibana default options to create the views, but I'm trying to get more personalized view.

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations.html

Back Again,

I've got how to build a plot with vertical bar using aggregations from Elastic, but now my problem is how to build a stacked bar plot?

I'm trying using this script:

{
  "$schema": "https://vega.github.io/schema/vega-lite/v2.4.0.json",
  "data": {
    "url": {
      "index": "trabalhista",
      "%context%": true,
      "_source": [
        "data_registrado_contrato",
        "diretoria",
        "id_contrato"
      ],
      "size":2000,
      "body": {}
    },
    "format": {"property": "hits.hits"}
  },
  "mark": "bar",
  "transform": [
    {
      "calculate": "toDate(datum._source['data_registrado_contrato'])",
      "as": "time"
    }
    
  ],
  "encoding": {
    "x": {
      "timeUnit": "year",
      "field": "time",
      "type": "ordinal",
      "axis": {"title": "Ano"}
    },
    "y":{
    },
    "color": {
      "field": "_source.diretoria",
      "type": "nominal",
      "legend": {"title": "Diretorias"}
    }
  }
}

The "y" axis is missing because I've tried almost all examples I found, but no one seems to work.

Also tried using this as aggregation:

{
  "aggs":{
    "meus_buckets":{
      "composite": {
        "sources":[
          {"data": {"date_histogram": {"field": "data_registrado_contrato", "interval": "year"}}},
          {"diretoria": {"terms": {"field": "diretoria.keyword"}}}
          ]
      }
    }
  },
  "size":0
}

Changing the format and the fields of axes, but no success neither.

Can someone help me?

Hi @BrunoSdeMenezes
Is it maybe you are missing the sclale->range field inside color element? Like in this example
I am not really used to vega-lite, but have some expirience in vega. Not sure if you want to here a solution in vega but you could almost follow this example to create a stacked barchart.

Hope you get a step forward to the solution.

Hi @thekm1 actually i've got a solution and worked fine.

Thanks.

1 Like

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