# Binding data with aggregations result into GroupBarChart with Vega

**URL:** <https://discuss.elastic.co/t/binding-data-with-aggregations-result-into-groupbarchart-with-vega/134026>\
**Category:** Kibana\
**Created:** [May 31, 2018, 10:46am UTC](https://discuss.elastic.co/t/binding-data-with-aggregations-result-into-groupbarchart-with-vega/134026 "2018-05-31T10:46:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pqt120583](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@pqt120583](https://discuss.elastic.co/u/pqt120583)\
**Post date:** [May 31, 2018, 10:46am UTC](https://discuss.elastic.co/t/binding-data-with-aggregations-result-into-groupbarchart-with-vega/134026/1 "2018-05-31T10:46:25Z")

</div>

Hi all,

I'm in trouble to binding this data to Vega group bar chart:

#data1:

> [  
> {  
> "key": "morning",  
> "from": 0,  
> "to": 11,  
> "doc\_count": 125,  
> "orders ": {  
> "value": 56  
> },  
> "customers": {  
> "value": 47  
> }  
> },  
> {  
> "key": "afternoon",  
> "from": 13,  
> "to": 18,  
> "doc\_count": 25,  
> "orders": {  
> "value": 13  
> },  
> "customers": {  
> "value": 21  
> }  
> },  
> {  
> "key": "evening",  
> "from": 18,  
> "to": 24,  
> "doc\_count": 35,  
> "orders": {  
> "value": 19  
> },  
> "customers": {  
> "value": 23  
> }  
> }  
> ]

If following vega sample I need to transform data to the following :

#data2:

> ```
> [
> {
> "key": "morning",
> "from": 0,
> "to": 11,
> "doc_count": 125,
> "position": 0,
> "value": 56,
> "name": "orders"
> },	
> {
> "key": "morning",
> "from": 0,
> "to": 11,
> "doc_count": 125,
> "position": 1,
> "value": 47,
> "name": "customers"
> },
> {
> "key": "afternoon",
> "from": 13,
> "to": 18,
> "doc_count": 25,
> "position": 0,
> "value": 13,
> "name": "orders"
> },
> {
> "key": "afternoon",
> "from": 13,
> "to": 18,
> "doc_count": 25,
> "position": 0,
> "value": 21,
> "name": "customers"
> },
> {
> "key": "evening",
> "from": 18,
> "to": 24,
> "doc_count": 35,
> "position": 0,
> "value": 19,
> "name": "orders"
> },
> {
> "key": "evening",
> "from": 18,
> "to": 24,
> "doc_count": 35,
> "position": 1,
> "value": 23,
> "name": "customers"
> }
> ]
> 
> ```

Is it possible to use vega transform to convert the data #1 to data #2 ?  
Or is there any other implemetation which can binding this data to group bar chart with VEGA ?

Anyway, I can display this data by using the build-in chart in kibana easily but I want to use VEGA for the UI customization.

Thanks,  
Thomas.

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [May 31, 2018, 6:42pm UTC](https://discuss.elastic.co/t/binding-data-with-aggregations-result-into-groupbarchart-with-vega/134026/2 "2018-05-31T18:42:39Z")

</div>

Hi, when you need data to come back in a tabular format, Elasticsearch's composite aggregation can be very useful: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-composite-aggregation.html)

Say I have data that looks like:

```auto
{
  "_source": {
    "@date": "2018-05-30T07:19:46Z",
    "key": "dinner",
    "orders": {
      "metric": 721
    }
  }
},
{
  "_source": {
    "@date": "2018-05-29T13:46:28Z",
    "key": "lunch",
    "customers": {
      "value": 433
    }
  }
},

```

A composite aggregation can return the data in a tabular format:

```auto
POST /restaurant/_search?size=0
{
  "aggs": {
    "table": {
      "composite": {
        "sources": [
          {
            "date": {
              "date_histogram": { "field": "@date", "interval": "day" }
            }
          },
          {
            "customers": {
              "terms": { "field": "customers.value" }
            }
          },
          {
            "orders": {
              "terms": { "field": "orders.value" }
            }
          }
        ]
      }
    }
  }
}

```

You can use any kind of bucket aggregation as source aggregations.

---

<div class="post-metadata">

**Author:** ![pqt120583](https://avatars.discourse-cdn.com/v4/letter/p/ee59a6/32.png) [@pqt120583](https://discuss.elastic.co/u/pqt120583)\
**Post date:** [June 1, 2018, 4:19am UTC](https://discuss.elastic.co/t/binding-data-with-aggregations-result-into-groupbarchart-with-vega/134026/3 "2018-06-01T04:19:59Z")

</div>

Thank tsullivan,

I have tried to use the composite aggregation, but i cannot make range aggregations as a item of source.

Here's my previous query to get the result with aggregations:

```
GET test/_search
{
  "size":0,
	"_source":{"excludes":[]},
	"aggs":{
		"hours":{
			"range":{
				"script":{ "inline":"doc['logTimeStamp'].date.hourOfDay","lang":"painless"},
				"ranges":[
					{"key": "morning" , "from":0,"to":11},
					{"key": "afternoon", "from":13,"to":18},
					{"key": "evening", "from":18,"to":24}					
				]},
				"aggs":{
					"orders":{
						"cardinality":{"field":"order.orderCode.keyword"}
					}
					,"customers":{
						"cardinality":{"field":"customer.customerCode.keyword"}
					}
				}
		}
	}
}

```

Do you have any idea of converting this query to return the tabular data ?

Many thanks,  
Thomas.

---

<div class="post-metadata">

**Author:** ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)\
**Post date:** [June 1, 2018, 8:36pm UTC](https://discuss.elastic.co/t/binding-data-with-aggregations-result-into-groupbarchart-with-vega/134026/4 "2018-06-01T20:36:06Z")

</div>

Unfortunately, composite it pretty strict about what kind of aggregations can be in the source aggregations, and it also isn't possible to have a parent aggregation around a composite aggregation.

If it is at all possible, you'd want to have an `hours` field pre-calculated and be part of the document at index time.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [June 29, 2018, 8:36pm UTC](https://discuss.elastic.co/t/binding-data-with-aggregations-result-into-groupbarchart-with-vega/134026/5 "2018-06-29T20:36:20Z")

</div>

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