# Generate chart from custom aggregation

**URL:** <https://discuss.elastic.co/t/generate-chart-from-custom-aggregation/226389>\
**Category:** Kibana\
**Created:** [April 3, 2020, 10:46am UTC](https://discuss.elastic.co/t/generate-chart-from-custom-aggregation/226389 "2020-04-03T10:46:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oula\_oula](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/oula_oula/32/63182_2.png) [@oula\_oula](https://discuss.elastic.co/u/oula_oula)\
**Post date:** [April 3, 2020, 10:46am UTC](https://discuss.elastic.co/t/generate-chart-from-custom-aggregation/226389/1 "2020-04-03T10:46:21Z")

</div>

Hi,

I'm trying to make a chart that shows for each superhero the time he's spent between to cities.  
The problem is that I cannot add a field using "scripted field" method because it cannot match multiple docs at the same time. So, I think I should use aggregations but I don't really understand how it works for generating charts.

Actually, my goal is to join two docs by superhero "name" fields and make a duration by calculating the time difference between "arrival" fields of each document of the chosen superhero.  
At the end I would like to have a chart gathering all superheros (or filtered ones) times duration calculated for disclosing who is lingering at the bar.

Thank you

```auto
#Doc A
{
        "_index" : "test_inds",
        "_type" : "_doc",
        "_score" : 1.0,
        "_source" : {
          "name" : """Iron Man""",
          "arrival" : "2020-01-01T06:20:00.000Z",
          "city" : "New-York",
          "@timestamp" : "2020-04-01T14:11:58.536Z",
          "loglvl" : "INFO"
        }
      },
#Doc B
      {
        "_index" : "test_inds",
        "_type" : "_doc",
        "_score" : 1.0,
        "_source" : {
          "name" : """Iron Man""",
          "arrival" : "2020-01-03T10:50:00.000Z",
          "city" : "Los Angeles",
          "@timestamp" : "2020-04-01T14:11:58.536Z",
          "loglvl" : "INFO"
        }

```

---

<div class="post-metadata">

**Author:** ![matw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/matw/32/13913_2.png) [@matw](https://discuss.elastic.co/u/matw)\
**Post date:** [April 3, 2020, 11:31am UTC](https://discuss.elastic.co/t/generate-chart-from-custom-aggregation/226389/2 "2020-04-03T11:31:03Z")

</div>

Hi @oula_oula

This is not possible with scripted fields, you would need have this pre-aggregated, so you'd need 1 record with timestamp start, end, duration to make it work.  
It might be possible to do with the Vega-Lite visualization of Kibana. It supports aggregations and calculations with the given records

> **[Aggregation](https://vega.github.io/vega-lite/docs/aggregate.html)**
>
> Vega-Lite - a high-level grammar for statistical graphics. Vega-Lite provides a higher-level grammar for visual analysis, comparable to ggplot or Tableau, that generates complete Vega specifications. Vega-Lite specifications consist of simple...

  

> **[Calculate Transform](https://vega.github.io/vega-lite/docs/calculate.html)**
>
> Vega-Lite - a high-level grammar for statistical graphics. Vega-Lite provides a higher-level grammar for visual analysis, comparable to ggplot or Tableau, that generates complete Vega specifications. Vega-Lite specifications consist of simple...

Best,  
Matthias

---

<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:** [May 1, 2020, 11:31am UTC](https://discuss.elastic.co/t/generate-chart-from-custom-aggregation/226389/3 "2020-05-01T11:31:09Z")

</div>

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